Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Jdom parsing problem

Status
Not open for further replies.

sudhakarpothu

Programmer
Jan 8, 2007
19
DE
Hello Friends,

I am trying to parse two xml documents (using JDOM) and create a third one which is combination of these two. following is the code I'm using.

import java.io.*;
import java.util.Iterator;
import java.io.IOException;
import java.util.List ;

import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.Format;
import org.jdom.input.SAXBuilder;
import org.jdom.output.XMLOutputter;
import org.jdom.JDOMException;
import org.xml.sax.InputSource ;
import org.jdom.input.SAXBuilder;
import org.jdom.xpath.XPath;

public class Merger {
public static void main(String[] args){

String filename=new String("demosystem.xml");

try {

SAXBuilder builder = new SAXBuilder();

Document doc = builder.build (new File "demoSystem1.xml"));
Document doc1 = builder.build(new File("demoSystem2.xml"));

Document doctoWrite=new Document();
doctoWrite=doc;

//doctoWrite.getRootElement()addContent((Element)( doc1.getRootElement().clone()));

doctoWrite.getRootElement().getChild("application").addContent((Element)( doc1.getRootElement().getChild("application").getChild("source").clone()));

try {
XMLOutputter serializer = new XMLOutputter();
FileOutputStream out = new FileOutputStream(filename);
serializer.output(doctoWrite,out);
}
catch (IOException e) {
System.err.println(e);
}

}catch (Exception e) {
e.printStackTrace ();
}
}
}


//doctoWrite.getRootElement()addContent((Element)( doc1.getRootElement().clone()));

This commented line is working properly when i use it and I am able to merge both the XML.



But when I am trying to include it in the child "application" it is giving me null pointer exception

doctoWrite.getRootElement().getChild("application").addContent((Element)( doc1.getRootElement().getChild("application").getChild("source").clone()));



My XML is

Sys version="2" xsi:schemaLocation="----------------.xsd">
<!--Definition of the application structure-->
?|
<application>
?|
<source name="S0" orientation="false" xPosition="120" yPosition="180" width="140" height="100">

.................................
....................


Please help me what I am doing wrong in this case. Should I use some schema and parse it?

Thanks in advance
 
Well, there are like eight sources of the exception in the same line: I'd try either to debug it to know where the null is coming from or if you don't feel confortable debugging, just split the line to see in which one the exception is being thrown.

With that info, you will be in a better position to analyze the error.

Cheers,
Dian
 
THis line of code is working fine

doctoWrite.getRootElement().addContent((Element)( doc1.getRootElement().clone()));


but when I include .getChild("application") (as below) I have NULL pointer exception

doctoWrite.getRootElement().getChild("application").addContent((Element)( doc1.getRootElement().clone()));
 
I am able to do it if i take out the version="2" xsi:schemaLocation="----------------.xsd"> from root element.

So the clone() doesn't work with schema?

Please write as I am very new to Java programming
 
Then the problem may be that the document you're writing doesn't have a node called "application" yet.

Cheers,
Dian
 
What I see in the XML document is a root <Sys....... then the next element is <application>

Sys version="2" xsi:schemaLocation="----------------.xsd">
<!--Definition of the application structure-->
?|
<application>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top