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

1 Answers1

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();
    }
a bug
  • 1