troubleshooting Question

Creating an XML file with nested elements in Java

Avatar of Member_2_1242703
Member_2_1242703 asked on
ProgrammingJavaJScript
10 Comments1 Solution1699 ViewsLast Modified:
I would like to create XML file in a Java program using nested elements. I have no issues creating XML files, I just can't seem to organize them properly. The output should be like this:


<ns0:MyNS0 xmlns:ns0="www.blahblah.org">
<MyRoot>
      <MyMainChild>
            <MySubChild1>Attribute1</MySubChild1>
            <MySubChild2>Attribute2</MySubChild2>
            <MySubChild3>
<MySubSubChild1>Attribute3</ MySubSubChild1>
                  <MySubSubChild2>Attribute4</ MySubSubChild2>
            </MySubChild3>
      </MyMainChild>
      <MyMainChild2>Attribute5</ MyMainChild2>
      <MyMainChild3>Attribute6</ MyMainChild3>
</MyRoot>
</ns0:MyNS0>


I've attached a sample of the code I'm using to give you an idea of how I am creating my files, (and hopefully to discourage posts of links on simple XML creation that I've already seen :-p.)
I know the code does not produce the above, desired, result...just showing how I'm doing it.
My main issue is that I have been unable to nest the child elements (MySubSub1 and MySubSub2 above.)

TIA
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
				            DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
				            Document doc = docBuilder.newDocument();

				            //child	
				            Element root = doc.createElement("ns0:MyNS0");   
				            root.setAttribute("xmlns:ns0","http://www.blahblah.org");
				            doc.appendChild(root); 

				            Element mainChild = doc.createElement("MyMainChild");				            
				            Element childRO = doc.createElement("Attribute1");
				            childRO.appendChild(doc.createTextNode(Utilities.escapeXML(var1)));
				            mainChild.appendChild(childRO);				             
				            root.appendChild(mainChild);
				            
				            Element mainChild2 = doc.createElement("MyMain2");				            
				            Element childRO2 = doc.createElement("fwefwefwe2");
	
				            
				            Element mainChild3 = doc.createElement("dqwdqwdqwdqw3");				            
				            Element childRO3 = doc.createElement("dwqdwdqdww3");
				            childRO3.appendChild(doc.createTextNode(Utilities.escapeXML(var2)));
				            mainChild3.appendChild(childRO3);				             
				            root.appendChild(mainChild3);

				            
				            childRO2.appendChild(doc.createTextNode(Utilities.escapeXML(var3)));
				            root.appendChild(mainChild2);
				            mainChild2.appendChild(childRO2);   			             
				            
				            
				            		            
				            TransformerFactory transfac = TransformerFactory.newInstance();
				            Transformer trans = transfac.newTransformer();
				            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
				            trans.setOutputProperty(OutputKeys.INDENT, "yes");
				            StringWriter sw = new StringWriter();
				            StreamResult result = new StreamResult(sw);
				            DOMSource source = new DOMSource(doc);
				            trans.transform(source, result);
				            String xmlString = sw.toString();
				            
				            File f = new File(outputDir + xmlPrefix + STORED_DATE + "_" + var45 + ".xml");
				            FileWriter fw = new FileWriter(f);
				            fw.write(xmlString);
				            fw.close();
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros