1

I have an XML but all I know is the name of the element I want to find the value of. That is, I can't use XPath to easily retrieve the element. I have to recursively work my way through the XML which I seem to fail. What I've tried is:

Assume an XML of the following structure:

<Store id="1">
  ...
  <Customer>
    <CustomerId>123</CustomerId>
  </Customer>
</Store>

I want to get the value of the CustomerId tag. In reality, I don't know where in the XML my CustomerId tag is. I tried the following recursion:

private String parseXmlForCustomerId(Element element) {
    if (element.getNodeName().equals("CustomerId")) {
        return element.getTextContent();
    }
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
        Node node = children.item(i);
        if (node.getNodeType() == Node.ELEMENT_NODE && parseXmlForCustomerId((Element) node).equals("CustomerId")) {
            return node.getTextContent();
        }
    }
    return "";
}

What have I done wrong?

Thanks in advance!

2
  • 1
    Why can't you use xpath? //CustomerId seems fine. Commented Jan 10, 2018 at 14:39
  • Oh didn't know that! Will try! Commented Jan 10, 2018 at 14:44

3 Answers 3

2

Xpath should work using

XPathExpression expr = xpath.compile("//CustomerId");
NodeList nl = (NodeList) expr.evaluate(doc, XPathConstants.NODESET);
Sign up to request clarification or add additional context in comments.

Comments

0
    if (node.getNodeType() == Node.ELEMENT_NODE
            && parseXmlForCustomerId((Element) node).equals("CustomerId")) {
        return node.getTextContent();
    }

should be:

    if (node.getNodeType() == Node.ELEMENT_NODE) {
        String found = parseXmlForCustomerId((Element) node);
        if (!found.isEmpty()) {
            return found;
        }
    }

However XPath is possible.

Comments

0

Wrote a reusable method to parse TagValue from xmlRecords based on nodeTagName.

public String parseXMLwithNodeTagNames(String xmlRecords, String nodeTagName) {
        String tagValue = "";
        try {
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder();
            Document doc = null;
            String s = new String(xmlRecords.getBytes(), "ISO-8859-1");
            doc = db.parse(new InputSource(new StringReader(s)));
            NodeList nl = doc.getElementsByTagName(nodeTagName);
            if (nl != null) {
                for (int i = 0; i < nl.getLength(); i++) {
                    Node item = nl.item(i);
                    String name = item.getNodeName();
                    tagValue = item.getTextContent();

                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return tagValue;

    }

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.