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:
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();
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 ?
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
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.
why can't you use the same way to add <MySubSubChild1> to <MySubChild3> ?