I'm currently trying to get a part of an xml using DOM with Java language. For instance I would like to be able to have as a String the raw code of an XML Element (Tag) and it's children nodes.
我目前正在尝试使用DOM和Java语言来获得xml的一部分。例如,我希望能够将XML元素(标记)和它的子节点的原始代码作为字符串。
Example :
例子:
XML file :
XML文件:
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
The goal is to have this for the 1st book element :
我们的目标是为第一本书的元素:
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
I'm looking in the Element methods, I can't find some method useful for this in it.
我在元素方法中找不到有用的方法。
Thank you.
谢谢你!
2 个解决方案
#1
3
The W3C DOM does not have a convenient "toXML" like some of the alternative Java XML APIs provide, you need to use either the javax.xml.transform
API or an org.w3c.dom.ls.LSSerializer
. Assuming a variable doc
containing the DOM Document
, you can obtain an instance of LSSerializer
like this:
W3C DOM不像一些可选的Java XML api提供的“toXML”,您需要使用Java . XML。转换API或org.w3c.dom.ls.LSSerializer。假设一个包含DOM文档的变量文档,您可以获得如下的LSSerializer实例:
// import org.w3c.dom.ls.*;
DOMImplementationLS ls = (DOMImplementationLS)doc.getImplementation();
LSSerializer ser = ls.createLSSerializer();
Once you have one of these, then you can serialize any Node
from that document as an XML string using
一旦有了其中的一个节点,就可以使用XML字符串将该文档中的任何节点序列化
String xml = ser.writeToString(node);
#2
1
Look into xpath. While generally xpath is used in the context of HTML it stands for the XML Path Language, try the following.
xpath。虽然xpath通常在HTML上下文中使用,但它代表XML路径语言,请尝试以下操作。
//book[contains(@category,'cooking')]
/ / book[包含(@category,“烹饪”)]
#1
3
The W3C DOM does not have a convenient "toXML" like some of the alternative Java XML APIs provide, you need to use either the javax.xml.transform
API or an org.w3c.dom.ls.LSSerializer
. Assuming a variable doc
containing the DOM Document
, you can obtain an instance of LSSerializer
like this:
W3C DOM不像一些可选的Java XML api提供的“toXML”,您需要使用Java . XML。转换API或org.w3c.dom.ls.LSSerializer。假设一个包含DOM文档的变量文档,您可以获得如下的LSSerializer实例:
// import org.w3c.dom.ls.*;
DOMImplementationLS ls = (DOMImplementationLS)doc.getImplementation();
LSSerializer ser = ls.createLSSerializer();
Once you have one of these, then you can serialize any Node
from that document as an XML string using
一旦有了其中的一个节点,就可以使用XML字符串将该文档中的任何节点序列化
String xml = ser.writeToString(node);
#2
1
Look into xpath. While generally xpath is used in the context of HTML it stands for the XML Path Language, try the following.
xpath。虽然xpath通常在HTML上下文中使用,但它代表XML路径语言,请尝试以下操作。
//book[contains(@category,'cooking')]
/ / book[包含(@category,“烹饪”)]