k.horde k.horde - 6 months ago 18
Java Question

XML generation, element is being added incorrectly

I am writing a method which adds few element under the same root element in a loop but when I open the XML file it look like the first element is added correctly and the other ones are added under main element.

This is my method

addStepElement()
:

DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder();
Document document = documentBuilder.parse(file);
document.getDocumentElement().normalize();

Element rootElement = null;
NodeList findRootElementList = document.getElementsByTagName("testDetails");
for(int iterator = 0; iterator < findRootElementList.getLength(); iterator++) {
Node node = findRootElementList.item(iterator);
if(node.getNodeName().equals("testDetails")) {
rootElement = (Element)node;
}
}


Element step = document.createElement("Step");
rootElement.appendChild(step);
step.setAttribute("Step", "className");
step.setAttribute("result", "PASS");
step.setAttribute("screenshot", "file.jpg");
step.setAttribute("input", "email");
step.setAttribute("element", "submit");


Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
DOMSource source = new DOMSource(document);
StreamResult result = new StreamResult(new File(file.getAbsolutePath()));
transformer.transform(source, result);


This is my output XML:

<Test>
<testDetails date="19-05-2016" testName="Test" time="11:24:14">
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
</testDetails>
</Test>


This is what I want to achieve:

<Test>
<testDetails date="19-05-2016" testName="Test" time="11:24:14">
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
<Step Step="className" element="submit" input="email" result="PASS" screenshot="file.jpg"/>
</testDetails>
</Test>


I do my method in a loop:

for(int i = 0; i < 5; i++) {
addStepElement();
}


What do I do wrong?

Answer

There is nothing wrong with the code as it produces an equivalent XML.

Indentation makes XML easier to read by human eye, but it doesn't change the meaning of XML whatsoever. All <Step> elements in the first XML are children of <testDetails> just like those <Step> elements in the 2nd XML. No difference in XML point of view.