Android 1.5 和 dom4j - 属性名称解析不正确
我正在开发 Android 1.5 的应用程序。目前,我需要解析 XML 文件。由于 Android 在 API Level 3 中不原生支持 XPath,因此我决定使用 dom4j 和 jaxen 库来读取文件。
我有简单的 XML 文件 (test1.xml):
<?xml version="1.0" encoding="utf-8" ?>
<tests>
<resources base_lang="en">
<string base="example">
<localization lang="pl">Przykład</localization>
</string>
</resources>
<test name="main name">
<title>Main Title</title>
<description>Long description</description>
</test>
</tests>
和简单的代码:
SAXReader reader = new SAXReader();
Document doc = reader.read("file:///sdcard/tests/test1.xml");
Node node = doc.selectSingleNode("/tests/test");
Element elem = (Element)node;
for(int j=0;j<elem.attributeCount();++j) {
Attribute attr = elem.attribute(j);
Logger.getLogger(AutoTesterMain.class.getName()).log(Level.SEVERE, "ATTR: "+attr.getName()+"="+attr.getValue());
}
它读取 test1.xml 文件,找到“/tests/test”节点并枚举它的属性。当我将这段代码作为“桌面”java应用程序的一部分运行时(我对java术语并不深入),它显示了我所期望的内容(Fedora Linux下的OpenJDK 1.8.3):
SEVERE: ATTR: name=main name
不幸的是,在Android 1.5(模拟器ofc)中,完全相同的代码显示:
E/AutoTesterMain( 823): ATTR: base_lang=main name
如您所见,属性的值正常,但名称有问题。我完全不知道为什么在这个地方会出现来自完全其他 DOM 元素(“/tests/resources”)的属性名称。它似乎错误地解析了我的文件,所以我可能设置了错误......
当然,两个版本都使用完全相同的 .jar 和 dom4j 和 jaxen 库,所以这可能没问题。
这个问题比仅仅列出不正确的属性产生的影响更大。它还不允许我使用 XPath 正确读取属性 - 选择“/tests/test/@name”没有给出任何内容,而“/tests/test/@base_lang”给出了“main name”字符串。这显然是由与上面列出属性相同的错误引起的。
有人遇到过这个吗?我该如何解决它?
不幸的是,迁移到具有原生 XPath 支持的较新 Android 对我来说不是一个选择。
我试图在网上和这里找到任何东西,但没有运气。
I am working on application for Android 1.5 . Currently, I need to parse XML file. As Android does not support XPath natively in API Level 3, I decided to use dom4j and jaxen libraries to read a file.
I have simple XML file (test1.xml):
<?xml version="1.0" encoding="utf-8" ?>
<tests>
<resources base_lang="en">
<string base="example">
<localization lang="pl">Przykład</localization>
</string>
</resources>
<test name="main name">
<title>Main Title</title>
<description>Long description</description>
</test>
</tests>
And simple code:
SAXReader reader = new SAXReader();
Document doc = reader.read("file:///sdcard/tests/test1.xml");
Node node = doc.selectSingleNode("/tests/test");
Element elem = (Element)node;
for(int j=0;j<elem.attributeCount();++j) {
Attribute attr = elem.attribute(j);
Logger.getLogger(AutoTesterMain.class.getName()).log(Level.SEVERE, "ATTR: "+attr.getName()+"="+attr.getValue());
}
It reads test1.xml file, finds "/tests/test" node and enumerates it's attributes. When I run this code as part of "desktop" java application (I'm not deep in the java terminology), it displays what I expect (OpenJDK 1.8.3 under Fedora Linux):
SEVERE: ATTR: name=main name
Unfortunately, in Android 1.5 (emulator ofc), the exact same code shows:
E/AutoTesterMain( 823): ATTR: base_lang=main name
As you can see, attribute's value is OK, but there's problem with name. I have completely no idea, why in this place appears name of attribute from completely other DOM element ("/tests/resources"). It seems to parse my file incorrectly, so I probably set something wrong ...
Of course both versions uses the exact same .jars with dom4j and jaxen libraries, so this is probably ok.
This problem has more effects than only incorrect attributes listing. It also disallows me to read attributes properly using XPath - selection "/tests/test/@name" gives nothing, while "/tests/test/@base_lang" gives me "main name" string. This is obviously caused by the same error as in listing attributes above.
Does anyone encountered this? How could I fix it?
Moving to newer Android with native XPath support is unfortunately not an option for me.
I was trying to find anything on web and here, but had no luck.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在模拟器 Android 2.1 上运行我的应用程序时,我遇到了同样的问题。虽然对于 2.2 来说它工作得很好。我使用的dom4j lib版本是v1.6。
正如您从下面的摘录中看到的,所有属性名称都替换为我的 xml 文件中第一个属性的名称:
当实际的 xml 为:
似乎 xml 文档解析不正确。这是我的源代码:
如果您找到了此问题的解决方案,请告诉我。
编辑: dom4j 2.0 alpha 2 存在同样的问题
EDIT2:终于我找到了解决方案。问题确实出在dom4j源代码中。这是链接大约两年前(!)记录的问题。
要修复它,您需要在 \dom4j-1.6.1\src\java\org\dom4j\tree\NamespaceStack.java 中使用正确的以下方法:
I have same issue when running my app on emulator Android 2.1. Although for 2.2 it works perfectly. The version of the dom4j lib I am using is v1.6.
As you can see from the following extract all attribute names are replaced with the name of the first attribute in my xml file:
When the actual xml is :
It seems that xml document is parsed incorrectly. Here is my source code:
Let me know if you have found solution for this issue.
EDIT: same issue for the dom4j 2.0 alpha 2
EDIT2: finally I've found a solution. The problem indeed in dom4j source code. Here is the link to the issue recorded almost two years(!) ago.
To fix it you need correct following method in \dom4j-1.6.1\src\java\org\dom4j\tree\NamespaceStack.java: