使用nodeList创建XML文档 [英] Create XML document using nodeList
问题描述
我需要使用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文件显示如下
< 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
$ b
这是代码:
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);
文档newXmlDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()。newDocument();
元素root = newXmlDocument.createElement(root);
newXmlDocument.appendChild(root); (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.0encoding =UTF-16?>
< root>< market>
< name> Real< / name>
< / market>< market>
< name> play< / name>
< / market>< / root>
一些注释:
- 我已将
exp
更改为/ configs / markets / market
,因为我怀疑你要复制市场
元素,而不是单个市场
元素 - code> printXmlDocument ,我使用了这个 answer
I希望这有帮助。
如果不想创建新的根元素,那么可以使用原来的XPath表达式返回一个由单个节点组成的 NodeList
(请记住,您的XML必须有一个根元素),您可以直接添加到新的XML文档中。 / p>
请参阅以下代码,我在上面的代码中注释了行:
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);
文档newXmlDocument = DocumentBuilderFactory.newInstance()
.newDocumentBuilder()。newDocument();
//元素root = newXmlDocument.createElement(root);
//newXmlDocument.appendChild(root); (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.0encoding =UTF-16?>
< markets>
< market>
< name> Real< / name>
< / market>
< market>
< name> play< / name>
< / market>
< / markets>
I need to create a XML Document object using the NodeList. Can some one pls help me to do this. I have shown you the code and the xml below
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 file is shown below
<configs>
<markets>
<market>
<name>Real</name>
</market>
<market>
<name>play</name>
</market>
</markets>
</configs>
Thanks in advance..
You should do it like this:
- you create a new
org.w3c.dom.Document newXmlDoc
where you store the nodes in yourNodeList
, - you create a new root element, and append it to
newXmlDoc
- then, for each node
n
in yourNodeList
, you importn
innewXmlDoc
, and then you appendn
as a child ofroot
Here is the code:
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);
}
The output is:
<?xml version="1.0" encoding="UTF-16"?>
<root><market>
<name>Real</name>
</market><market>
<name>play</name>
</market></root>
Some notes:
- I've changed
exp
to/configs/markets/market
, because I suspect you want to copy themarket
elements, rather than the singlemarkets
element - for the
printXmlDocument
, I've used the interesting code in this answer
I hope this helps.
If you don't want to create a new root element, then you may use your original XPath expression, which returns a NodeList
consisting of a single node (keep in mind that your XML must have a single root element) that you can directly add to your new XML document.
See following code, where I commented lines from the code above:
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();
}
}
This will give you the following output:
<?xml version="1.0" encoding="UTF-16"?>
<markets>
<market>
<name>Real</name>
</market>
<market>
<name>play</name>
</market>
</markets>
这篇关于使用nodeList创建XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!