7 Replies Latest reply: Apr 25, 2007 4:46 AM by 807606 RSS

    Jdom xml parsing problem

    807606
      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
        • 1. Re: Jdom xml parsing problem
          807606
          But when I am trying to include it in the child
          "application" it is giving me null pointer
          exception



          octoWrite.getRootElement().getChild("application").add
          Content((Element)(
          doc1.getRootElement().getChild("application").getChild
          ("source").clone()));
          A first step would be to decompose the above and put all the different calls each on its own line. Try to recompile and post the offending line.

          And please use the code tags.
          • 2. Re: Jdom xml parsing problem
            807606
            doctoWrite.getRootElement().getChild("application").addContent((Element)(doc1.getRootElement().clone()));
            when I include
            getChild("application")
            I am getting null pointer exception otherwise it is OK.
            • 3. Re: Jdom xml parsing problem
              807606
              that isn't well-formed XML. or you haven't posted the real XML
              • 4. Re: Jdom xml parsing problem
                807606
                ya It is not the real XML which I'm using but the structure is similar
                • 5. Re: Jdom xml parsing problem
                  807606
                  ya It is not the real XML which I'm using but the
                  structure is similar
                  post the actual XML then, maybe? looks to me like it's not structured how you think it is
                  • 6. Re: Jdom xml parsing problem
                    807606
                    Sym version="0.9.2 " xsi:schemaLocation=" http://sym.com/xml/sym Sym.xsd">
                    <!--Definition of the application structure--> 
                    �|
                        <application>
                    �|
                        <source name="S0" orientation="false" xPosition="120" yPosition="180" width="140" height="100"> 
                    �|
                        <ports> 
                    <outputport name="o0" orientation="false"/>
                    </ports>
                    </source>
                    �|
                        <task name="T0" orientation="false" xPosition="330" yPosition="180" width="140" height="100"> 
                    �|
                        <ports>
                    <inputport name="i0" orientation="false"/>
                    <outputport name="o0" orientation="false"/>
                    </ports>
                    �|
                        <processmodes globalmode="2" sensitiv="false"> 
                    <modelist/>
                    </processmodes>
                    <speedup factor="1.0"/>
                    </task>
                    �|
                        <sink name="S1" orientation="false" xPosition="560" yPosition="180" width="140" height="100"> 
                    
                    �|
                        <ports>
                    �|
                        <inputport name="i0" orientation="false">
                    �|
                        <inputrequirement>
                    �|
                        <standardeventmodel periodic="false"> 
                    <eventmodeltype type="any"/> 
                    </standardeventmodel>
                    </inputrequirement>
                    </inputport>
                    </ports>
                    </sink>
                    �|
                        <eventstream name="E0" orientation="false"> 
                    <src process="S0" outputPort="o0" type="source"/> 
                    <target process="T0" inputPort="i0" type="task"/>
                    <emifeafType>PLAIN_EMIF</emifeafType> 
                    </eventstream>
                    �|
                        <eventstream name="E1" orientation="false"> 
                    <src process="T0" outputPort="o0" type="task"/>
                    <target process="S1" inputPort="i0" type="sink"/> 
                    <emifeafType>PLAIN_EMIF</emifeafType> 
                    </eventstream>
                    </application>
                    
                    </Sym>
                    • 7. Re: Jdom xml parsing problem
                      807606
                      Why don't we go step by step:

                      Do you get ""Sym" as doc.getRootElement?
                      Do you have "application" in the List return by getAllChildren or getChildren on that root?

                      Once you do the above, it should be clear as to what is not working.