I'm trying to parse an XML file into an XML file. So basically I'm appending some static elements, attributes, and Text Content for the most part, but I have about 16 dynamic TextContent fields that I want to parse from an existing XML document into my new one. The way I want to do it is to use a for statement the nodes to iterate through to pull the information. I'm getting an implementation error on the for statements and I'm not sure how else to do this without a for statement. Any ideas? Thanks. Here's the relevant code so far:
public static void main(String[] args) { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder loader = factory.newDocumentBuilder(); Document input = loader.parse(new FileInputStream("c:\\XML\\input.xml")); Node Order = input.getDocumentElement(); NodeList DetailsList = input.getElementsByTagName("Details"); for (Node Detail : DetailsList) { DocumentBuilder docBuilder = factory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); Element root = doc.createElement("MercuryGate"); doc.appendChild(root); //...Various create element and append statements follow NodeList FieldsList = input.getElementsByTagName("Field"); for (Node Field : FieldsList){ //...My for statements that pull the content and then insert it into the setTextContent fields where necessary in my previous code TransformerFactory tranFactory = TransformerFactory.newInstance(); Transformer aTransformer = tranFactory.newTransformer(); Source src = new DOMSource(doc); Result dest = new StreamResult("c:\\XML\\xmltest.xml"); aTransformer.transform(src, dest); } } catch(Exception e){ System.out.println(e.getMessage()); }