0

I am trying to read an xml file where structure is like this

<root>
    <entry>
        <key1 value="value for key 1.1"/>
        <key2 value="value for key 2.1"/>
        <key3 value="value for key 3.1"/>
    </entry>

    <entry>
        <key1 value="value for key 1.2"/>
        <key2 value="value for key 2.2"/>
        <key3 value="value for key 3.2"/>
    </entry>

    <entry>
        <key1 value="value for key 1.3"/>
        <key2 value="value for key 2.3"/>
        <key3 value="value for key 3.3"/>
    </entry>

</root>

I tried to do similar as this SO answer https://stackoverflow.com/a/54015781/19536031 but end up getting null value when the given example is a single entry xml field (with 1 tag, while mine has many tag). My code:

InputStream xmlStream = assetManager.open("file.xml");
            try {
                XmlPullParser xmlParser = Xml.newPullParser();
                xmlParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
                xmlParser.setInput(xmlStream, null);
                xmlParser.nextTag();

                int imgNum = 0;
                xmlParser.require(XmlPullParser.START_TAG, null, "root");
                while (xmlParser.next() != XmlPullParser.END_TAG) {
                    if (xmlParser.getEventType() != XmlPullParser.START_TAG) {
                        continue;
                    }

                    xmlParser.require(XmlPullParser.START_TAG, null, "entry");
                    String value1 = xmlParser.getAttributeValue(null, "key1");
                    Log.d("TAG", value1.toString());
                    xmlParser.nextTag();
                    xmlParser.require(XmlPullParser.END_TAG, null, "entry");
                }



            }

How can I modify the code to read through a tag with many <entry> tag? How to browser deeper in case there are nested tag in key like

<entry>
        <key1 value="value for key 1.3"/>
        <key2 value="value for key 2.3"/>
        <key3>
            <sub-key>some value</sub-key>
            <sub-key2>some other value</sub-key2>
        </key3>
    </entry>
rachelbrl
  • 1
  • 1

0 Answers0