java - 将 DOM 写为 XML 文件

标签 java xml dom xml-parsing saxon

from手册:

Writing Out a DOM as an XML File

After you have constructed a DOM (either by parsing an XML file or building it programmatically) you frequently want to save it as XML. This section shows you how to do that using the Xalan transform package.

Using that package, you will create a transformer object to wire a DOMSource to a StreamResult. You will then invoke the transformer's transform() method to write out the DOM as XML data.

我的输出:

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 的内容、外观或包含内容。我认为更重要的是迭代或遍历下面的文档:

    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。所需的输出是 html,但来自 Document,而不是 String

Oracle 文档中关于编写 DOM 的内容是解析该文档。这个文档还没有被解析吗? 或者,换句话说,我如何确定它是否是 XML 文件?

那么......再次改造它

另请参阅:

Java: convert StreamResult to DOM

最佳答案

您实际上只需将 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>

关于java - 将 DOM 写为 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54047580/

相关文章:

jQuery 嵌套 DOM 选择器

java - 多线程 FTP 输入流的输出不一致

java - 奇怪的 httpPost 行为(可能与 json、auth、代理相关)GET 有效,POST 有效,但没有代理就不行(部分解决)

Java代码重构

javascript - 将元素插入 DOM,基于时间戳的位置

javascript - 如何判断 DOM 元素在当前视口(viewport)中是否可见?

java - 关于如何将 @autowire 声明放置在父类/接口(interface)的 protected 变量上的混淆。这是如何 Autowiring 的?

java - DOM、SAX 和 StAX XML 解析器之间有什么区别?

android - 如何将带有圆角的android BottomAppBar

xml - NSXMLDocument objectByApplyingXSLT with XSL Include