Ismail MAC
Ismail MAC

Reputation: 61

xmlns : android parsing

I am trying to parse below XML data. I need only d:MessageId and d:SerialNumber.

<feed xmlns="http://www.w3.org/2005/Atom" ...>
<script id="tinyhippos-injected"/>
    <id>...</id>
    <title type="text">ViewError</title>
    <updated>2015-08-25T01:41:55Z</updated>
    <author>...</author>
    <link href="EDI_ViewError" rel="self" title="EDI_ViewError"/>
    <entry>...</entry>
    <entry>...</entry>
    <entry>
        <id>
            serveraddress
        </id>
        <title type="text">ViewError('ABCDEFG')</title>
        <updated>2015-08-25T01:41:55Z</updated>
        <category term="ViewError" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme"/>
        <link href="ViewError('GSTU7417706')" rel="edit" title="ViewError"/>
        <content type="application/xml">
            <m:properties   xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
                <d:MessageId>msgId_1</d:MessageId>
                <d:SerialNumber>sernr_1</d:SerialNumber>
            </m:properties>
        </content>
    </entry>
    </feed>

How can I achieve this? My android code is below.

HTTP Part:

HttpClient httpclient = new DefaultHttpClient();
String user = "username";
String pwd = "pass";
HttpGet httpGet = new     HttpGet("properhttplink");
httpGet.addHeader("Auth Header");
HttpResponse response = httpclient.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();

Parser Part:

XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myParser = xmlFactoryObject.newPullParser();
myParser.setInput(is, null);
String dee;
int event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT)
{
    String name=myParser.getName();
    switch (event){
        case XmlPullParser.START_TAG:
            break;
        case XmlPullParser.TEXT:
            break;
        case XmlPullParser.END_TAG:
            if(name.equals("d")){
                dee =  myParser.getAttributeValue(null,"value");
            }
            break;
    }
    event = myParser.next();
}

Please note that myParser.getEventType() always returns "0" and myParser.getName() always returns "null".

Upvotes: 1

Views: 127

Answers (1)

Karakuri
Karakuri

Reputation: 38595

First: The name of the tag is MessageId, not d. d is the namespace of the tag; namespaces are used to distinguish between tags that have the same name but different meanings. If there are no other uses for MessageId in your document (i.e. you will never see it with any other namespace elsewhere), then you can effectively ignore it.

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

or, if the namespace really does matter

if ("d".equals(parser.getNamespace()) && "MessageId".equals(parser.getName())) {

Second: Your code uses getAttributeValue(), but the content you are trying to read is text content of the tag, not an attribute of the tag. You should be using getText() (if the current event is TEXT) or nextText() (if the current event is START_TAG). Which leads us to...

Third: You can't read the text from the END_TAG event.

Here's where I would start for your parser code:

XmlPullParserFactory xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser parser = xmlFactoryObject.newPullParser();
parser.setInput(is, null);
String dee;
int event;
while ((event = parser.next()) != XmlPullParser.END_DOCUMENT) {
    switch (event) {
        case XmlPullParser.START_TAG:
            if ("MessageId".equals(parser.getName()) {
                dee = parser.nextText();
            }
            break;

        // TODO: other event types, if you care about them

    } // end switch
} // end while

Upvotes: 1

Related Questions