你應該做的是這樣的:
- 創建新
org.w3c.dom.Document newXmlDoc
您存儲節點在NodeList
,
- 您創建一個新的根元素,將其追加到
newXmlDoc
- 然後,對每個節點
n
在NodeList
,你導入newXmlDoc
n
,然後追加n
爲root
這孩子是代碼:
public static void main(String[] args) {
String exp = "/configs/markets/market";
String path = "src/a/testConfig.xml";
try {
Document xmlDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(path);
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExpression = xPath.compile(exp);
NodeList nodes = (NodeList) xPathExpression.
evaluate(xmlDocument, XPathConstants.NODESET);
Document newXmlDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
Element root = newXmlDocument.createElement("root");
newXmlDocument.appendChild(root);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Node copyNode = newXmlDocument.importNode(node, true);
root.appendChild(copyNode);
}
printTree(newXmlDocument);
} catch (Exception ex) {
ex.printStackTrace();
}
}
public static void printXmlDocument(Document document) {
DOMImplementationLS domImplementationLS =
(DOMImplementationLS) document.getImplementation();
LSSerializer lsSerializer =
domImplementationLS.createLSSerializer();
String string = lsSerializer.writeToString(document);
System.out.println(string);
}
輸出是:
<?xml version="1.0" encoding="UTF-16"?>
<root><market>
<name>Real</name>
</market><market>
<name>play</name>
</market></root>
一些注意事項:
- 我已經改變了
exp
到/configs/markets/market
,因爲我懷疑你要複製的market
元素,而不是單一的markets
元素
- 爲
printXmlDocument
,我使用了有趣的代碼在此answer
我希望這有幫助。
如果你不希望創建一個新的根元素,那麼你可以使用你原來的XPath表達式,它返回一個NodeList
由單個節點(請記住,你的XML必須有一個根元素),您可以直接將其添加到新的XML文檔中。
請參見下面的代碼,在那裏我評論從上面的代碼行:
public static void main(String[] args) {
//String exp = "/configs/markets/market/";
String exp = "/configs/markets";
String path = "src/a/testConfig.xml";
try {
Document xmlDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().parse(path);
XPath xPath = XPathFactory.newInstance().newXPath();
XPathExpression xPathExpression = xPath.compile(exp);
NodeList nodes = (NodeList) xPathExpression.
evaluate(xmlDocument,XPathConstants.NODESET);
Document newXmlDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
//Element root = newXmlDocument.createElement("root");
//newXmlDocument.appendChild(root);
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.item(i);
Node copyNode = newXmlDocument.importNode(node, true);
newXmlDocument.appendChild(copyNode);
//root.appendChild(copyNode);
}
printXmlDocument(newXmlDocument);
} catch (Exception ex) {
ex.printStackTrace();
}
}
這會給你以下的輸出:
<?xml version="1.0" encoding="UTF-16"?>
<markets>
<market>
<name>Real</name>
</market>
<market>
<name>play</name>
</market>
</markets>
它工作正常馬爾科。但問題是有一個叫做root的元素,它並不在xml文檔中。有沒有辦法做到這一點,沒有根元素。在此先感謝 – nath 2011-04-26 08:40:21
,您需要一個XML中的根元素。你可能要做的是用你原來的XPath('String exp =「/ configs/markets」;')提取'markets',然後你的'NodeList'將包含一個節點,它可以直接導入並附加到你的新XML文件:見編輯答案。 – MarcoS 2011-04-26 08:52:44
感謝Marco。現在它的工作很好.. :) – nath 2011-04-26 09:37:28