写出DOM作为XML文件

时间:2019-01-04 23:39:40

标签: java xml dom xml-parsing saxon

直接from手册:

  

将DOM编写为XML文件

     

构造DOM后(通过解析XML文件或   您通常需要将其保存为XML。   本节说明如何使用Xalan转换   包。

     

使用该软件包,您将创建一个变压器对象来连接一个   DOMSource到StreamResult。然后,您将调用变压器的   transform()方法将DOM作为XML数据写出。

我的输出:

SetWaitAndRetryPolicy1()

首先,我想为thufir@dur:~/NetBeansProjects/helloWorldSaxon$ thufir@dur:~/NetBeansProjects/helloWorldSaxon$ gradle clean run > Task :run Jan 04, 2019 3:28:24 PM helloWorldSaxon.HandlerForXML createDocumentFromURL INFO: http://books.toscrape.com/ Jan 04, 2019 3:28:26 PM helloWorldSaxon.HandlerForXML createDocumentFromURL INFO: javax.xml.transform.dom.DOMResult@3cda1055 Jan 04, 2019 3:28:26 PM helloWorldSaxon.HandlerForXML createDocumentFromURL INFO: html BUILD SUCCESSFUL in 2s 4 actionable tasks: 4 executed thufir@dur:~/NetBeansProjects/helloWorldSaxon$ 是,看起来或包含的内容提供更有意义的输出。我认为,更重要的是迭代或遍历下面的domResult

document

输出为“ html”使我相信这是 public void createDocumentFromURL() throws SAXException, IOException, TransformerException, ParserConfigurationException { LOG.info(url.toString()); TransformerFactory transformerFactory = TransformerFactory.newInstance(); XMLReader xmlReader = XMLReaderFactory.createXMLReader("org.ccil.cowan.tagsoup.Parser"); Source source = new SAXSource(xmlReader, new InputSource(url.toString())); DOMResult domResult = new DOMResult(); Transformer transformer = transformerFactory.newTransformer(); transformer.transform(source, domResult); //how do I find the result of this operation? LOG.info(domResult.toString()); //traverse or iterate how? DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); // Document document = documentBuilder.parse(); ///bzzzt, wrong Document document = (Document) domResult.getNode(); LOG.info(document.getDocumentElement().getTagName()); } 。所需的输出是html,但来自html,而不是Document

Oracle关于写出String的文档是对文档进行解析。该文档尚未解析吗? 或者,换句话说,如何确定它完全不是DOM文件?

所以.....是否再次转换

另请参阅:

Java: convert StreamResult to DOM

1 个答案:

答案 0 :(得分:4)

您实际上只需要将DOM转换为文件即可。

示例

// Create DOM
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = document.createElement("Root");
document.appendChild(root);
Element foo = document.createElement("Foo");
foo.appendChild(document.createTextNode("Bar"));
root.appendChild(foo);

您可以将该DOM保存到这样的文件中:

// Write DOM to file as XML
File xmlFile = new File("/path/to/file.xml");
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(xmlFile));

您还可以像这样打印DOM:

// Print DOM as XML
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.transform(new DOMSource(document), new StreamResult(System.out));

输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Root><Foo>Bar</Foo></Root>

如果要格式化XML:

// Print DOM as formatted XML
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.transform(new DOMSource(document), new StreamResult(System.out));

输出

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Root>
    <Foo>Bar</Foo>
</Root>