I am having trouble in parsing my XML, I have tried many things.
I have xml like this.
<question id="1" text="Zodiac Sign" >
<option id="1" >Aries</option>
<option id="2" >Taurus</option>
<option id="3" >Gemini</option>
<option id="4" >Cancer</option>
<option id="5" >Leo</option>
<option id="6" >Virgo</option>
<option id="7" >Libra</option>
<option id="8" >Scarpio</option>
<option id="9" >Sagitarius</option>
<option id="10" >Capricorn</option>
<option id="11" >Acqauarius</option>
</question><question id="2" text="Home Town" >
</question><question id="3" text="Current Locaion" >
</question><question id="4" text="Nationality" ></question>
<question id="5" text="Height" ></question>
<question id="6" text="Build(optional)" >
<option id="13" >Slim</option>
<option id="14" >Athletic</option>
<option id="15" >Few Pounds Extra</option>
<option id="16" >Obese</option>
</question><question id="7" text="Hair Color" >
</question><question id="8" text="Color Of Eyes" >
<option id="23" >Black</option>
<option id="24" >Brown</option>
<option id="25" >Blue</option>
</question><question id="9" text="Smoking Habits" >
</question>
I need to get questions and I want to save them in an array and then there are some questions which have options as child tag, I want to add those options in another array with the reference to that question id.
How can I actually parse this xml.?
I have been trying to do this using XMLPullParser
Here is my code, but its not working.
StringBuilder sb = new StringBuilder();
int depth = 1;
while (depth != 0) {
switch (parser.next()) {
case XmlPullParser.END_TAG:
depth--;
if (depth > 0) {
sb.append("</" + parser.getName() + ">");
}
break;
case XmlPullParser.START_TAG:
depth++;
StringBuilder attrs = new StringBuilder();
for (int i = 0; i < parser.getAttributeCount(); i++) {
attrs.append(parser.getAttributeName(i) + "=\""
+ parser.getAttributeValue(i) + "\" ");
}
sb.append("<" + parser.getName() + " " + attrs.toString() + ">");
break;
default:
sb.append(parser.getText());
break;
}
}
payload = sb.toString();
I have built my own parser for parsing this xml.
Here is the code