0

I am getting xml response from 3rd party API via feign client which I am collecting in String and then trying to convert the String into org.w3c.dom.Document.

I have searched for String to Document conversion code and came across below links.

  1. https://howtodoinjava.com/java/xml/parse-string-to-xml-dom/
  2. How to convert String to DOM Document object in java?
  3. https://www.journaldev.com/1237/java-convert-string-to-xml-document-and-xml-document-to-string

problem is my conversion logic is not working and Document = null.

 public static void main(String[] args) {
        final String xmlStr = "<Emp id=\"1\"><name>Pankaj</name><age>25</age>\n"+
                "<role>Developer</role><gen>Male</gen></Emp>";

        Document doc = convertStringToXMLDocument(xmlStr);
 }
 private static Document convertStringToXMLDocument(String xmlString)
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder;
        try
        {
            builder = factory.newDocumentBuilder();
            Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
            return doc;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

I have tried debugging code of builder.parse() but not able to find why document conversion is null.

Output: doc: "[#document : null]"
anonymous
  • 47
  • 10

1 Answers1

0

Your Document doc is not null. The "[#document : null]" only is the output from xerces NodeImpl.toString. It means [element name or element type : element value]. So [#document : null] means document element : no direct value. That's true because only text nodes have direct values.

You will be able to iterate over the Document doc and to get all the child nodes (element nodes as well as text nodes) as follows:

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

public class DocumentBuilderTest {
    
 public static void main(String[] args) {
  final String xmlStr = "<Emp id=\"1\"><name>Pankaj</name><age>25</age>\n"+
                        "<role>Developer</role><gen>Male</gen></Emp>";
  Document doc = convertStringToXMLDocument(xmlStr);
  printAllChildNodes(doc);
 }
 
 public static void printAllChildNodes(Node node) {
  System.out.println(node);

  NodeList nodeList = node.getChildNodes();
  for (int i = 0; i < nodeList.getLength(); i++) {
   Node currentNode = nodeList.item(i);
   printAllChildNodes(currentNode);
  }
 }
 
 private static Document convertStringToXMLDocument(String xmlString) {
  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder;
  try {
   builder = factory.newDocumentBuilder();
   Document doc = builder.parse(new InputSource(new StringReader(xmlString)));
   return doc;
  } catch (Exception e) {
   e.printStackTrace();
  }
  return null;
 }
}

This prints:

[#document: null]
[Emp: null]
[name: null]
[#text: Pankaj]
[age: null]
[#text: 25]
[#text:
]
[role: null]
[#text: Developer]
[gen: null]
[#text: Male]
Axel Richter
  • 56,077
  • 6
  • 60
  • 87