3

Now I am want to parse xml which is coming from webservice. For parsing, I used xpath and 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

<?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.

Any help will be appreciated. I want to parse whole xml and get the value of the attribute.

1
  • What happens if you remove the namespace from the alarm-response-list?
    – zwets
    CommentedMar 6, 2013 at 5:54

1 Answer 1

4

You need to set up namespaces on your XPath object. Use XPath.setNamespaceContext() to do this. Alternatively you could just use local names in your XPath expression, e.g. //*[local-name()='alarm-response-list']/*[local-name()='alarm-responses']/*[local-name()='alarm'] but the namespace option is better.

A whole lot of information about using namespaces with XPath expressions is already in another answer here.

2
  • After getting the length of response I want to get the value of every attribute but i will not getting the value of the attribute.I use the code likeCommentedMar 6, 2013 at 7:27
  • To get the attribute elements out, iterate over all the nodes in the NodeList then cast the nodes that come out into Element objects. From these elements you can get child elements, XML attributes, text content, etc.
    – prunge
    CommentedMar 6, 2013 at 8:14

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.