写出DOM作为XML文件 [英] Writing Out a DOM as an XML File

查看:95
本文介绍了写出DOM作为XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直接来自手册:


将DOM编写为XML文件



构造DOM之后(通过解析XML文件或
以编程方式构建它),您通常希望将其另存为XML。
本节向您展示如何使用Xalan转换
程序包。



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


我的输出:

  thufir @ dur:〜/ NetBeansProjects / helloWorldSaxon $ 
thufir @ dur:〜/ NetBeansProjects / helloWorldSaxon $ gradle clean run

>任务:运行
2019年1月4日下午3:28:24 helloWorldSaxon.HandlerForXML createDocumentFromURL
信息:http://books.toscrape.com/
2019年1月4日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

成功完成2秒
4个可执行的任务:4个执行
thufir @ dur:〜/ NetBeansProjects / helloWorldSaxon $

首先,我想为 domResult 是,看起来或包含什么提供更有意义的输出。我认为,更重要的是迭代或遍历以下文档

 公共无效createDocumentFromURL()引发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变压器= TransformerFactory.newTransformer();
Transformer.transform(source,domResult); //如何找到此操作的结果?

LOG.info(domResult.toString()); //遍历或迭代如何?

DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance()。newDocumentBuilder();
//文档document = documentBuilder.parse(); /// bzzzt,错误的

文档document =(Document)domResult.getNode();

LOG.info(document.getDocumentElement()。getTagName());
}

输出是 html,这使我相信这是 html 。所需的输出是 html ,但来自 Document ,而不是 String



写出 DOM 的Oracle文档是用来解析文档的。该文档尚未解析吗? 或者,换句话说,如何确定它完全不是 XML 文件?



所以.....将其再次转换吗?



另请参见:



Java:将StreamResult转换为DOM

解决方案

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



示例

  //创建DOM 
文档document = DocumentBuilderFactory.newInstance()。newDocumentBuilder()。 newDocument();
元素root = document.createElement( Root);
document.appendChild(root);
元素foo = document.createElement( Foo);
foo.appendChild(document.createTextNode( Bar));
root.appendChild(foo);

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

  //将DOM以XML格式写入文件
File xmlFile = new File( / path / to / file.xml) ;
Transformer变压器= TransformerFactory.newInstance()。newTransformer();
Transformer.transform(新DOMSource(文档),新StreamResult(xmlFile));

您也可以像这样打印DOM:

  //将DOM打印为XML 
Transformer转换器= TransformerFactory.newInstance()。newTransformer();
Transformer.transform(新DOMSource(文档),新StreamResult(System.out));

输出



< pre $ = lang-xml prettyprint-override> <?xml version = 1.0 encoding = UTF-8 standalone = no?>< Root>< Foo> Bar< / Foo< / Root>

如果您想要XML格式:

  //将DOM打印为格式化的XML 
Transformer Transformer = TransformerFactory.newInstance()。newTransformer();
Transformer.setOutputProperty(OutputKeys.INDENT, yes);
Transformer.transform(新DOMSource(文档),新StreamResult(System.out));

输出



< pre class = lang-xml prettyprint-override> <?xml version = 1.0 encoding = UTF-8 standalone = no?>
< Root>
< Foo> Bar< / Foo>
< / Root>


Straight from the manual:

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.

my output:

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$ 

Firstly, I'd like more meaningful output for what the domResult is, looks like, or contains. More important, I believe, is iterating or traversing document below:

    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());
        }

That the output is "html" inclines me to believe that this is the html. The desired output is that html, but from a Document, rather than a String.

Oracle documention on writing out a DOM is to parse the document. Is this document not already parsed? Or, to put another way, how do I establish that it is or is not an XML file at all?

So.....transform it again?

see also:

Java: convert StreamResult to DOM

解决方案

You really just have to transform the DOM to your file.

Example

// 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);

You can save that DOM to a file like this:

// 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));

You can also just print the DOM like this:

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

Output

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

If you want the XML formatted:

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

Output

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

这篇关于写出DOM作为XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆