DOM4J Parse 不返回任何子节点
DOM4J Parse not returning any child nodes
我正在尝试开始编写一个使用 DOM4j 的程序,我希望用它来解析 XML 文件,将其保存到一些表中,最后允许用户操作数据。
不幸的是我停留在最基本的步骤,解析。
这是我的 XML 我试图包括的部分:
<?xml version="1.0"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04">
<BkToCstmrDbtCdtNtfctn>
<GrpHdr>
<MsgId>000022222</MsgId>
当我试图找到我的 XML 的根时,它 return 正确地找到了 "Document" 的根。当我尝试从 Document 中获取子节点时,它也正确地给出了 "BkToCstmrDbtCdtNtfctn"。问题是,当我尝试更进一步并从 "Bk" 获取子节点时,我做不到。我在控制台中得到这个:
org.dom4j.tree.DefaultElement@2b05039f [Element: <BkToCstmrDbtCdtNtfctn uri: urn:iso:std:iso:20022:tech:xsd:camt.054.001.04 attributes: []/>]
这是我的代码,如果有任何反馈,我将不胜感激。最终我想找回 "MsgId" 属性,但总的来说我只想弄清楚如何更深入地解析 XML 因为实际上它可能有大约 25 层。
public static Document getDocument(final String xmlFileName){
Document document = null;
SAXReader reader = new SAXReader();
try{
document = reader.read(xmlFileName);
}
catch (DocumentException e)
{
e.printStackTrace();
}
return document;
}
public static void main(String args[]){
String xmlFileName = "C:\Users\jhamric\Desktop\Camt54.xml";
String xPath = "//Document";
Document document = getDocument(xmlFileName);
Element root = document.getRootElement();
List<Node> nodes = document.selectNodes(xPath);
for(Iterator i = root.elementIterator(); i.hasNext();){
Element element = (Element) i.next();
System.out.println(element);
}
for(Iterator i = root.elementIterator("BkToCstmrDbtCdtNtfctn");i.hasNext();){
Element bk = (Element) i.next();
System.out.println(bk);
}
}
}
最好的方法可能是使用 XPath,但是由于 XML 文档使用名称空间,您不能使用 API 中的 "simple" selectNodes
方法。我会创建一个辅助方法来轻松评估 Document
或 Element
级别上的任何 XPath 表达式:
public static void main(String[] args) throws Exception {
Document doc = getDocument(...);
Map<String, String> namespaceContext = new HashMap<>();
namespaceContext.put("ns", "urn:iso:std:iso:20022:tech:xsd:camt.054.001.04");
// Select the first GrpHdr element in document order
Element element = (Element) select("//ns:GrpHdr[1]", doc, namespaceContext);
System.out.println(element.asXML());
// Select the text content of the MsgId element
Text msgId = (Text) select("./ns:MsgId/text()", element, namespaceContext);
System.out.println(msgId.getText());
}
static Object select(String expression, Branch contextNode, Map<String, String> namespaceContext) {
XPath xp = contextNode.createXPath(expression);
xp.setNamespaceURIs(namespaceContext);
return xp.evaluate(contextNode);
}
请注意,XPath 表达式必须使用映射到输入文档中使用的命名空间 URI 的命名空间前缀,但前缀的实际值无关紧要。
我正在尝试开始编写一个使用 DOM4j 的程序,我希望用它来解析 XML 文件,将其保存到一些表中,最后允许用户操作数据。
不幸的是我停留在最基本的步骤,解析。
这是我的 XML 我试图包括的部分:
<?xml version="1.0"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:camt.054.001.04">
<BkToCstmrDbtCdtNtfctn>
<GrpHdr>
<MsgId>000022222</MsgId>
当我试图找到我的 XML 的根时,它 return 正确地找到了 "Document" 的根。当我尝试从 Document 中获取子节点时,它也正确地给出了 "BkToCstmrDbtCdtNtfctn"。问题是,当我尝试更进一步并从 "Bk" 获取子节点时,我做不到。我在控制台中得到这个:
org.dom4j.tree.DefaultElement@2b05039f [Element: <BkToCstmrDbtCdtNtfctn uri: urn:iso:std:iso:20022:tech:xsd:camt.054.001.04 attributes: []/>]
这是我的代码,如果有任何反馈,我将不胜感激。最终我想找回 "MsgId" 属性,但总的来说我只想弄清楚如何更深入地解析 XML 因为实际上它可能有大约 25 层。
public static Document getDocument(final String xmlFileName){
Document document = null;
SAXReader reader = new SAXReader();
try{
document = reader.read(xmlFileName);
}
catch (DocumentException e)
{
e.printStackTrace();
}
return document;
}
public static void main(String args[]){
String xmlFileName = "C:\Users\jhamric\Desktop\Camt54.xml";
String xPath = "//Document";
Document document = getDocument(xmlFileName);
Element root = document.getRootElement();
List<Node> nodes = document.selectNodes(xPath);
for(Iterator i = root.elementIterator(); i.hasNext();){
Element element = (Element) i.next();
System.out.println(element);
}
for(Iterator i = root.elementIterator("BkToCstmrDbtCdtNtfctn");i.hasNext();){
Element bk = (Element) i.next();
System.out.println(bk);
}
}
}
最好的方法可能是使用 XPath,但是由于 XML 文档使用名称空间,您不能使用 API 中的 "simple" selectNodes
方法。我会创建一个辅助方法来轻松评估 Document
或 Element
级别上的任何 XPath 表达式:
public static void main(String[] args) throws Exception {
Document doc = getDocument(...);
Map<String, String> namespaceContext = new HashMap<>();
namespaceContext.put("ns", "urn:iso:std:iso:20022:tech:xsd:camt.054.001.04");
// Select the first GrpHdr element in document order
Element element = (Element) select("//ns:GrpHdr[1]", doc, namespaceContext);
System.out.println(element.asXML());
// Select the text content of the MsgId element
Text msgId = (Text) select("./ns:MsgId/text()", element, namespaceContext);
System.out.println(msgId.getText());
}
static Object select(String expression, Branch contextNode, Map<String, String> namespaceContext) {
XPath xp = contextNode.createXPath(expression);
xp.setNamespaceURIs(namespaceContext);
return xp.evaluate(contextNode);
}
请注意,XPath 表达式必须使用映射到输入文档中使用的命名空间 URI 的命名空间前缀,但前缀的实际值无关紧要。