1
属性に基づいてプログラムで適切なノードを選択しようとしている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要素のname属性とstate属性に基づいてタイトルテキストを返します。名前が指定されたテキストと等しい場合、タイトルが返されます。私がAlgo1として名前をつけ、1.1と言ったら、正しいタイトルを得る。私がAlgo1という名前をつけて1.2と書いたら、私は空白になります。デバッグすると、Algo1、state1.1が渡されますが、パーサーは2番目の部分をスキップし、Algo1、状態1.2のタイトル要素に直接ジャンプしているように見えます。私は起こっていることを理解してください。 ありがとうございます。