我下RES/XML/hi.xml xml文件,當我使用下面的代碼讀取僅id元素,但我想讀取XML標記不id值的內容Android的XML閱讀
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();
}
}
這裏是xml文件的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>
感謝
我沒有答案,因爲我從來沒有在android上使用XML解析器。嘗試使用JSON。在基於java的應用程序中使用起來要容易得多。它還具有更小,更易於閱讀的益處。 – Nallath 2011-06-03 11:59:40
感謝您的建議:) – 2011-06-03 12:02:10
除非您在限制其禁止使用的情況下工作,否則我強烈建議您使用XML序列化/反序列化API,如XStream http://xstream.codehaus.org,而不是手動解析XML。 – 2011-06-03 12:31:25