0

I want to convert XML string into Document in java. the code is below..

package org.com;

import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;


public class MainPage {

    public static void main(String[] args) 
    {
        final String xmlStr = "<employees>" + 
                                "   <employee id=\"101\">" + 
                                "        <name>Lokesh Gupta</name>" + 
                                "       <title>Author</title>" + 
                                "   </employee>" + 
                                "   <employee id=\"102\">" + 
                                "        <name>Brian Lara</name>" + 
                                "       <title>Cricketer</title>" + 
                                "   </employee>" + 
                                "</employees>";

        //Use method to convert XML string content to XML Document object
        Document doc = convertStringToXMLDocument( xmlStr);
        doc.getDocumentElement().normalize();
        //Verify XML document is build correctly
        System.out.println(doc.getFirstChild().getNodeName());
    }


    private static Document convertStringToXMLDocument(String xmlString) 
    {
        //Parser that produces DOM object trees from XML content
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        //API to obtain DOM Document instance
        DocumentBuilder builder;
        try
        {
            //Create DocumentBuilder with default configuration
            builder = factory.newDocumentBuilder();

            //Parse the content to Document object
            Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
            return doc;
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
        return null;
    }
}



But when i am trying to debug this code then Document is showing null like this.. [#document: null]. Here question is that how to convert XML string into only in Document object without using Nodelist to read child node one by one.

I referred this Example

Please help..

Thanks in advance

3
  • I don't see what the problem is. When I run the program, the output is employees. See also stackoverflow.com/a/5698623/407651. Commented Dec 23, 2019 at 8:04
  • There is no error in this code but Document is showing null while i was trying to debug. so how to solve this Commented Dec 23, 2019 at 9:15
  • See the answer that I linked to in my comment. Commented Dec 23, 2019 at 9:16

1 Answer 1

0

1,my example use jdom 2,like this code

 final String xmlStr = "<employees>" +
            "   <employee id=\"101\">" +
            "        <name>Lokesh Gupta</name>" +
            "       <title>Author</title>" +
            "   </employee>" +
            "   <employee id=\"102\">" +
            "        <name>Brian Lara</name>" +
            "       <title>Cricketer</title>" +
            "   </employee>" +
            "</employees>";


    StringReader stringReader = new StringReader(xmlStr);

    SAXBuilder builder = new SAXBuilder();
    Document build = builder.build(stringReader);
    Element rootElement = build.getRootElement();
    List employee = rootElement.getChildren();

    for (int i = 0; i < employee.size(); i++) {
        Element emp = (Element) employee.get(i);
        String id = emp.getAttributeValue("id");
        String name = emp.getChildText("name");
        String title = emp.getChildText("title");
        System.out.printf("id: %s,name: %s, title : %s", id, name, title);
        System.out.println();
    }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.