• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Cannot get the proper XML document as the output

 
Harshi Gunarathna
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi..
I am new to XML and DOM.I'm trying to output an XML document.I am using LinkedHashMap as the input to provide elements and subelements. The code is:

LinkedHashMap one = new LinkedHashMap();
LinkedHashMap two = new LinkedHashMap();
LinkedHashMap three = new LinkedHashMap();
LinkedHashMap four = new LinkedHashMap();
LinkedHashMap five = new LinkedHashMap();
one.put("No_Of_Tickets", two);
two.put("Time", three);
three.put("Adult", four);
four.put("Balcony", 5);
four.put("ODC", 10);
three.put("Children", five);
five.put("Balcony", 15);
five.put("ODC", 10);

Then i am using the below code to print the xml document.

public class Test {

//No generics
private LinkedHashMap table;
private Document dom;
Element rootEle;
Element one;

public Test(LinkedHashMap table) {
//create a list to hold the data
this.table = table;
//Get a DOM object
createDocument();
createDOMTree();
printToFile();
}

private void createDOMTree() {

//create the root element <Reports>
rootEle = dom.createElement("Report");

dom.appendChild(rootEle);

x((LinkedHashMap) table);


}

private void x(LinkedHashMap t) {
Iterator it = t.entrySet().iterator();
while (it.hasNext()) {
Map.Entry pairs = (Map.Entry) it.next();
Object key = pairs.getKey();
Object value = pairs.getValue();
one = createElement(key, value);
rootEle.appendChild(one);
}
}

private Element createElement(Object key, Object value) {
Element next = dom.createElement(key.toString());

if (value instanceof LinkedHashMap) {

x((LinkedHashMap) value);
} else {
Text oneText = dom.createTextNode(value.toString());
next.appendChild(oneText);
}
return next;
}


public void printToFile() {


try {

//print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);
//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File("C:/java/JavaApplication6/a.xml")), format);

serializer.serialize(dom);

} catch (IOException ie) {
ie.printStackTrace();
}
}

private void createDocument() {

//get an instance of factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocumentBuilder();

//create an instance of DOM
dom = db.newDocument();

} catch (ParserConfigurationException pce) {
//dump it
System.out.println("Error " + pce);
System.exit(1);
}

}
}

But as the output i get the below XML document.

<?xml version="1.0" encoding="UTF-8"?>
<Report>
<Balcony>5</Balcony>
<ODC>10</ODC>
<Adult/>
<Balcony>15</Balcony>
<ODC>10</ODC>
<Children/>
<Time/>
<No_Of_Tickets/>
</Report>

Here the correct output should be:

<?xml version="1.0" encoding="UTF-8"?>
<Report>
<No_Of_Tickets>
<Time>
<Adult>
<Balcony>5</Balcony>
<ODC>10</ODC>
<Adult/>
<Children>
<Balcony>15</Balcony>
<ODC>10</ODC>
<Children/>
<Time/>
<No_Of_Tickets/>
</Report>

Can somebody help me to find the issue with the code.Thanks in advance.

Regards,
Harshi
 
Paul Clapham
Sheriff
Posts: 21113
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your "x()" method always adds elements to the root. That's not what you want.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic