Reputation: 4400
I have parsed XML many times but didn't come through this type of XML to parse.
I have parsed many time XML of the type :
<a>
<a1>abc</a1>
<a2>abc</a2>
<a3>abc</a3>
<a4>abc</a4>
</a>
But I don't have any knowledge about parsing xml of the below type :
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<orders>
<order id="1" xlink:href="http://192.168.1.9/prestashop/api/orders/1"/>
<order id="2" xlink:href="http://192.168.1.9/prestashop/api/orders/2"/>
</orders>
</prestashop>
Question : How would I get the URL mention in side the order element..?
Upvotes: 3
Views: 594
Reputation: 4400
I have search things and have got the way to parse the XMl
that i have stated above using XMLPullParser
.
I have posted the solution here hoping it may help someone like me.
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser xpp = factory.newPullParser();
xpp.setInput(new StringReader (RESPONSE_STRNG_HERE));
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT)
{
if(eventType == XmlPullParser.START_DOCUMENT)
{
System.out.println("Start document");
}
else if(eventType == XmlPullParser.END_DOCUMENT)
{
System.out.println("End document");
}
else if(eventType == XmlPullParser.START_TAG)
{
System.out.println("Start tag "+xpp.getName());
Log.e("Count ",""+xpp.getAttributeCount());
for(int ia=0;ia<xpp.getAttributeCount();ia++)
{
Log.e("Attribute Name ",xpp.getAttributeName(ia));
Log.e("Attribute Value ",xpp.getAttributeValue(ia));
}
}
else if(eventType == XmlPullParser.END_TAG)
{
System.out.println("End tag "+xpp.getName());
}
else if(eventType == XmlPullParser.TEXT)
{
Log.e("Text ",xpp.getText());
}
eventType = xpp.next();
}
Upvotes: 0
Reputation: 157487
you are using SaxParser
you have to subclass DefaultHandler and, inside
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("order")) {
String href = attributes.getValue("xlink:href");
}
}
Upvotes: 1