使用nodeList创建XML文档
问题描述:
我需要使用NodeList创建XML文档对象。有人能帮我做到这一点。我已经向您展示的代码及以下使用nodeList创建XML文档
import
javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.*; import
org.w3c.dom.*;
public class ReadFile {
public static void main(String[] args) {
String exp = "/configs/markets";
String path = "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);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
XML文件的XML如下所示
<configs>
<markets>
<market>
<name>Real</name>
</market>
<market>
<name>play</name>
</market>
</markets>
</configs>
在此先感谢..
答
你应该做的是这样的:
- 创建新
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>
答
您可以尝试Document的adoptNode()
方法。
也许你需要遍历你的NodeList
。您可以通过nodeList.item(i)
访问个人Nodes
。
如果你想包装在Element
搜索结果,你可以使用的createElement()的新创建的Document
和appendChild()
Element
它工作正常马尔科。但问题是有一个叫做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