我有关于XML的数据,其中应用程序试图根据属性以编程方式选择适当的节点。
这是XML文件
<?xml version="1.0" encoding="utf-8"?>
<CoagAlgo>
<AlgoName
name="Algo1"
state="1.1"
references="test1">
<titletext>Title1</titletext>
<optText
selectState="1.2">opt1</optText>
<optText
selectState="1.3">opt2</optText>
</AlgoName>
<AlgoName
name="Algo1"
state="1.2"
references="text2">
<titletext>Title2</titletext>
<optText
selectState="1.2.1">opt12</optText>
<optText
selectState="1.2.2">opt13</optText>
</AlgoName>
<AlgoName
name="Algo2"
state="1.1"
references="text2">
<titletext>Title21</titletext>
<optText
selectstate="2.1">opt21</optText>
<optText
selectstate="2.2">opt22</optText>
</AlgoName>
</CoagAlgo>
以下是代码的一部分;
public class MainActivity extends AppCompatActivity {
public static String algoName1="Algo1";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String stringXMLContect = null;
try {
XmlPullParser parser=(XmlPullParser)xmlParserIni();
stringXMLContect= getTitle(parser,algoName1,"1.2");
}catch (XmlPullParserException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
TextView myText=(TextView)findViewById(R.id.textView);
myText.setText(stringXMLContect);
RadioGroup radioGroup=(RadioGroup)findViewById(R.id.mainGroup);
ArrayList<String> optChoices=new ArrayList<String>();
try{
XmlPullParser parser=(XmlPullParser)xmlParserIni();
optChoices=optTextReader(parser,algoName1,"1.1");
}catch (XmlPullParserException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
for (int i=0;i<optChoices.size();i++){
RadioButton radioButton=new RadioButton(this);
radioButton.setText(optChoices.get(i));
radioGroup.addView(radioButton);
}
}
private XmlPullParser xmlParserIni() throws XmlPullParserException, IOException {
Context context = this.getApplicationContext();
XmlPullParser xmlResourceParser = context.getResources().getXml(R.xml.optchoices);
xmlResourceParser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES,true);
return xmlResourceParser;
}
private String getTitle(XmlPullParser xmlResourceParser, String algoName,String algoState)throws XmlPullParserException,IOException{
String attrValue = "";
int eventType=0;
while (xmlResourceParser.next()!= XmlPullParser.END_DOCUMENT){
eventType=xmlResourceParser.getEventType();
String algoRootName=xmlResourceParser.getName();
switch (eventType){
case XmlPullParser.START_TAG:
if(algoRootName!= null && algoRootName.equalsIgnoreCase("AlgoName")){
String algoCompName=xmlResourceParser.getAttributeValue(null,"name");
String algoCompState=xmlResourceParser.getAttributeValue(null,"state");
if(algoCompName!=null && algoCompName.equalsIgnoreCase(algoName)&& algoCompState!=null && algoCompState.equalsIgnoreCase(algoState)){
xmlResourceParser.next();
if (xmlResourceParser.next() == XmlPullParser.TEXT) {
attrValue = xmlResourceParser.getText();
}
}
}
break;
}
xmlResourceParser.next();
}
return attrValue;
}
这应该根据每个AlgoName元素的名称和状态属性返回标题文本。如果名称等于给定文本,则应返回标题。如果我将名字命名为Algo1并声明为1.1,我会得到正确的标题。如果我将名字命名为Algo1并将其命名为1.2,我就会空白。当我调试时,我可以看到Algo1,state1.1被传递给但是解析器似乎跳过第二部分并且直接转到Algo1的title元素,状态1.2。请帮我理解发生了什么。 提前谢谢。
答案 0 :(得分:1)
删除行'xmlResourceParser.next();'文件末尾4行。