In a birt project development, the situation encountered parsing xml file birt report generation, so decided to use dom4j to resolve. [You need to download dom4j.jar, jaxen-1.1-beta- 10.jar] these two libraries. and initially forgotten to include jaxen caused the error.
xml Code Example:
<Report xmlns = "http://www.eclipse.org/birt/2005/design" Version = "3.2.15" the above mentioned id = "1">
< Property name =-List "cssStyleSheets">
<Structure>
<Property name = "fileName"> D: eport.css </ Property>
</ Structure>
</ List-Property>
</ Report>
I in accordance with general analytical methods dom4j We are encoded:
public class TransferXML {
public static void main (String [] args) throws Exception {
SAXReader SAXReader new new SAXReader = ();
File File = new new File ( "D: /test.xml");
Document document = saxReader.read(file);
Tmp = document.selectNodes List ( "// List-Property");
System.out.println (tmp.size ());
}
}
result output is not 0. I 1. envisioned
and the code is relatively simple, it should there is no problem. then I found a different xml files, the results can be found success node.
for two xml files using the compare tool compare results xmlns only difference there is. there is a not a .faint ... ...
to find relevant information online, or api, etc. consider the two solutions. the first of which is taken from the internet to search, tentatively write here, only a reference (do not sue me, horrifying).
the first embodiment.
public class TransferXML {
public static void main (String [] args) throws Exception {
the Map Map new new = the HashMap ();
map.put ( "Design", "HTTP: // WWW .eclipse.org / BIRT / 2005 / Design ");
SAXReader SAXReader new new SAXReader = ();
File File = new new File (" D: /test.xml ");
the Document Document = SAXReader.read(file);
XPath x = document.createXPath("//design:list-property");
x.setNamespaceURIs(map);
List nodelist = x.selectNodes(document);
System.out.println(nodelist.size());
}
}
第二个解决方案:
public class TransferXML {
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design");
SAXReader saxReader = new SAXReader();
File file = new File("D:/test.xml");
saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
Document document = saxReader.read(file);
Tmp = document.selectNodes List ( "// Design: List-Property");
System.out.println (tmp.size ());
}
}
In contrast, I still prefer the second method.
If you there are better ways, can stay with me for reference only.
---------------------
author: anyoneking
source: CSDN
original: https: // blog .csdn.net / anyoneking / article / details / 2077074
Disclaimer: This article is a blogger original article, reproduced, please attach Bowen link!
Dom4j have questions about the resolve namespace xmlns, unresolved
Guess you like
Origin blog.csdn.net/xingqibaing/article/details/91881270
Recommended
Ranking