Android的 - XML解析 - 工作的仿真器,但不是在设备
在我的应用我解析XML,一块结构做题:Android的 - XML解析 - 工作的仿真器,但不是在设备
<answers>
<answer value="A">A</answer>
<answer value="B">B</answer>
<answer value="C">C</answer>
</answers>
我与XML DOM解析它:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
那伟大工程,并根据答案的项目我创建一个单选按钮这样的:
NodeList answers = doc.getElementsByTagName("answers").item(0).getChildNodes();
int j = 0;
RadioGroup group = new RadioGroup(this);
RadioButton button1 = new RadioButton(this);
button1.setId((i+1)*100+(j++));
button1.setText(answers.item(1).getChildNodes().item(0).getNodeValue());
button1.setTextColor(Color.BLACK);
RadioButton button2 = new RadioButton(this);
button2.setId((i+1)*100+(j++));
button2.setText(answers.item(2).getChildNodes().item(0).getNodeValue());
button2.setTextColor(Color.BLACK);
RadioButton button3 = new RadioButton(this);
button3.setId((i+1)*100+(j));
button3.setText(answers.item(3).getChildNodes().item(0).getNodeValue());
button3.setTextColor(Color.BLACK);
这段代码工作PE我的HTC Desire在Android 2.1u1上运行(所以SDK v.8)
但是在设备中,我在这条线上得到了错误button2.setText(answers.item(2).getChildNodes().item(0).getNodeValue());
猜测没有这个模拟器的SDK v.7(Android 2.0)在回答.item(2)
- 但它必须是...我调试仿真器中运行此代码,并发现answers.item(0)
是包含XML节点的“答案”的名称TextNode
...
但它是真实的我有点困惑,解析这个XML时一切都搞乱了,因为我仍然需要计算我在什么时候调用哪个索引以及何时调用哪个元素(节点)......但是我仍然发现这个实现比使用SAX
简单得多。 ..
是不是有什么simillar到SimpleXml
PHP中的Java????
无论如何,我的主要问题是:它怎么可能是应用程序在模拟器中完美工作,而在设备上它会抛出NullPointerException
在我尝试设置button2文本的行?
非常感谢您的帮助!
getChildNodes()返回所有Node
s的答案,而不仅仅是所有的Element
s。你可能想通过所有的孩子进行迭代,并检查各自为Element
与标签名“答案”
怎么是这样的:
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
}
这样,你不依赖于特定的答案的子元素数目,并且您保证您不会尝试访问不存在的元素。
如果你真的想要它基于一个答案节点的孩子做,
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.
}
}
“这样你就不依赖特定数量的答案子元素,并且保证你不会尝试访问不存在的元素。” - 这是真的,我知道这个解决方案,但每个答案元素中总是只有三个答案元素,所以我知道只需要创建三个RadioButton。无论如何,感谢您的帮助,特别是检查标签名称的这一部分 - 我从未想到这一点......我以不同的方式解决了问题,问题主要在于PHP脚本中...... (在下一条评论中继续) – shadyyx 2011-05-06 12:35:48
...生成XML - 我正在使用'\ n'分隔在仿真器**中生成多个包含'\ n \ n \ n \ n' **的TextImpl节点的行, (在某种程度上)不存在于真正的设备中......从PHP脚本中删除每个'\ n'后,解析在模拟器中的工作方式与在真实设备中的工作方式相同......我为该帖子创建了+1,但不能将其标记为答案,因为我在其他地方以不同的方式想出了答案......但谢谢你! – shadyyx 2011-05-06 12:39:02
听起来很脆弱。空格不应该影响xml的解析方式。即使你总是有3个单选按钮,它仍然更清晰地循环,而不是假设它们存在于某个索引处。 – 2011-05-06 17:31:54
使用(简单XML)http://simple.sourceforge.net/]
@Root
public class Answers {
@ElementMap(entry="answer", key="value" attribute="true" inline=true)
public Map<String, String> map;
}
或者如果你只有三个条目你可以做。
@Root
public class Answers {
@Path("answer[1]")
@Text
private answerValue1;
@Path("answer[2]")
@Text
private answerValue2;
@Path("answer[3]")
@Text
private answerValue3;
@Path("answer[1]")
@Attribute(name="value")
private answerAttribute1;
@Path("answer[2]")
@Attribute(name="value")
private answerAttribute2;
@Path("answer[3]")
@Attribute(name="value")
private answerAttribute3;
}
然后阅读像这样。
Persister persister = new Persister();
Answers answers = persister.read(Answers.class, xml);
就这么简单!
呵呵,当我直接在devicem上输出setText()的值,'button1.setText(answers.item(1).getChildNodes()。item(0)。getNodeValue());'在没有错误的情况下处理,并且在下一个项目'button2.setText(answers.item(2).getChildNodes()。item(0).getNodeValue());'时抛出错误。因此,我完全不理解它... – shadyyx 2011-05-05 17:42:43
我的建议:停止使用文档构建器在Android中解析XML,而是使用简单库(http://simple.sourceforge.net/),这里是一篇博文(http://massaioli.homelinux.com/wordpress/2011/04/21/simple-xml-in-android-1-5-and-up/) – 2011-05-06 00:52:00
@RobertMassaioli:嗯,我看着它,它看起来不错,但仍然不是很简单 - 你必须在XML中为每个“对象”编写和定义一个类(所以对于每个TAG) - 简化的地方在哪里?是的,也许当解析XML时,它更简单,但是你必须生成更多的代码两次...但是 - 当还剩一段时间时,我会试一试... – shadyyx 2011-05-06 11:20:23