简体   繁体   中英

How to parse an xml and get the content of specific element

My xml String is

Got message from Queue ==> <?xml version='1.0' encoding='UTF-8'?><soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003
/05/soap-envelope"><soapenv:Body><ns1:PostPublicationResponse xmlns:ns1="http://www.openoandm.org/xml/ISBM/"><ns1:Messag
eID>urn:uuid:7d361fb0-bc54-48bd-bbd1-6e34960ef3f8</ns1:MessageID><ns1:MessageContent><MessageContent xmlns="http://www.o
penoandm.org/xml/ISBM/"><hi>k786</hi></MessageContent></ns1:MessageContent></ns1:PostPublicationResponse></soapenv:Body>
</soapenv:Envelope>

Now i have writtent a function that is trying to get Content of element MessageContent ie <hi>k786</hi> but i am getting null value always. My function to parse above xml is:

private String parseQueueMessage(String message)
        throws ParserConfigurationException, SAXException, IOException,
        XPathExpressionException {
    String resultMsg = "";
    DocumentBuilderFactory domFactory = DocumentBuilderFactory
            .newInstance();
    domFactory.setNamespaceAware(true);

    DocumentBuilder builder = domFactory.newDocumentBuilder();

    Document doc = builder.parse(new InputSource(new java.io.StringReader(
            message)));

    XPath xpath = XPathFactory.newInstance().newXPath();
    // XPath Query for showing all nodes value

    xpath.setNamespaceContext(new NamespaceContext() {

        @SuppressWarnings("rawtypes")
        @Override
        public Iterator getPrefixes(String arg0) {
            return null;
        }

        @Override
        public String getPrefix(String arg0) {
            return null;
        }

        @Override
        public String getNamespaceURI(String arg0) {
            if("xmlns:ns1".equals(arg0)) {
                return "http://www.openoandm.org/xml/ISBM/";
            }
            return null;
        }
    });


    XPathExpression expr = xpath.compile("//xmlns:ns1:MessageContent");

    Object result = expr.evaluate(doc, XPathConstants.NODESET);

    NodeList nodes = (NodeList) result;
    for (int i = 0; i < nodes.getLength(); i++) {
        System.out.println("The message obtained after parsing : "
                + nodes.item(i).getNodeValue());
        resultMsg = nodes.item(i).getNodeValue();
    }

    return resultMsg;
}

What i have done wrong in here? Thanks in advance

You need to define the name space URI first before selecting from XPATH. For example, first define the namespace URI as follows on the root;

element.setAttribute("xmlns:ns1", "http://www.openoandm.org/xml/ISBM/");
xpath.compile("//ns1:MessageContent");

//Try something like ...

XmlDocument doc = new XmlDocument(); doc.LoadXml("urn:uuid:7d361fb0-bc54-48bd-bbd1-6e34960ef3f8k786 ");

XmlElement elem = (XmlElement) doc.DocumentElement.FirstChild;
Console.Write("{0}:{1} = {2}", elem.Prefix, elem.LocalName, elem.InnerText);
Console.WriteLine("\t namespaceURI=" + elem.NamespaceURI);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM