2

for example I have xml file:

 <names>
        <item value="Hello" />
        <item value="All" />
        <item value="This" />
        <item value="Data" />
 </names>

So I need to get all raw data from the given attribute (names):

    <item value="Hello" />
    <item value="All" />
    <item value="This" />
    <item value="Data" />

In a string format, so the data should be:

String data = "
<item value="Hello" />
<item value="All" />
<item value="This" />
<item value="Data" />
";

and I have a peace of code:

 int state = 0;
    do {
        try {
            state = parser.next();
        } catch (XmlPullParserException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }       
        if (state == XmlPullParser.START_TAG) {
            if (parser.getName().equals("names")) {

// here I need to get the data
// String data = ...

               break;
            }
        }
    } while(state != XmlPullParser.END_DOCUMENT);

so, how do I get data from xml element in string format?

2 Answers 2

3

I used DOM2XmlPullBuilder to convert XmlPullParser to a DOM, then convert the DOM to a string. The code looks something like this.

DOM2XmlPullBuilder dom2XmlPullBuilder = new DOM2XmlPullBuilder();
Element element = dom2XmlPullBuilder.parseSubTree(xmlPullParser);
convertNodeToString(element); // implement using a Transformer
Sign up to request clarification or add additional context in comments.

Comments

1

I believe instead of where you have

if (parser.getName().equals("names")) {...

it should be something like this:

ArrayList<String> data=new ArrayList<String>();
....
....
if (parser.getName().equals("item")) {
   data.add(xpp.getAttributeValue(0));
}

I'm a beginner so I was just searching this information recently, luckily I found a helpful sample from commonsguy (which copied and ran fine without any change) - https://github.com/commonsguy/cw-android/tree/master/Resources/XML

Comments

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.