0

For the message below, my parsing code is giving :name #text value.

Why it is returing this record?

Message:

<emp>
<emp_name>david</emp-name>
<emp-no>123</emp-no>
</emp>

code:

final Document doc = parser.loadXml(message);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getDocumentElement().getChildNodes();
public Map<String, String> getElementKeyValue(NodeList nodeList) {
  Map<String, String> elements = new HashMap<String, String>();
  if (nodeList != null && nodeList.getLength() > 0) {
    for (int i = 0; i < nodeList.getLength(); i++) {
      Node node = nodeList.item(i); // Take the node from the list
      NodeList valueNode = node.getChildNodes(); // get the children // of the node
      String value = (valueNode.item(0) != null) ? valueNode.item(0)
                            .getNodeValue() : "";
      System.out.println("name " + node.getNodeName() + "  value "
                            + value);
      elements.put(node.getNodeName(), value);
    }
  }
  return elements;
}
3
  • Can you post the output that you are getting? and what is the expected output that you want? Commented Aug 23, 2013 at 12:33
  • name #text value name emp_name value david name #text value name emp-no value 123 Commented Aug 23, 2013 at 13:20
  • exepected: name emp_name value david ,name emp_no value 123 Commented Aug 23, 2013 at 13:21

1 Answer 1

1

The output name #text value stands for the text nodes that are between the tags. To avoid that in the code above, the method getElementsByTagName("emp_name") and getElementsByTagName("emp-no") could be used instead. Alternative: specifying corresponding whitespace settings for these elements in XSD.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.