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:
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: