1

I don't know why, but Log.i(TAG, "found: "+found); returns null for "found" element in my XML, that I get in a stream. I have debugged and it seems that my root element of XML is read, but elements of that root element are not. urlString is also correct. document is parsed without any errors and exceptions. But in this lines

NodeList nl = docEle.getElementsByTagName("found");
        found = nl.item(0).getNodeValue();

found is null. This is the full code of a method:

protected Void doInBackground(String... urls) {

        String urlString = urls[0];
        URL documentUrl = null;
        InputStream stream = null;
        URLConnection conn = null;
        DocumentBuilder builder = null;
        Document document = null;
        try {
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            builder = factory.newDocumentBuilder();

            documentUrl = new URL(urlString);
            conn = documentUrl.openConnection();
            stream = conn.getInputStream();
            document = builder.parse(stream);
        } 
        catch (IOException e) {
            Error = e.getMessage();
            Log.i(TAG, "Exception!", e);
        } 
        catch (SAXException e) {
            Error = e.getMessage();
            Log.i(TAG, "Exception!", e);
        }
        catch (ParserConfigurationException e) {
            Error = e.getMessage();
            Log.i(TAG, "Exception!", e);
        }
        finally 
        {
            if (stream != null)
            {
                try {
                    stream.close();
                } catch (IOException e) {
                    Error = e.getMessage();
                    Log.i(TAG, "Exception!", e);
                }
            }
        }

        /*NodeList nodes = document.getElementsByTagName("found");
        for (int i = 0; i < nodes.getLength(); i++) {
            found = nodes.item(i).getNodeValue();
            //System.out.println(found);
            Log.i(TAG, "found: "+found);
        }*/

        //get the root element
        Element docEle = document.getDocumentElement();
        NodeList nl = docEle.getElementsByTagName("found");
        found = nl.item(0).getNodeValue();
        Log.i(TAG, "found: "+found);

        return null;
    }

This is my urlString with XML: http://basa.med-info.ru/xse/index.php?query=%E3%F0%E8%EF%EF&nres=20

found must be equal to 20.

1 Answer 1

2

Use:

found = nl.item(0).getFirstChild().getNodeValue();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.