Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-XML解析-在模拟器上工作,但不在设备上工作_Android_Xml_Dom_Xml Parsing - Fatal编程技术网

Android-XML解析-在模拟器上工作,但不在设备上工作

Android-XML解析-在模拟器上工作,但不在设备上工作,android,xml,dom,xml-parsing,Android,Xml,Dom,Xml Parsing,在我的应用程序中,我正在解析一个xml,这是一段结构,存在以下问题: A. B C 我正在用XML DOM解析它: DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance(); DocumentBuilder db=dbf.newDocumentBuilder(); Document doc=db.parse(新的输入源(url.openStream()); doc.getDocumentElement().normaliz

在我的应用程序中,我正在解析一个xml,这是一段结构,存在以下问题:


A.
B
C
我正在用XML DOM解析它:

DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
Document doc=db.parse(新的输入源(url.openStream());
doc.getDocumentElement().normalize();
这非常有效,根据答案项目,我正在创建一个单选按钮,如下所示:

NodeList answers = doc.getElementsByTagName("answer");
for (int x = 0; x < answers.getLength(); x++) {
  Node answer = answers.get(x);
  // you could do some checking here, make sure Node is instanceof Element, etc.
  RadioButton radioButton = new RadioButton(this);
  radioButton.setId((i+1)*100+(x));
  radioButton.setText(node.getNodeValue());
  radioButton.setTextColor(Color.BLACK);
  // add the radio button to some view
}
NodeList answers=doc.getElementsByTagName(“answers”).item(0.getChildNodes();
int j=0;
放射组组=新放射组(此);
RadioButton button1=新的RadioButton(本);
按钮1.setId((i+1)*100+(j++);
button1.setText(answers.item(1.getChildNodes().item(0.getNodeValue());
按钮1.setTextColor(颜色为黑色);
RadioButton button2=新的RadioButton(本);
按钮2.setId((i+1)*100+(j++);
button2.setText(answers.item(2.getChildNodes().item(0.getNodeValue());
按钮2.setTextColor(颜色为黑色);
RadioButton button3=新的RadioButton(本);
按钮3.设置ID((i+1)*100+(j));
button3.setText(answers.item(3.getChildNodes().item(0.getNodeValue());
按钮3.setTextColor(颜色为黑色);
这段代码在模拟器SDK v.7(Android 2.0)中运行得非常好,而我的HTC Desire在Android 2.1u1(so SDK v.8)上运行

但是在设备中,我在这一行
button2.setText(answers.item(2.getChildNodes().item(0.getNodeValue))上得到了错误猜测答案中没有
.item(2)
,但必须是。。。我在emulator中调试这段代码,发现
answers.item(0)
是一个
TextNode
包含XML节点“answers”的名称

但我确实有点困惑,在解析这个XML时,一切都搞砸了,因为我仍然需要计算我有多深,以及何时调用哪个元素(节点)上的什么索引。。。但是我仍然发现这个实现比使用SAX要简单得多

Java中的PHP中的
SimpleXml
难道没有类似的东西吗

不管怎么说,我的主要问题是:当应用程序在emulator上运行时,它在我试图为按钮2设置文本的行上抛出
NullPointerException
,这怎么可能呢

非常感谢你的帮助

getChildNodes()返回answers下的所有
节点
s,而不仅仅是所有
元素
s。您可能希望遍历所有子元素,并检查每个子元素是否是标记名为“answer”的
元素

像这样的怎么样:

NodeList answers = doc.getElementsByTagName("answer");
for (int x = 0; x < answers.getLength(); x++) {
  Node answer = answers.get(x);
  // you could do some checking here, make sure Node is instanceof Element, etc.
  RadioButton radioButton = new RadioButton(this);
  radioButton.setId((i+1)*100+(x));
  radioButton.setText(node.getNodeValue());
  radioButton.setTextColor(Color.BLACK);
  // add the radio button to some view
}
NodeList answers=doc.getElementsByTagName(“answer”);
对于(int x=0;x

这样,您就不依赖于答案的特定数量的子元素,并且您保证不会尝试访问不存在的子元素

如果确实希望基于Answers节点的子节点执行此操作

Node answersNode = // get the node
NodeList childNodes = answersNode.getChildNodes();
for (int x = 0; x < childNodes.getLength(); x++) {
  Node node = childNodes.get(0);
  if (node instanceof Element && node.getNodeName().equals("answer")) {
    // do the same as above to create a radio button.
  }
}
Node answersNode=//获取节点
NodeList childNodes=answersNode.getChildNodes();
对于(int x=0;x
使用(简单XML)[http://simple.sourceforge.net/]

@Root
public class Answers {

   @ElementMap(entry="answer", key="value" attribute="true" inline=true)
   public Map<String, String> map;
}
那就这样读吧

Persister persister = new Persister();
Answers answers = persister.read(Answers.class, xml);

就这么简单

哦,当我直接在设备上输出setText()值时,
button1.setText(answers.item(1.getChildNodes().item(0.getNodeValue))处理无误,并且在下一项
按钮2.setText(answers.item(2.getChildNodes().item(0.getNodeValue())时输出值抛出了一个错误。。。因此我一点也不懂…我的建议是:停止在Android中使用document builder解析XML,改用Simple Library(),这里有一篇关于它的博客文章()@RobertMassaioli:Hmm,我看了一下,它看起来不错,但仍然不是很简单-你必须为XML中的每个“对象”编写和定义一个类(因此对于每个标记)-简化在哪里???是的,也许在解析XML时更简单,但您必须生成两倍多的代码。。。但是-我会在还有时间的时候尝试一下…@shadyyx这不是真的,你不需要为每个标记都写一个类。看一看简单的路径注释,它允许您使用XPath表达式映射多个嵌套的XML元素和属性。“这样您就不依赖于特定数量的答案子元素,并且您保证不会尝试访问不存在的子元素。”-没错,我知道这个解决方案,但是在每个答案元素中总是只有三个答案元素,所以我知道只需要创建三个单选按钮。无论如何,谢谢你的帮助,特别是检查标签名称的部分-我从来没有想到这一点。。。我用不同的方式解决了这个问题,问题主要出在PHP脚本中。。。(在下一篇评论中继续)。。。生成XML-我正在使用
\n
来分隔生成另一个TextImpl节点的行,该节点在emulator中包含
\n\n\n
,但实际设备中不存在该节点。。。从PHP脚本中删除每个
\n
后,解析在emulator中的工作方式与在真实设备中的工作方式相同。。。我在这篇文章中加了+1,但无法将其标记为答案,因为我在其他地方发现了它,并且以不同的方式。。。但是谢谢你!!!那听起来很脆弱。。空白不应影响xml的解析方式。即使你总是有3个单选按钮,它仍然比t更干净