在Android中解析同名标记为null的XML
我在解析以下XML时遇到问题:在Android中解析同名标记为null的XML,android,xml,Android,Xml,我在解析以下XML时遇到问题: <key>key 1</key> <string>string 1</string> <key>key 2</key> <string>string 2</string> <key>key 3</key> <string>string 3</string> 键1 字符串1 关键2 字符串2 关键3 字符串3 我已经编写
<key>key 1</key>
<string>string 1</string>
<key>key 2</key>
<string>string 2</string>
<key>key 3</key>
<string>string 3</string>
键1
字符串1
关键2
字符串2
关键3
字符串3
我已经编写了以下代码,但我只获得TextView中第一个标记的数据,而不是标记string2和String3的数据。如何解析具有相同名称标记的XML
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Context mContext = this;
TextView tv = (TextView) findViewById(R.id.info);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
AssetManager assetManager = mContext.getAssets();
InputStream inputStream = assetManager.open("abc.xml");
Document doc=db.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("string");
for (int i = 0; i < nodeList.getLength(); i++) {
tv.setText( "\n\n"+ nodeList.item(i).getChildNodes().item(i).getNodeValue()+"\n");
}
} catch (Exception e) {
System.out.println("Pasing Excpetion = " + e);
}
}
公共类MainActivity扩展活动{
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
最终上下文mContext=此;
TextView tv=(TextView)findViewById(R.id.info);
试试{
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
AssetManager AssetManager=mContext.getAssets();
InputStream InputStream=assetManager.open(“abc.xml”);
文档doc=db.parse(inputStream);
doc.getDocumentElement().normalize();
NodeList NodeList=doc.getElementsByTagName(“字符串”);
对于(inti=0;i
可能是您从这个条件开始…更改您的条件从
for (int i = 0; i < nodeList.getLength(); i++) {
到
仍然只在第一个标记中获取数据。如何避免包含一些字符串标记?例如,如果我只想在标记键1、键2和键4之后包含字符串标记,而不想在标记键3和键5之后包含字符串标记。我会将键标记的值与字符串进行比较,如果条件满足,则附加下一个节点文本,否则如何忽略nex的数据t节点?请帮忙。
for (int i = 0; i < nodeList.getLength()-1; i++) {
tv.setText( "\n\n"+ nodeList.item(i).getChildNodes().item(i).getNodeValue()+"\n");
tv.setText( "\n\n"+ nodeList.item(i).getChildNodes().item(0).getNodeValue()+"\n");