简体   繁体   English

使用XPath和Java解析XML

[英]Parse xml using xpath and java

Now I am want to parse xml which is coming from webservice. 现在,我想解析来自webservice的xml。 For parsing, I used xpath and java. 对于解析,我使用了xpath和java。 I have the following code: 我有以下代码:

package test.client;

import com.sun.org.apache.xpath.internal.NodeSet;
import java.io.FileReader;
import java.io.StringReader;
import javax.lang.model.element.Element;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

public class GetAlarmsCriticalDataCenterLinksDown {
    public static void main(String args[]) throws Exception {
        try {
            GetAlarms ga = new GetAlarms();
            String xml = ga.getAlarms(
                "Alarms", 
                "C:/Documents and Settings/Administrator/My Documents/NetBeansProjects/WebSpectrumDemo/src/java/com/xml/Alarms/GetAlarmsCriticalDataCenterLinksDown.xml");
            System.out.println("Response XML:\n " + xml);
            XPathFactory factory = XPathFactory.newInstance();
            XPath xPath = factory.newXPath();
            String loc = "";
            NodeList nodes = (NodeList) xPath.evaluate(
                "//alarm-response-list/alarm-responses/alarm", 
                new InputSource(new StringReader(xml)), 
                XPathConstants.NODESET);
            System.out.println("Success : " + nodes.getLength());
        } catch (Exception e) {
            System.out.println(e);
        }
    }
}

and my String xml looks like 我的String xml看起来像

<?xml version="1.0" encoding="UTF-8"?>
<alarm-response-list xmlns="http://www.ca.com/spectrum/restful/schema/response"
                     error="EndOfResults" throttle="2" total-alarms="2">
  <alarm-responses>
    <alarm id="51230f5c-11fe-1002-01bc-000c296b3541">
      <attribute id="0x1006e">HBBCPMPLSR1.hdfcami.com</attribute>
      <attribute id="0x11f84" error="NoSuchAttribute" />
      <attribute id="0x118cd" />
      <attribute id="0x11f4e">1361252188</attribute>
    </alarm>
    <alarm id="512313db-c5ee-1003-01bc-000c296b3541">
      <attribute id="0x1006e">HBBCPINTR0.hdfcami.com</attribute>
      <attribute id="0x11f84" error="NoSuchAttribute" />
      <attribute id="0x118cd" />
      <attribute id="0x11f4e">1361253339</attribute>
    </alarm>
  </alarm-responses>
</alarm-response-list>

But I'm stuck. 但是我被困住了。 I'm getting nodes.getLength() = 0 . 我正在获取nodes.getLength() = 0

Any help will be appreciated. 任何帮助将不胜感激。 I want to parse whole xml and get the value of the attribute. 我想解析整个xml并获取属性的值。

You need to set up namespaces on your XPath object. 您需要在XPath对象上设置名称空间。 Use XPath.setNamespaceContext() to do this. 使用XPath.setNamespaceContext()执行此操作。 Alternatively you could just use local names in your XPath expression, eg //*[local-name()='alarm-response-list']/*[local-name()='alarm-responses']/*[local-name()='alarm'] but the namespace option is better. 或者,您可以只在XPath表达式中使用本地名称,例如//*[local-name()='alarm-response-list']/*[local-name()='alarm-responses']/*[local-name()='alarm']但命名空间选项更好。

A whole lot of information about using namespaces with XPath expressions is already in another answer here . 有关在XPath表达式中使用名称空间的大量信息,已经在此处提供了另一个答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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