0

i have xml string which i need to format and capture the data.

I tried a way but it gives me the following exception :

org.xmlpull.v1.XmlPullParserException: Unexpected token (position:TEXT {"data":"\u003c?...@1:538 in java.io.InputStreamReader@2af1e959)
    at org.kxml2.io.KXmlParser.next(KXmlParser.java:432)

Method used for parsing is :

public void parseXml(String aadharResponse) throws XmlPullParserException {
        try {

            XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();
            InputStream inputStream = new ByteArrayInputStream(aadharResponse.getBytes(Charset.forName("UTF-8")));
            xpp.setInput(inputStream,"UTF-8"); // pass input whatever xml you have
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                    Util.printMessage(TAG, "Start document");
                } else if (eventType == XmlPullParser.START_TAG) {
                    Util.printMessage(TAG, "Start tag " + xpp.getName());
                } else if (eventType == XmlPullParser.END_TAG) {
                    Util.printMessage(TAG, "End tag " + xpp.getName());
                } else if (eventType == XmlPullParser.TEXT) {
                    Util.printMessage(TAG, "Text " + xpp.getText()); // here you get the text from xml
                }
                eventType = xpp.next();
            }
            Util.printMessage(TAG, "End document");

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

Can anyone suggest what's wrong in this?

Selvin
  • 6,598
  • 3
  • 37
  • 43
Siddhesh Pawar
  • 259
  • 4
  • 24
  • first of all you need to check that your xml response (aadharResponse.getBytes(Charset.forName("UTF-8")) is valid or not! – Radhey Apr 10 '18 at 07:31

2 Answers2

0

Might be duplicated of this post. In which, it suggest to move the .xml file from the res/xml folder to the assets folder. And then acquire the InputStream in this manner.

InputStream in = this.getAssets().open("sample.xml");
Pai-Hsiang Huang
  • 342
  • 3
  • 12
0

make one xml file in asset folder like below.. country.xml

    <?xml version="1.0" encoding="utf-8"?>

<country id="1">
    <name>
        India
    </name>
    <capital>
        New Delhi
    </capital>
</country>

<country id="2">
    <name>
        Australia
    </name>
    <capital>
        Canberra
    </capital>
</country>

<country id="3">
    <name>
        USA
    </name>
    <capital>
        Washington, D.C.
    </capital>
</country>

then after used below code for parsing like ..

public class MainActivity extends AppCompatActivity {


TextView textView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    textView= (TextView)findViewById(R.id.text);

    XmlPullParserFactory pullParserFactory;

    try {
        pullParserFactory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = pullParserFactory.newPullParser();

        InputStream in_s = getApplicationContext().getAssets().open("sample.xml");
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
        parser.setInput(in_s, null);

        ArrayList<Country> countries=  parseXML(parser);

        String text="";

        for(Country country:countries)
        {

            text+= "id : "+country.getId()+" name : "+country.getName()+" capital : "+country.getCapital()+"\n";
        }

        textView.setText(text);



    } catch (XmlPullParserException e) {

        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

private ArrayList<Country> parseXML(XmlPullParser parser) throws XmlPullParserException,IOException
{
    ArrayList<Country> countries = null;
    int eventType = parser.getEventType();
    Country country = null;

    while (eventType != XmlPullParser.END_DOCUMENT){
        String name;
        switch (eventType){
            case XmlPullParser.START_DOCUMENT:
                countries = new ArrayList();
                break;
            case XmlPullParser.START_TAG:
                name = parser.getName();
                if (name.equals("country")){
                    country = new Country();
                    country.id=parser.getAttributeValue(null,"id");
                } else if (country != null){
                    if (name.equals("name")){
                        country.name = parser.nextText();
                    } else if (name.equals("capital")){
                        country.capital = parser.nextText();
                    }
                }
                break;
            case XmlPullParser.END_TAG:
                name = parser.getName();
                if (name.equalsIgnoreCase("country") && country != null){
                    countries.add(country);
                }
                }
        eventType = parser.next();
    }

    return countries;

    }

}