This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
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

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
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

Cannot get the proper XML document as the output

Harshi Gunarathna

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

private void createDOMTree() {

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


x((LinkedHashMap) table);


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

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

public void printToFile() {

try {

OutputFormat format = new OutputFormat(dom);
//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);


} catch (IOException ie) {

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


But as the output i get the below XML document.

<?xml version="1.0" encoding="UTF-8"?>

Here the correct output should be:

<?xml version="1.0" encoding="UTF-8"?>

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

Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Your "x()" method always adds elements to the root. That's not what you want.
I agree. Here's the link:
subject: Cannot get the proper XML document as the output
It's not a secret anymore!