2 Replies Latest reply: Mar 4, 2014 9:35 PM by 96af9c3e-c798-433a-9a5a-c34c807f9218 RSS

    Converting Strings To XML And Back To Strings

    96af9c3e-c798-433a-9a5a-c34c807f9218

      Hello Experts,

       

      I'm stucked with the following error message when i try to convert a String to XML and back to a String.

       

      Exception in thread "main" org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.

          at com.sun.org.apache.xerces.internal.dom.ParentNode.internalInsertBefore(Unknown Source)

          at com.sun.org.apache.xerces.internal.dom.ParentNode.insertBefore(Unknown Source)

          at com.sun.org.apache.xerces.internal.dom.NodeImpl.appendChild(Unknown Source)

          at com.innovatrail.consulting.xml.XMLTEST2.main(XMLTEST2.java:66)

       

       

       

      Below is  my Java code snippet: The code syntactically correct except line 66. If you comment out line 66 in the source code, everything works just fine.

      The XML representation of my input string is denoted by INPUT 1 and that of my output string is denoted by OUTPUT 2.

       

      package com.Test.xml;

       

      import java.io.ByteArrayInputStream;

      import java.io.StringWriter;

       

      import javax.xml.parsers.DocumentBuilder;

      import javax.xml.parsers.DocumentBuilderFactory;

       

      import javax.xml.transform.Transformer;

      import javax.xml.transform.TransformerConfigurationException;

      import javax.xml.transform.TransformerException;

      import javax.xml.transform.TransformerFactory;

      import javax.xml.transform.dom.DOMSource;

      import javax.xml.transform.stream.StreamResult;

       

      import org.w3c.dom.*;

       

       

      public class XMLTEST2 {

       

            public static DocumentBuilderFactory factory                = null;

            public static DocumentBuilder        builder                = null;

            public static Document               doc1                   = null;

            public static Document               doc2                   = null;

       

      String inputxmlstr = " <Collection><BookList><Book><Title>Wonder Man</Title><Author>John Smith</Author><Category>Fiction</Category></Book><Book>

      <Title>Wonder Woman</Title><Author>Anna Smith</Author><Category>Fiction</Category></Book></BookList><BookList><Book><Title>Wonder Man</Title><Author>John Smith</Author><Category>Fiction</Category></Book><Book><Title>Wonder Woman</Title><Author>Anna Smith</Author><Category>Fiction</Category>

      </Book></BookList></Collection>";

       

      String outputxmlstr1 = "<Collection></Collection>";

       

            /**

             * @param args

             */

            public static void main(String[] args) {

                  // TODO Auto-generated method stub

                 

                                              

              try {


               factory = DocumentBuilderFactory.newInstance();

                        builder = factory.newDocumentBuilder();

                        doc1    = builder.parse( new ByteArrayInputStream(inputxmlstr.getBytes()) );

                        doc2    = builder.parse( new ByteArrayInputStream(outputxmlstr1.getBytes()) );

       

                 

              } catch (Exception e) {

                  e.printStackTrace();

              }

             

       

                  // extract and manipulate xml nodes

                 

                  NodeList booklist = doc1.getElementsByTagName("Book");

                 

                              for(int i=0; i<booklist.getLength();i++){

                       

                              Node root = doc2.getDocumentElement();

                             

      Error Line 66:         root.appendChild(booklist.item(i));                       

                             

                        }

                                          

                 

                              try {

                       

                                        // convert xml document back to string

                                       tf          = TransformerFactory.newInstance();         

                      transformer = tf.newTransformer();

                      StringWriter writer = new StringWriter();

                                      transformer.transform(new DOMSource(doc2), new StreamResult(writer));

          

                  } catch (TransformerConfigurationException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                  } catch (TransformerException e) {

                        // TODO Auto-generated catch block

                        e.printStackTrace();

                  }

       

             }

        }

       

       

      Your help and tip will be appreciated. Thanks in advance.

       

      Yours Ringo

       

      INPUT 1:

      
      
      <Collection>
      <BookList>
        <Book>
          <Title>Wonder Man
          </Title>
          <Author>John Smith
          </Author>
          <Category>Fiction
          </Category>
        </Book>
        <Book>
          <Title>Wonder Woman
          </Title>
          <Author>Anna Smith
          </Author>
          <Category>Fiction
          </Category>
        </Book>
      </BookList>
      <BookList>
        <Book>
          <Title>Wonder Man
          </Title>
          <Author>John Smith
          </Author>
          <Category>Fiction
          </Category>
        </Book>
        <Book>
          <Title>Wonder Woman
          </Title>
          <Author>Anna Smith
          </Author>
          <Category>Fiction
          </Category>
        </Book>
      </BookList>
      </Collection>
      

       

       

      OUTPUT 2:

      
      
      <Collection>
        <Book>
          <Title>Wonder Man</Title>
          <Author>John Smith</Author>
          <Category>Fiction</Category>
        </Book>
        <Book>
          <Title>Wonder Woman</Title>
          <Author>Anna Smith</Author>
          <Category>Fiction</Category>
        </Book>
        <Book>
          <Title>Wonder Man</Title>
          <Author>John Smith</Author>
          <Category>Fiction</Category>
        </Book>
        <Book>
          <Title>Wonder Woman</Title>
          <Author>Anna Smith</Author>
          <Category>Fiction</Category>
        </Book>
      </Collection>