The DocumentBuilder handler breaks the line when hits '& amp;'

I have this xml:
<user>
<name>H &amp; M</name>

and I will analyze it using this code:


    DocumentBuilder documentBuilder = null;
            Document document = null;

        try {
            documentBuilder = DocumentBuilderFactory.newInstance()
            .newDocumentBuilder();
            document = documentBuilder.parse(is);

        } catch (Exception e) {
            return result;
        }

        NodeList nl = document.getElementsByTagName(XML_RESPONSE_ROOT);
        if (nl.getLength() > 0) {
            resp_code = nl.item(0).getAttributes().getNamedItem(
                    XML_RESPONSE_STATUS).getNodeValue();

            if (resp_code.equals(RESP_CODE_OK_SINGLE)) {
                nl = document
                .getElementsByTagName(XML_RESPONSE_TAG_CONTACT);
                NodeList values = nl.item(i).getChildNodes();

etc..

when I get the value node: node.getNodeValue ();

I only get that before the ampersand, even if the ampersand is shielded

I want to get the whole line: "H and M"

thank

+5
source share
2 answers

It depends on how your XML document was created. In particular, it can have multiple text node nodes in "H and M", while your code expects it to be one. Try using nodeVariable.normalize () before getting its value.

API- DOM: "normalize() - Node, , " ", (, , , , CDATA ) , .. , ..."

+4

"" getTextContent().

0

All Articles