Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Using XPath is more efficient and flexible (than normal iteration) while parsing xml.</p> <p><a href="http://www.ibm.com/developerworks/library/x-javaxpathapi/index.html" rel="nofollow">XPath Tutorial from IBM</a> <br/> <a href="http://onjava.com/pub/a/onjava/2005/01/12/xpath.html" rel="nofollow">XPath Reference Orielly tutorial </a> <br/> <a href="http://docs.oracle.com/javase/tutorial/jaxp/xslt/xpath.html" rel="nofollow">Xpath Reference Oracle java tutorial </a><br/></p> <p>Try below code.</p> <pre><code>import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.xpath.XPath; import javax.xml.xpath.XPathConstants; import javax.xml.xpath.XPathExpression; import javax.xml.xpath.XPathExpressionException; import javax.xml.xpath.XPathFactory; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class RequestResponse { public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException { URL url = new URL("http://belbooner.site40.net/testXmls/details.xml"); RequestResponse req = new RequestResponse(); req.getHTTPXml(url); } void getHTTPXml(URL url) throws ParserConfigurationException, IOException, SAXException { HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("ACCEPT", "application/xml"); InputStream xml = conn.getInputStream(); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); org.w3c.dom.Document document = builder.parse(xml); System.out.println(document); String doctype = conn.getContentType(); System.out.println(doctype); XPathFactory pathFactory = XPathFactory.newInstance(); XPath path = pathFactory.newXPath(); XPathExpression expression; try { expression = path.compile("/result/server/checks/check/checkid"); NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET); String checkids[] = getNodeValue(nodeList); for (String checkid : checkids) { System.out.print(checkid + ", "); } } catch (XPathExpressionException e) { e.printStackTrace(); } conn.disconnect(); } String[] getNodeValue(NodeList nodes) { String checkIds[] = new String[nodes.getLength()]; for (int i = 0; i &lt; nodes.getLength(); i++) { Node node = nodes.item(i); checkIds[i] = node.getTextContent(); } return checkIds; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload