Avatar of Member_2_1242703
Member_2_1242703
 asked on

Creating an XML file with nested elements in Java

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();

Open in new window

JavaJScriptProgramming

Avatar of undefined
Last Comment
for_yan

8/22/2022 - Mon
for_yan

Sorry, maybe I'm missing something, but why is that an issue: if you added <MySubChild1> to <MyMainChild>
why can't you use the same way to add <MySubSubChild1> to  <MySubChild3> ?
Member_2_1242703

ASKER
I've tried but it always produces each as an individual child element instead of one nested inside the other.
for_yan

Can you post the full program,
so that I could try it myself.
And form which library do you use Utilities ?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
for_yan

Actually does not matter - I can figure out saomething myself
ASKER CERTIFIED SOLUTION
for_yan

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Member_2_1242703

ASKER
Getting close i think....


<ns0:MyNS0 xmlns:ns0="http://www.blahblah.org">
<MyMainChild>
<Attribute1>123</Attribute1>
<SubChild one="good">
<SubSubChild>5234</SubSubChild>
</SubChild>
<dqwdqwdqwdqw3>
<dwqdwdqdww3>345</dwqdwdqdww3>
</dqwdqwdqwdqw3>
<MyMain2>
<fwefwefwe2>456</fwefwefwe2>
</MyMain2>
</MyMainChild>
</ns0:MyNS0>
for_yan

Yes, it has SubSubChild nested within SubChild - so now just follow your structure.

It is also a little confusing to name text "Attribute" , of couser you can have any text , but in XML sense
attribute is of couser siomething like that <SubChild one="good"> , so "one" is attribute
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
for_yan


Actually the main point is that you create all elements invoking createElement() method on doc
It is the way how you use appendChild() for them - that is what determines the structure, the number of netsed levels, etc.
Member_2_1242703

ASKER
I got it. Thanks!
for_yan

Great!  You are always welcome.
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck