开发者

Android XML Reading

I have xml file under res/xml/hi.xml when i use the below code it reads only the id element but i want to read the content of the xml tag not the id value

while (eventType != XmlPullParser.END_DOCUMENT) //Keep going until end of xml document
{
  if (eventType == XmlPullParser.START_DOCUMENT)
  {
    Log.d("abhayxxxxx", myxml.getName() + "start document reading");
    //Start of XML, can check this with myxml.getName() in Log, see if 开发者_运维问答your xml has read successfully
  }
  else if (eventType == XmlPullParser.START_TAG)
  {
    NodeValue = myxml.getName();
    Log.d("abhayxxxx", NodeValue + " node ");
    //Start of a Node
    if (NodeValue.equalsIgnoreCase("author"))
    {
      Log.d("abhayxxxx", "Reading data" + myxml.getAttributeValue(0));
    }
  }
  else if (eventType == XmlPullParser.END_TAG)
  {
    //End of document
  }
  else if (eventType == XmlPullParser.TEXT)
  {
    //Any XML text
  }

  try
  {
    eventType = myxml.next();
  }
  catch (XmlPullParserException e)
  {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
  catch (IOException e)
  {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }
}

here is the xml file's coontent

<?xml version="1.0"?>
<catalog>
    <book code="bk101">
        <author id="Gambardella, Matthew">content</author>
    </book>
    <book code="bk102">
        <author id="Ralls, Kim">content</author>
    </book>
</catalog>

Thanks


I think problem is here in getAttributeValue() function:

if (NodeValue.equalsIgnoreCase("author"))
        {

               Log.d("abhayxxxx","Reading data" + myxml.getAttributeValue(0));

        }

As you are using getAttributeValue(0), it will return the value of attribute of a tag, not the node value as you want. To get the node value use nextText() function.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜