Skip to content Skip to sidebar Skip to footer

How To Reading Xml From A Url In Android

I want to read a XML document from a URL: public void DownloadXmlFile() throws IOException{ //TODO String url = 'http://api.m1858.com/coursebook.xml'; URL u

Solution 1:

This is not an XML Problem its a Strict Mode Problem. You should'nt do time intensiv things in Gui Thread, do it in a own Thread.

However, you can disable it, but you shouldt ;) see here

Solution 2:

there are two step for read data from server...

1.Make a HTTP request to get the data from the webservice 2.Parse a XML document and read the contents

try 
    {

        URLurl=newURL("http://www.w3schools.com/xml/note.xml");

        DocumentBuilderFactorydbf= DocumentBuilderFactory.newInstance();
        DocumentBuilderdb= dbf.newDocumentBuilder();
        Documentdoc= db.parse(newInputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeListnodeList= doc.getElementsByTagName("note");
        /** Assign textview array lenght by arraylist size */


        to = newTextView[nodeList.getLength()];
        from = newTextView[nodeList.getLength()];
        heading = newTextView[nodeList.getLength()];
        body = newTextView[nodeList.getLength()];



        for (inti=0; i < nodeList.getLength(); i++) 
        {
            Nodenode= nodeList.item(i);

            to[i] = newTextView(this);
            from[i] = newTextView(this);
            body[i] = newTextView(this);
            heading[i] = newTextView(this);

            ElementfstElmnt= (Element) node;
            NodeListtoList= fstElmnt.getElementsByTagName("to");
            ElementnameElement= (Element) toList.item(0);
            toList = nameElement.getChildNodes();
            to[i].setText("To = "+ ((Node) toList.item(0)).getNodeValue());

            NodeListfromList= fstElmnt.getElementsByTagName("from");
            ElementfromElement= (Element) fromList.item(0);
            fromList = fromElement.getChildNodes();
            from[i].setText("from = "+ ((Node) fromList.item(0)).getNodeValue());

            NodeListheadingList= fstElmnt.getElementsByTagName("heading");
            ElementheadingElement= (Element) headingList.item(0);
            headingList = headingElement.getChildNodes();
            heading[i].setText("heading = "+ ((Node) headingList.item(0)).getNodeValue());


            NodeListbodyList= fstElmnt.getElementsByTagName("body");
            ElementbodyElement= (Element) bodyList.item(0);
            bodyList = bodyElement.getChildNodes();
            body[i].setText("body = "+ ((Node) bodyList.item(0)).getNodeValue());

            layout.addView(to[i]);
            layout.addView(from[i]);
            layout.addView(heading[i]);
            layout.addView(body[i]);

        }
    } 
    catch (Exception e) 
    {
        System.out.println("XML Pasing Excpetion = " + e);
    }

Solution 3:

Why you don't google or look for the error here on stackoverflow? It's full of answers...

You have to extend an AsyncTask to avoid the blocking of the GUI and do this kind of operation (as downloading or parsing stuff) in background.

Post a Comment for "How To Reading Xml From A Url In Android"