xml文件:remote_list.xml
内容:
<?xml version="1.0" encoding="UTF-8"?>
<remote_list brand="13" >
<remote id="LCD09" category="TV" >
<set format="UPD6124-D7C5_F_40K" >
<group name="key_code" bits="7" >
<field name="key_code" bits="0:6" >
<option name="IR_KEY_CHANNEL_DOWN" value="0x11" />
<option name="IR_KEY_CHANNEL_UP" value="0x10" />
<option name="IR_KEY_POWER_TOGGLE" value="0x15" />
<option name="IR_KEY_TV_AV" value="0x25" />
<option name="IR_KEY_VOLUME_DOWN" value="0x13" />
<option name="IR_KEY_VOLUME_UP" value="0x12" />
</field>
</group>
<group name="custom_code" bits="5" >
<field name="custom_code" bits="0:4" fixed="0x1" />
</group>
</set>
</remote>
</remote_list>
XmlPullMethod:请大神指教
public class XmlPullTest {
@SuppressLint("NewApi")
public static List<TestParams> getParams(InputStream input)throws Throwable {
List<TestParams> listParams = null;
TestParams testParams = null;
// TestParams items = null;
//xml工厂
XmlPullParserFactory parserFactory = XmlPullParserFactory.newInstance();
XmlPullParser parser = parserFactory.newPullParser();
//解析文件输入流
parser.setInput(input, "utf-8");
//产生第一个事件
int evenType = parser.getEventType();
//只要不是文档结束事件,就一直循环
while (evenType != XmlPullParser.END_DOCUMENT) {
switch (evenType) {
//开始触发文档事件
case XmlPullParser.START_DOCUMENT:
listParams = new ArrayList<TestParams>();
break;
//开始触发元素事件
case XmlPullParser.START_TAG:
//获取解析器当前指向的元素名称
String data = parser.getName();
Log.e("tag", data);
if ("remote_list".equals(data)) {
//通过解析器获取元素值,并设置和testParser相对的值
testParams = new TestParams();
testParams.setBrand(parser.getAttributeValue(0));
Log.e("remote_list", parser.getAttributeValue(0));
}
if (testParams != null) {
if ("remote".equals(data)) {
testParams.setId(parser.getAttributeValue(0));
Log.e("remote", parser.getAttributeValue(0));
testParams.setCategory(parser.getAttributeValue(1));
Log.e("remote", parser.getAttributeValue(1));
}else if ("set".equals(data)) {
testParams.setFormat(parser.getAttributeValue(0));
Log.e("set", parser.getAttributeValue(0));
}else if (data.equals("group")) {
testParams.setGroupName(parser.getAttributeValue(0));
Log.e("group", parser.getAttributeValue(0));
testParams.setGroupBits(parser.getAttributeValue(1));
Log.e("group", parser.getAttributeValue(1));
}else if (data.equals("field")) {
testParams.setFieldName(parser.getAttributeValue(0));
Log.e("field", parser.getAttributeValue(0));
testParams.setFieldBits(parser.getAttributeValue(1));
Log.e("field", parser.getAttributeValue(1));
if (parser.getAttributeCount() == 3) {
testParams.setFieldFixed(parser.getAttributeValue(2));
Log.e("field", parser.getAttributeValue(2));
// items = new TestParams();
// items = testParams;
// listParams.add(items);
// parser.next();
listParams.add(testParams);
}
}else if (data.equals("option")) {
testParams.setOptionName(parser.getAttributeValue(0));
Log.e("option", parser.getAttributeValue(0));
testParams.setOptionValue(parser.getAttributeValue(1));
Log.e("option", parser.getAttributeValue(1));
// items = new TestParams();
// items = testParams;
// listParams.add(items);
// parser.next();
listParams.add(testParams);
}
}
// listParams.add(testParams);
break;
//触发结束事件
case XmlPullParser.END_TAG:
if (parser.getName().equals("remote_list") && testParams != null) {
// listParams.add(testParams);
testParams = null;
}
break;default:
break;
}
evenType = parser.next();
}
return listParams;
}
}
问题已解决