【问题标题】:Writing Out a DOM as an XML File将 DOM 写为 XML 文件
【发布时间】:2019-01-04 23:39:39
【问题描述】:

from说明书:

将 DOM 写成 XML 文件

在你构建了一个 DOM 之后(通过解析 XML 文件或 以编程方式构建它)您经常希望将其保存为 XML。 本节向您展示如何使用 Xalan 转换来实现 包。

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

我的输出:

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

    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

关于写出DOM 的Oracle 文档是解析文档。该文档是否尚未解析? 或者,换句话说,我如何确定它是否是 XML 文件?

所以.....再次改造它

另见:

Java: convert StreamResult to DOM

【问题讨论】:

    标签: java xml dom xml-parsing saxon


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-03-31
      • 2015-12-11
      • 2010-09-14
      • 2012-04-01
      • 1970-01-01
      • 1970-01-01
      • 2019-06-08
      • 1970-01-01
      相关资源
      最近更新 更多