-1

i have been trying to parse a xml which look like this. is this correct format of xml is so how to parse it. And i want to know what is the format of xml.

<string xmlns="http://schemas.microsoft.com/2003/10/Serialization/">1|3|195,317,65,108|342,227,66,99|280,98,84,112</string>

and my xml parser activity is

public class RetrieveFeed extends AsyncTask {

URL url;
ArrayList<String> headlines = new ArrayList();
ArrayList<String> links = new ArrayList();
@Override
protected Object doInBackground(Object[] objects) {
    // Initializing instance variables


    try {
        url = new URL("http://54.179.134.139/viViewapi/api/values");

        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(false);
        XmlPullParser xpp = factory.newPullParser();

        // We will get the XML from an input stream
        xpp.setInput(getInputStream(url), "UTF_8");


        boolean insideItem = false;

        // Returns the type of current event: START_TAG, END_TAG, etc..
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if (eventType == XmlPullParser.START_TAG) {

                if (xpp.getName().equalsIgnoreCase("html")) {
                    insideItem = true;
                } else if (xpp.getName().equalsIgnoreCase("pre")) {
                    if (insideItem)
                        headlines.add(xpp.nextText()); //extract the headline
                }
                /*else if (xpp.getName().equalsIgnoreCase("link")) {
                    if (insideItem)
                        links.add(xpp.nextText()); //extract the link of article
                }*/
            } else if (eventType == XmlPullParser.END_TAG && xpp.getName().equalsIgnoreCase("html")) {
                insideItem = false;
            }

            eventType = xpp.next(); //move to next element
        }

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    return headlines;
}


public InputStream getInputStream(URL url) {
    try {
        return url.openConnection().getInputStream();
    } catch (IOException e) {
        return null;
    }
}

public ArrayList<String> heads()
{
    return headlines;
}

}

i don't know whether the approach is right or wrong

  • Your title says that you are having issues but you don't say what the issues are. The body of your question says "I want to know what is the format of xml", but that's a totally inappropriate question for SO - there's plenty of reference material that explains the format of XML. – Michael Kay Apr 26 '17 at 14:51

1 Answers1

0

XML like JSON needs at least one root element. In your example you do have a root element called "string" so no problems here.

Check out this XML Syntax Guide for a quick XML tutorial. Note that "string" can be used as it is not a reserved word (see XML Reserved Words).

The XML snippet you posted is valid XML. You can validate XML using this XML Validator.

You don't necessarily need the xmlns property (namespace) here so it could become:

<string>1|3|195,317,65,108|342,227,66,99|280,98,84,112</string>

This XML is just describing a single string. If you wanted to describe an object with a string property you would have something like this as the XML:

<root>
  <string>1|3|195,317,65,108|342,227,66,99|280,98,84,112</string>
</root>

If you want to know more about reading the values from the XML then check out this previous simple xml parsing Stack Overflow answer.

Community
  • 1
  • 1