Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing XML using java DOM
    primarykey
    data
    text
    <p>Im new in Java, and i have a task to Parse one xml file using http with current url <a href="http://belbooner.site40.net/testXmls/details.xml" rel="nofollow">http://belbooner.site40.net/testXmls/details.xml</a></p> <p>I created Some class to parse it using Dom method, but im having java.lang.NullPointerException while trying to get one Nodes value So here's the code</p> <pre><code>import java.security.KeyStore.Builder; import java.util.*; import java.io.*; import java.net.*; import javax.swing.text.Document; import javax.xml.*; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.w3c.dom.*; import org.w3c.dom.CharacterData; import org.xml.sax.InputSource; 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 { //URL url = new URL("http://belbooner.site40.net/testXmls/details.xml"); 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.print(doctype); NodeList root = document.getChildNodes(); Node server = getNodes("server",root); Node check = getNodes("check", server.getChildNodes()); NodeList nodes = check.getChildNodes(); String checkid= getNodeValue("checkid", nodes); System.out.println(checkid); conn.disconnect(); //return (Document) DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(xml); } Node getNodes(String tagName, NodeList nodes) { for(int i=0; i&lt; nodes.getLength();i++) { Node node= nodes.item(i); if(node.getNodeName().equalsIgnoreCase(tagName)) { return node; } } return null; } String getNodeValue(String tagName, NodeList nodes ) { for ( int i = 0; i &lt; nodes.getLength(); i++ ) { Node node = nodes.item(i); if (node.getNodeName().equalsIgnoreCase(tagName)) { NodeList childNodes = node.getChildNodes(); for (int y = 0; y &lt; childNodes.getLength(); y++ ) { Node data = childNodes.item(y); if ( data.getNodeType() == Node.TEXT_NODE ) { return data.getNodeValue(); } if(data instanceof CharacterData) { CharacterData cd= (CharacterData) data; return cd.getData(); } } } } return ""; } } </code></pre> <p>The stacktrace I'm getting is the following:</p> <pre><code>application/xmlException in thread "main" java.lang.NullPointerException at RequestResponse.getHTTPXml(RequestResponse.java:45) at RequestResponse.main(RequestResponse.java:22) </code></pre> <p>After changin <code>Node server = getNodes("server",root);</code> to `</p> <pre><code> Node resultNode = getNodes("result", root); Node server = getNodes("server", resultNode.getChildNodes());` `application/xmlException in thread "main" java.lang.NullPointerException at RequestResponse.getHTTPXml(RequestResponse.java:49) at RequestResponse.main(RequestResponse.java:22) ` </code></pre> <p>Please help me to find the issue. </p>
    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.
 

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