I have tried all possible answers on Stackoverflow but I can't get this resolved.
I have the following xml as a String:
private static final String xmlStr = "<parameters>\n" +
" <parameter>\n" +
" <name>emp</name>\n" +
" <keyvalue>John Smith</keyvalue>\n" +
" </parameter>\n" +
" <parameter>\n" +
" <name>age</name>\n" +
" <keyvalue>22</keyvalue>\n" +
" </parameter>\n" +
" <parameter>\n" +
" <name>Birth Date</name>\n" +
" <keyvalue>02/05/1978</keyvalue>\n" +
" </parameter>\n" +
" <parameter>\n" +
" <name>password</name>\n" +
" <keyvalue>ye63633</keyvalue>\n" +
" </parameter>\n" +
"</parameters>";
The parameters can be replaced with any string (which I tried without sucess), and the following code returns null document:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new ByteArrayInputStream(xmlStr.getBytes("UTF-8")));
I have tried replacing document with:
Document document = builder.parse(new InputSource(new StringReader(xmlStr)));//This used to work 2 weeks ago.
document is returning null document all the time and I can't figure out what I am doing wrong. The same code used to work few weeks ago.
I am using Java 1.8 !!
I appericiate your help.
UPDATE:
Here is the full code that takes the above xmlStr and try to access age of the employee.
private String getElementValue(String tagName, String xmlString) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new InputSource(new StringReader(xmlString)));
Element rootElement = document.getDocumentElement();
NodeList list = rootElement.getElementsByTagName(tagName);
if (list != null && list.getLength() > 0) {
NodeList subList = list.item(0).getChildNodes();
if (subList != null && subList.getLength() > 0) {
return subList.item(0).getNodeValue();
}
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
Based on the comments, the document is not null at all but there is an issue accessing the desired xml tag. So the code NodeList list = rootElement.getElementsByTagName("age");
has size 0. i.e its not entering the loop at all.
It seems to be working correctly. The following code, added after builder.parse
, shows that the XML is being parsed:
System.out.println("Root element: " + document.getDocumentElement().getNodeName());
NodeList nodeList = document.getElementsByTagName("parameter");
for (int temp = 0; temp < nodeList.getLength(); temp++) {
org.w3c.dom.Node node = nodeList.item(temp);
System.out.println("\nCurrent element: " + node.getNodeName());
if (node.getNodeType() == org.w3c.dom.Node.ELEMENT_NODE) {
Element element = (Element) node;
System.out.println("Name: " + element.getElementsByTagName("name").item(0).getTextContent());
System.out.println("Key Value: " + element.getElementsByTagName("keyvalue").item(0).getTextContent());
}
}
Output:
Root element: parameters
Current element: parameter
Name: emp
Key Value: John Smith
Current element: parameter
Name: age
Key Value: 22
Current element: parameter
Name: Birth Date
Key Value: 02/05/1978
Current element: parameter
Name: password
Key Value: ye63633
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With