aspose file tools*
The moose likes XML and Related Technologies and the fly likes Cannot get the proper XML document as the output Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Cannot get the proper XML document as the output" Watch "Cannot get the proper XML document as the output" New topic
Author

Cannot get the proper XML document as the output

Harshi Gunarathna
Greenhorn

Joined: Oct 26, 2008
Posts: 8
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
Bartender

Joined: Oct 14, 2005
Posts: 18886
    
    8

Your "x()" method always adds elements to the root. That's not what you want.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cannot get the proper XML document as the output