我有这个 xml 文件:
<?xml version="1.0" encoding="UTF-8"?>
<Products>
<Product productName="testProduct1">
<Fields>
<Field name="Stack" />
<Field name="Overflow" />
</Fields>
<AnotherFields>
<Field name="Test" />
</AnotherFields>
</Product>
<Product productName="testProduct">
<Fields>
<Field name="StackOverflow" />
</Fields>
</Product>
</Products>
并且要读取所有product
具有专有属性值的子标签productName
,所有其他标签都想跳过。
这是我的java代码,我被卡住了:
public void mainParser(XmlResourceParser configXML, String productNameParameter)
throws XmlPullParserException, IOException {
int eventType = -1;
String strName, productName;
while (eventType != XmlResourceParser.END_DOCUMENT) {
if (eventType == XmlResourceParser.START_TAG) {
strName = configXML.getName();
if (strName.equals("Product")) {
if (eventType == XmlResourceParser.START_TAG) {
productName = configXML.getAttributeValue(null, "productName");
if (productName.equals(productNameParameter)) {
eventType = configXML.next();
//here is the problem
}
}
}
}
eventType = configXML.next();
}
}
任何人都可以帮忙吗?