现在,我想解析来自webservice的xml.对于解析,我使用了xpath和java.我有以下代码:
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:
" + 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);
}
}
}
我的String xml看起来像
error="EndOfResults" throttle="2" total-alarms="2">
HBBCPMPLSR1.hdfcami.com
1361252188
HBBCPINTR0.hdfcami.com
1361253339
但是我被困住了.我正在获取nodes.getLength()= 0.
任何帮助将不胜感激.
我想解析整个xml并获取属性的值.