0

Is there any working xml read via http tutorial or example?

I have a server which contain the next lines url: http://192.168.0.1/update.xml:

<?xml version='1.0' encoding='UTF-8'?>
<Version>1</Version>

And I would like to shown the "1" number to a TextView. How can I do it?

2 Answers 2

2

Here is a piece of code you can adapt to your desires :

To get the remote file content :

            HttpClient client = new DefaultHttpClient();
            HttpGet get = new HttpGet("my_url");        
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            try {               
                String response = client.execute(get,responseHandler);

            } catch (Exception e) {
                Log.e("RESPONSE", "is "+e.getMessage());                
            }

To parse the XML string :

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();
        xpp.setInput(new StringReader(response));
            String value = null;

        while(xpp.getEventType() !=XmlPullParser.END_DOCUMENT){ // loop from the beginning to the end of the XML document

            if(xpp.getEventType()==XmlPullParser.START_TAG){

                if(xpp.getName().equals("version")){
                    // start tag : <version>
                                    // do some stuff here, like preparing an 
                                    // object/variable to recieve the value "1" of the version tag
                }
            }
            else if(xpp.getEventType()==XmlPullParser.END_TAG){             

                       // ... end of the tag: </version> in our example             
                }
                    else if(xpp.getEventType()==XmlPullParser.TEXT){ // in a text node
                 value = xpp.getText(); // here you get the "1" value 
        }

        xpp.next(); // next XPP state
    }                  
Sign up to request clarification or add additional context in comments.

1 Comment

Ive got error for the response cause its not a variable this line: xpp.setInput(new StringReader(response));
0

This is not a particularly complicated task and it is covered in some detail here on the Android developer site.

2 Comments

But there arent a working example. Thats why I asked your help
If you could use that resource to attempt to write the code, then come back with a more specific question on where you are struggling, I might be able to help. As it stands, I would have to write an application for you. ;)

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.