sudhakarpothu
Programmer
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
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