使用 Xerces 将 DOM 序列化为 FileOutputStream

发布于 2024-12-18 06:03:37 字数 330 浏览 2 评论 0原文

我正在使用 this 链接使用 DOM 生成 XML 文件。它说“Xerces 解析器与 JDK 1.5 发行版捆绑在一起。因此您无需单独下载解析器。”

然而,当我在 Eclipse Helios 中编写以下行时,即使我的系统中有 Java 1.6,它也会出现编译时错误。

import org.apache.xml.serialize.XMLSerializer;

为什么会这样呢?

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."

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;

Why is it so?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤独难免 2024-12-25 06:03:37

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

另外,要序列化 ​​XML Document,您应该使用 DOM Level 3 Load and Save(存在于 JDK 中)或不带样式表的 XSLT 转换(身份转换)。剩下的就看具体的实现了。 Xerces XMLSerializer 已弃用:

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

以下是 DOM 级别 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();
    }
}

以下是身份转换的示例:

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 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.

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:

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.

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

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);
    }
}
a√萤火虫的光℡ 2024-12-25 06:03:37

它将位于 IIRC,com.sun.org.apache.xml.serialize.XMLSerializer。然而,这些都是私人课程,并且可能随时发生变化。我建议改用标准公共 API(javax.* 等)。 (使用不带任何 XSLT 的转换 API。)

It will be in, IIRC, com.sun.org.apache.xml.serialize.XMLSerializer. However, those are private classes and likely to change at any time. I suggest using the standard public APIs (javax.* and friends) instead. (Use the transform API without any XSLT.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文