问题描述
我工作的应用程序的Android 1.5。目前,我需要解析XML文件。由于Android不支持原生的XPath在API等级3,我决定使用dom4j中和Jaxen的库来读取文件。
我有简单的XML文件(test1.xml):
<?XML版本=1.0编码=UTF-8&GT?;
<测试与GT;
<资源base_lang =ENGT&;
<串基地=榜样>
<本土化LANG =PL>Przykład< /本地化>
< /串>
< /资源>
<试验NAME =主域名>
<标题>主标题< /标题>
<描述>长描述< /描述>
< /试验>
< /测试>
和简单的code:
SAXReader依靠读卡器=新SAXReader依靠();
文档的DOC = reader.read(文件:///sdcard/tests/test1.xml);
节点node = doc.selectSingleNode(/测试/测试);
元素ELEM =(元素)节点;
为(中间体J = 0; J&下; elem.attributeCount(); ++ j)条{
属性ATTR = elem.attribute(J);
。Logger.getLogger(AutoTesterMain.class.getName())日志(Level.SEVEREATTR:+ attr.getName()+=+ attr.getValue());
}
据读取test1.xml文件,找到/测试/测试节点,并列举了它的属性。当我运行这个code作为桌面的Java应用程序的一部分(我不是在java术语深),它显示我的期望(OpenJDK的1.8.3在Fedora Linux系统):
严重:ATTR:名称=主名称
不幸的是,Android 1.5的(模拟器OFC),完全相同的code所示:
E / AutoTesterMain(823):ATTR:base_lang =主名称
正如你所看到的,属性的值是确定的,但有名字的问题。我完全不知道,为什么在这个地方出现属性的名字从其他完全DOM元素(/测试/资源)。这似乎不正确地分析我的文件,所以我可能设置一些错误...
当然,两个版本采用与dom4j的和Jaxen的库完全相同的.jar文件,所以这可能是好的。
这问题比只有不正确的属性更多的上市效果。它还禁止我使用XPath正确读取属性 - 选择/测试/测试/ @名给什么,而/测试/测试/ @ base_lang给我主名称字符串。这是通过相同的错误在列表上述属性明显引起
有没有人遇到过吗?我怎么能解决这个问题?
移动到新的Android原生支持XPath是可惜不是我的选择。
我试图找到网络和这里任何事情,但没有运气。
在模拟器Android 2.1的运行我的应用程序时,我有同样的问题。虽然2.2它完美。我使用dom4j的LIB的版本是V1.6。
你可以从以下提取所有的属性名都与我的XML文件中的第一个属性的名称代替看到:
<?XML版本=1.0编码=UTF-8&GT?;
!&LT; DOCTYPE NCX PUBLIC - // NISO // DTD NCX 2005-1 // ENhttp://www.daisy.org/z3986/2005/ncx-2005-1.dtd\"><ncx的xmlns =http://www.daisy.org/z3986/2005/ncx/版本=2005-1&GT;
&LT; HEAD&GT;
&LT;元版本=DTB:UID版本=6efac3c8-0d51-46bd-b125-58dae01bf92e/&GT;
&LT;元版本=DTB:深度版=4/&GT;
&LT;元版本=DTB:totalPageCount版本=0/&GT;
&LT;元版本=DTB:maxPageNumber版本=0/&GT;
&LT; /头&GT;
&LT; docTitle&GT;
&LT;文字和GT;书籍及LT; /文字和GT;
&LT; / docTitle&GT;
&LT; navMap&GT;
&所述;的navPoint版本=的navPoint-1版本=1&GT;
&LT; navLabel&GT;
&LT;文字和GT;&头1 LT; /文字和GT;
&LT; / navLabel&GT;
&lt;内容版本=TEXT / Section0001.xhtml/&GT;
&LT;版本的navPoint =的navPoint-2版本=2&GT;
在实际的XML是:
&LT;?XML版本=1.0编码=UTF-8&GT?;
!&LT; DOCTYPE NCX PUBLIC - // NISO // DTD NCX 2005-1 // EN
http://www.daisy.org/z3986/2005/ncx-2005-1.dtd\">&LT; NCX的xmlns =http://www.daisy.org/z3986/2005/ncx/版本=2005-1&GT;
&LT; HEAD&GT;
&LT; META NAME =DTB:UIDCONTENT =6efac3c8-0d51-46bd-b125-58dae01bf92e/&GT;
&LT; META NAME =DTB:深度CONTENT =4/&GT;
&LT; META NAME =DTB:totalPageCountCONTENT =0/&GT;
&LT; META NAME =DTB:maxPageNumberCONTENT =0/&GT;
&LT; /头&GT;
&LT; docTitle&GT;
&LT;文字和GT;书籍及LT; /文字和GT;
&LT; / docTitle&GT;
&LT; navMap&GT;
&所述;的navPoint ID =的navPoint-1playOrder =1&GT;
&LT; navLabel&GT;
&LT;文字和GT;&头1 LT; /文字和GT;
&LT; / navLabel&GT;
&lt;内容SRC =TEXT / Section0001.xhtml/&GT;
&所述;的navPoint ID =的navPoint-2playOrder =2&GT;
似乎XML文档不能被正确解析。下面是我的源$ C $ C:
的InputStream =新的FileInputStream(ncfPath);
SAXReader依靠读卡器=新SAXReader依靠(); // dom4j的SAXReader依靠
文献文件= reader.read(InputStream的);
字符串XML = document.asXML();
让我知道你是否已经找到了这个问题的解决方案。
编辑:为dom4j的2.0阿尔法2 同样的问题
EDIT2:终于我找到了一个解决方案。在dom4j中源$ C $ C确实是问题。这里是<一个href=\"https://sourceforge.net/tracker/index.php?func=detail&aid=2789052&group_id=16035&atid=116035#\"相对=nofollow>链接的问题记录的近两年(!)前。
要解决它,你需要正确的下面的方法中的 \\ dom4j的-1.6.1的\\ src \\ java的\\组织\\ dom4j的\\树\\ NamespaceStack.java 的:
公开的QName getAttributeQName(字符串的namespaceURI,字符串的localName,字符串的qualifiedName){
//修复:是的qualifiedName在Android 2.1空
//指https://sourceforge.net/tracker/index.php?func=detail&aid=2789052&group_id=16035&atid=116035#
如果(的qualifiedName == NULL || qualifiedName.length()== 0){
的qualifiedName =的localName;
}
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.
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:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN" "http://www.daisy.org/z3986/2005/ncx-2005-1.dtd"><ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta version="dtb:uid" version="6efac3c8-0d51-46bd-b125-58dae01bf92e"/>
<meta version="dtb:depth" version="4"/>
<meta version="dtb:totalPageCount" version="0"/>
<meta version="dtb:maxPageNumber" version="0"/>
</head>
<docTitle>
<text>Book</text>
</docTitle>
<navMap>
<navPoint version="navPoint-1" version="1">
<navLabel>
<text>Header1</text>
</navLabel>
<content version="Text/Section0001.xhtml"/>
<navPoint version="navPoint-2" version="2">
When the actual xml is :
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ncx PUBLIC "-//NISO//DTD ncx 2005-1//EN"
"http://www.daisy.org/z3986/2005/ncx-2005-1.dtd">
<ncx xmlns="http://www.daisy.org/z3986/2005/ncx/" version="2005-1">
<head>
<meta name="dtb:uid" content="6efac3c8-0d51-46bd-b125-58dae01bf92e"/>
<meta name="dtb:depth" content="4"/>
<meta name="dtb:totalPageCount" content="0"/>
<meta name="dtb:maxPageNumber" content="0"/>
</head>
<docTitle>
<text>Book</text>
</docTitle>
<navMap>
<navPoint id="navPoint-1" playOrder="1">
<navLabel>
<text>Header1</text>
</navLabel>
<content src="Text/Section0001.xhtml"/>
<navPoint id="navPoint-2" playOrder="2">
It seems that xml document is parsed incorrectly. Here is my source code:
inputStream = new FileInputStream(ncfPath);
SAXReader reader = new SAXReader(); // dom4j SAXReader
Document document = reader.read(inputStream);
String xml = document.asXML();
Let me know if you have found solution for this issue.
EDIT: same issue for the dom4j 2.0 alpha 2EDIT2: 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:
public QName getAttributeQName(String namespaceURI, String localName, String qualifiedName) {
// Fix: qualifiedName is empty on Android 2.1
// refer to https://sourceforge.net/tracker/index.php?func=detail&aid=2789052&group_id=16035&atid=116035#
if (qualifiedName == null || qualifiedName.length() == 0) {
qualifiedName = localName;
}
这篇关于Android的1.5和dom4j的 - 属性解析不正确的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!