使用Xerces将DOM序列化为FileOutputStream [英] Serialize DOM to FileOutputStream using Xerces

查看:144
本文介绍了使用Xerces将DOM序列化为FileOutputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用链接,以使用DOM生成XML文件。它表示Xerces解析器与JDK 1.5分发版捆绑在一起,因此您不需要单独下载解析器。

I am using this link to generate XML file using DOM. It says that "Xerces parser is bundled with the JDK 1.5 distribution.So you need not download the parser separately."

但是,当我在Eclipse中写下列行即使我的系统中有Java 1.6,Helios也提供编译时错误。

However, when I write the following line in my Eclipse Helios it gives compile-time error even though I have Java 1.6 in my system.

import org.apache.xml.serialize.XMLSerializer;

为什么是这样?

推荐答案

Xerces确实与JDK捆绑在一起,但您应该使用它与 javax.xml.parsers 下的JAXP API。检查以下程序的输出。

Xerces is indeed bundled with the JDK but you should use it with the JAXP API under javax.xml.parsers. Check the output of the program below.

另外,要序列化一个XML 文档,应该使用DOM Level 3加载和保存(存在于JDK中)或没有样式表(身份转换)的XSLT转换。其余的依赖于具体的实现。 Xerces XMLSerializer已被弃用:

Also, to serialize an XML Document, you should use DOM Level 3 Load and Save (present in the JDK) or an XSLT transformation with no stylesheet (identity transformation). The rest is dependent on a specific implementation. The Xerces XMLSerializer is deprecated:


已弃用。这个类在Xerces 2.9.0中被弃用了。建议新的应用程序使用DOM Level 3 LSSerializer或JAXP的XML转换API(TrAX)来串行化XML。有关详细信息,请参阅Xerces文档。

Deprecated. This class was deprecated in Xerces 2.9.0. It is recommended that new applications use the DOM Level 3 LSSerializer or JAXP's Transformation API for XML (TrAX) for serializing XML. See the Xerces documentation for more information.

以下是DOM级别3的序列化示例:

Here is an example of serialization with DOM level 3:

import org.w3c.dom.*;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.*;

public class DOMExample3 {

    public static void main(String[] args) throws Exception {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();    
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("XML 3.0 LS 3.0");
        if (impl == null) {
            System.out.println("No DOMImplementation found !");
            System.exit(0);
        }

        System.out.printf("DOMImplementationLS: %s\n", impl.getClass().getName());

        LSParser parser = impl.createLSParser(
                DOMImplementationLS.MODE_SYNCHRONOUS,
                "http://www.w3.org/TR/REC-xml");
        // http://www.w3.org/2001/XMLSchema
        System.out.printf("LSParser: %s\n", parser.getClass().getName());

        if (args.length == 0) {
            System.exit(0);
        }

        Document doc = parser.parseURI(args[0]);

        LSSerializer serializer = impl.createLSSerializer();
        LSOutput output = impl.createLSOutput();
        output.setEncoding("UTF-8");
        output.setByteStream(System.out);
        serializer.write(doc, output);
        System.out.println();
    }
}

这是一个身份转换的例子: p>

Here is an example with an identity transformation:

import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class DOMExample2 {
    public static void main(String[] args) throws Exception {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder parser = factory.newDocumentBuilder();

        System.out.println("Parsing XML document...");
        Document doc;
        doc = parser.parse(args[0]);

        // Xerces Java 2

        /* Deprecated. This class was deprecated in Xerces 2.9.0.
         * It is recommended that new applications use the DOM Level 3
         * LSSerializer or JAXP's Transformation API for XML (TrAX)
         * for serializing XML and HTML.
         * See the Xerces documentation for more information.
         */  
        /*
        System.out.println("XERCES: Displaying XML document...");
        OutputFormat of = new OutputFormat(doc, "ISO-8859-1", true);
        PrintWriter pw = new PrintWriter(System.out);
        BaseMarkupSerializer bms = new XMLSerializer(pw, of);
        bms.serialize(doc);
*/
        // JAXP

        System.out.println("JAXP: Displaying XML document...");
        TransformerFactory transFactory = TransformerFactory.newInstance();
        System.out.println(transFactory.getClass().getName());
        //transFactory.setAttribute("indent-number", 2);
        Transformer idTransform = transFactory.newTransformer();
        idTransform.setOutputProperty(OutputKeys.METHOD, "xml");
        idTransform.setOutputProperty(OutputKeys.INDENT,"yes");
        // Apache default indentation is 0
        idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");                
        Source input = new DOMSource(doc);
        Result output = new StreamResult(System.out);
        idTransform.transform(input, output);
    }
}

这篇关于使用Xerces将DOM序列化为FileOutputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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