[英]Get specific XML tag text using Java in Android app
I've been trying to use XmlResourceParser but I don't feel it's the right tool for the job. 我一直在尝试使用XmlResourceParser,但我不认为这是完成这项工作的正确工具。 I have a set of items that have subitems and I want to pull a specific item out, such as the second item in this list:
我有一组包含子项的项目,我想拉出特定的项目,例如此列表中的第二项:
<story>
<id>1</id>
<name>The First Room</name>
<description>You aren't sure how you ended up here, but there is nothing in this room of interest. You should probably escape.</description>
<direction>
<name>North</name>
<id>2</id>
</direction>
<direction>
<name>East</name>
<id>3</id>
</direction>
</story>
<story>
<id>2</id>
<name>Moldy Room</name>
<description>This room is filled with mold. It would be hazardous to your heath to stick around here.</description>
<direction>
<name>South</name>
<id>1</id>
</direction>
<direction>
<name>West</name>
<id>4</id>
</direction>
</story>
I'd like to be able to pull them by "id" number in short, WITHOUT having to set up my own objects. 我希望能够用“ id”数字来简单地拉出它们,而不必设置我自己的对象。 If possible.
如果可能的话。
You can use this code : 您可以使用以下代码:
Resources res = activity.getResources();
XmlResourceParser xpp = res
.getXml(R.xml.myxml);
xpp.next();
int eventType = xpp.getEventType();
eventType = xpp.next();
eventType = xpp.next();
eventType = xpp.next();
short id = Short
.parseShort(xpp.getText());
Toast.makeText(
activity.getApplicationContext(),
"" + id, Toast.LENGTH_LONG)
.show();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.