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.
- https://howtodoinjava.com/java/xml/parse-string-to-xml-dom/
- How to convert String to DOM Document object in java?
- 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]"