Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

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

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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 in a few blogs that give a variation of the same solution that I have written. But the `Document` object's #Document variable is always null and nothing gets parsed.

    Here is the XML

         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

        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
    **v.getXML().toString()**
    I get
    `[#document: null]`

    Clearly, the parse is failing. But I don't understand why.

    Any suggestions/help is greatly appreciated.

    Thanks


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,103
    My Mood
    Mellow
    Thanks
    186
    Thanked 656 Times in 643 Posts

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

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

  3. #3
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

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

    Cross-post: Java Convert String of XML to Document object is null all the time

Similar Threads

  1. convert excel to xml and read the input from xml file
    By rahulruns in forum Object Oriented Programming
    Replies: 5
    Last Post: April 3rd, 2012, 11:13 AM
  2. string==null or string.equals(null) problem
    By csharp100 in forum What's Wrong With My Code?
    Replies: 31
    Last Post: November 4th, 2011, 08:17 AM
  3. Convert object to String
    By innspiron in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 15th, 2010, 08:48 AM
  4. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM
  5. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM