DOM4J中通过XPath操作DOM节点

本文通过介绍如何从XML文件中读入,操作DOM和输出DOM到文件来说明如何在DOM4J中通过XPath操作DOM节点.

1,读入XML

Document originalTemplate1 = TemplateUtil.getDocument("com/test/xml/dom4j/templateopr/template1.xml");
Document originalTemplate2 = TemplateUtil.getDocument("com/test/xml/dom4j/templateopr/template2.xml");
Document originalTemplate3 = TemplateUtil.getDocument("com/test/xml/dom4j/templateopr/template3.xml");
2,通过XPath操作节点:

TemplateUtil.setNodeValue(originalTemplate2,"//m0:t2/m0:e2e/m0:E2EDATA","E2E VALUE");
TemplateUtil.setNodeValue(originalTemplate2,"//m0:t2/m1:serviceState/m1:stateCode","CODE VALUE");

TemplateUtil.setNodeValue(originalTemplate3,"//m2:t3/m2:serviceAddressing/m2:messageId","msg id");
TemplateUtil.setNodeValue(originalTemplate3,"//m2:t3/m3:cloud/m3:mode","code");


Element t2Place = (Element)originalTemplate1.selectSingleNode("//m2:t2place");
Node t2Node = originalTemplate2.selectSingleNode("//m0:t2");

//将一个节点加到另外一个节点中作为子节点
t2Place.add((Node)t2Node.clone());

Node t3Node = originalTemplate3.selectSingleNode("//m2:t3");
Element t3Place = (Element)originalTemplate1.selectSingleNode("//m3:t3place");
t3Place.add((Node)t3Node.clone());

3,将XML DOM输出:

TemplateUtil.createFile(originalTemplate1,"D:/temp/test1.xml");

附:用到的TemplateUtil类:


public class TemplateUtil {
private static HashMap<String, Document> templateMap = new HashMap<String, Document>();

private static Map<String, String> getNameSpace() {
Map<String, String> ns = new HashMap<String, String>();

ns.put("m0", "http://www.test.com/m0/");
ns.put("m1", "http://www.test.com/m1/");
ns.put("m2", "http://www.test.com/m2/");
ns.put("m3", "http://www.test.com/m3/");

return ns;
}

public static Document getDocument(String tempaltePath,Map<String, String> nameSpaces) throws DocumentException {
Document document = null;
if (templateMap.get(tempaltePath) != null) {
document = (Document) templateMap.get(tempaltePath).clone();
} else {
synchronized (templateMap) {
document = readRequestFileTemplete(tempaltePath, nameSpaces);
templateMap.put(tempaltePath, document);
document = (Document) document.clone();
}
}
return document;
}

public static Document getDocument(String tempaltePath) throws DocumentException {
Document document = null;
if (templateMap.get(tempaltePath) != null) {
document = (Document) templateMap.get(tempaltePath).clone();
} else {
synchronized (templateMap) {
document = readRequestFileTemplete(tempaltePath, getNameSpace());
templateMap.put(tempaltePath, document);
document = (Document) document.clone();
}
}
return document;
}

private static Document readRequestFileTemplete(String file, Map<String, String> namespaceURIs)
throws DocumentException {
DocumentFactory documentFactory = new DocumentFactory();
documentFactory.setXPathNamespaceURIs(namespaceURIs);
SAXReader reader = new SAXReader();
reader.setDocumentFactory(documentFactory);

InputStream isFile = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
Document doc = reader.read(isFile);
return doc;
}

public static void setNodeValue(Document node, String nodeXpath, String nodeValue) {
Node e = node.selectSingleNode(nodeXpath);
if (e != null) {
e.setText(nodeValue == null ? "" : nodeValue);
}
}

public static File createFile(Document sourceDoc, String sourceFileName) throws FileNotFoundException,
UnsupportedEncodingException, IOException {

File finalFile = new File(sourceFileName);
FileOutputStream out = new FileOutputStream(finalFile);
XMLWriter writer = new XMLWriter(out);
writer.write(sourceDoc);
return finalFile;
}
}

XML

template1.xml

<root xmlns:m0="http://www.test.com/m0/"
xmlns:m1="http://www.test.com/m1/"
xmlns:m2="http://www.test.com/m2/"
xmlns:m3="http://www.test.com/m3/">
<m0:template>
<m2:t2place>
</m2:t2place>
<m3:t3place>
</m3:t3place>
</m0:template>
</root>

template2.xml

<template xmlns:m0="http://www.test.com/m0/"
xmlns:m1="http://www.test.com/m1/">
<m0:t2>
<m0:e2e>
<m0:E2EDATA>
E2E DATA
</m0:E2EDATA>
</m0:e2e>
<m1:serviceState>
<m1:stateCode>
OK
</m1:stateCode>
</m1:serviceState>
</m0:t2>
</template>

template3.xml

<template xmlns:m2="http://www.test.com/m2/"
xmlns:m3="http://www.test.com/m3/">
<m2:t3>

<m2:serviceAddressing>
<m2:from>
http://intra.test.com/
</m2:from>
<m2:to>
<m2:address>
http://ccm.intra.test.com
</m2:address>
</m2:to>
<m2:replyTo>
<m2:address>
http://intra.test.com/
</m2:address>
</m2:replyTo>
<m2:messageId>
8000588300000
</m2:messageId>
<m2:action>
http://ccm.intra.test.com
</m2:action>
</m2:serviceAddressing>

<m3:cloud>
<m3:mode>
All_Or_Nothing
</m3:mode>
</m3:cloud>
</m2:t3>
</template>

你可能感兴趣的:(dom4j)