在Android中将字符串解析为XML

时间:2018-04-10 06:59:27

标签: android xml-parsing

我有xml字符串,我需要格式化并捕获数据。

我尝试了一种方法,但它给了我以下例外:

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)

用于解析的方法是:

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();
        }
    }

有人能说出这个错误吗?

2 个答案:

答案 0 :(得分:0)

可能会与此post重复。 其中,它建议将.xml文件从res / xml文件夹移动到assets文件夹。然后以这种方式获取InputStream。

InputStream in = this.getAssets().open("sample.xml");

答案 1 :(得分:0)

在资产文件夹中制作一个xml文件,如下所示。 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>

然后使用下面的代码解析像..

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;

    }

}