Results 1 to 6 of 6
  1. #1
    anupamk is offline Member
    Join Date
    Apr 2014
    Posts
    1
    Rep Power
    0

    Default Java Convert String of XML to Document object is null all the time

    I am trying to parse a XML string into `org.w3c.dom.Document` object.

    I have looked at solutions provided [here](xml - How to convert String to DOM Document object in java? - Stack Overflow), [here](How to create a XML object from String in Java? - Stack Overflow) and a few other blogs that give a variation of the same solution. But the `Document` object's #Document variable is always null and nothing gets parsed.

    Here is the XML

    Java Code:
         XMLMappingValidator v = new XMLMappingValidator("<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
        "<mapping>\n" +
        "<container>\n" +
        "<source-container>c:\\stem.csv</source-container>\n" +
        "<source-type>CSV_FILE</source-type>\n" +
        "<target-container>db</target-container>\n" +
        "<target-type>MySQL_TABLE</target-type>\n" +
        "</container>\n" +
        "<entity>\n" +
        "<source-entity>stem</source-entity>\n" +
        "<target-entity>tbl_stem</target-entity>\n" +
        "</entity>\n" +
        "<attributes>\n" +
        "<attribute>\n" +
        "<datatype>SMALLINT</datatype>\n" +
        "<source-attribute>id</source-attribute>\n" +
        "<target-attribute>id</target-attribute>\n" +
        "</attribute>\n" +
        "<attribute>\n" +
        "<datatype>VARCHAR</datatype>\n" +
        "<source-attribute>name</source-attribute>\n" +
        "<target-attribute>name</target-attribute>\n" +
        "</attribute>\n" +
        "<attribute>\n" +
        "<datatype>TEXT</datatype>\n" +
        "<source-attribute>body</source-attribute>\n" +
        "<target-attribute>body</target-attribute>\n" +
        "</attribute>\n" +
        "<attribute>\n" +
        "<datatype>DATETIME</datatype>\n" +
        "<source-attribute>date</source-attribute>\n" +
        "<target-attribute>date</target-attribute>\n" +
        "</attribute>\n" +
        "<attribute>\n" +
        "<datatype>VARCHAR</datatype>\n" +
        "<source-attribute>info</source-attribute>\n" +
        "<target-attribute>info</target-attribute>\n" +
        "</attribute>\n" +
        "</attributes>\n" +
        "</mapping>");
    The constructor XMLMappingValidator object's code is as follows

    Java Code:
        public class XMLMappingValidator {
            private  Document xml;
            public XMLMappingValidator (String xmlString) throws 
                    IOException, SAXException, ParserConfigurationException {
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();  
                DocumentBuilder builder = factory.newDocumentBuilder();  
                this.xml = 
                        builder.parse(new InputSource(new StringReader(xmlString)));          
            }
        
            public Document getXML() {
                return xml;
            }
        }

    When I call
    Java Code:
    **v.getXML().toString()**
    I get
    Java Code:
    `[#document: null]`
    Clearly, the parse is failing. But I don't understand why.

    Any suggestions/help is greatly appreciated.

    Thanks

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Java Convert String of XML to Document object is null all the time

    Form the text string first as a String variable and print that variable out, because the very first line of that XML does not look valid to me.

    Indeed, when you've printed it out, copy/paste that text into an XML editor, which should highlight any format problems.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Java Convert String of XML to Document object is null all the time

    There's actually nothing wrong with getting "[#document: null]". This is due to the implementation of the underlying toString() method.

    One of the simplest ways to print out a Document object is to use an identity transform, e.g.,
    Java Code:
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.newTransformer().transform(new DOMSource(v.getXML()), new StreamResult(System.out));
    If you're wondering why Document.toString() returns "[#document: null]", do this:
    Java Code:
    System.out.println(v.getXML().getClass());
    The above will print out the actual implementation class of the Document interface. For me it's com.sun.org.apache.xerces.internal.dom.DeferredDoc umentImpl. From DeferredDocumentImpl (Xerces2 Implementation) you'd find that toString() is inherited from org.apache.xerces.dom.NodeImpl, and from GC: NodeImpl - org.apache.xerces.dom.NodeImpl (.java) - GrepCode Class Source:
    Java Code:
    return "["+getNodeName()+": "+getNodeValue()+"]";
    where getNodeValue() simply returns a null. (geNodeValue() is meant to be overridden by some of NodeImpl's other subclasses.)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,097
    Rep Power
    20

    Default Re: Java Convert String of XML to Document object is null all the time

    Oh, hang on. That line isn't wrong...it's the damn forum software snipping off the line!
    Sorry...:)
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    scsvel's Avatar
    scsvel is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Re: Java Convert String of XML to Document object is null all the time

    Java Code:
    TransformerFactory factory = TransformerFactory.newInstance();
    factory.newTransformer().transform(new DOMSource(v.getXML()), new StreamResult(System.out));
    Could you pls tell me how to assign the output to a string variable instead of displaying it in console.
    Thanks...

  6. #6
    scsvel's Avatar
    scsvel is offline Member
    Join Date
    Jun 2011
    Posts
    3
    Rep Power
    0

    Default Re: Java Convert String of XML to Document object is null all the time

    Got it.

    Java Code:
    File myFile = new File("out.txt");
    factory.newTransformer().transform(new DOMSource(v.getXML()), new StreamResult(new FileOutputStream(myFile)));
    Created temp file and stored output and then read the file content. Thanks...

Similar Threads

  1. How do I convert a String[] object to a Set?
    By RPFeltz in forum New To Java
    Replies: 5
    Last Post: 08-14-2014, 04:41 PM
  2. How to convert string array into object in java
    By kgkamaraj in forum New To Java
    Replies: 4
    Last Post: 02-12-2010, 12:33 PM
  3. How to convert a string array into object in java
    By kgkamaraj in forum Advanced Java
    Replies: 2
    Last Post: 02-12-2010, 09:12 AM
  4. Convert String to Time format
    By plasma in forum New To Java
    Replies: 1
    Last Post: 12-16-2009, 08:58 AM
  5. convert a string to an object in java
    By jforce93 in forum Advanced Java
    Replies: 1
    Last Post: 08-09-2009, 11:57 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •