簡體   English   中英

使用Xerces將DOM序列化為FileOutputStream

[英]Serialize DOM to FileOutputStream using Xerces

我正在使用鏈接使用DOM生成XML文件。 它說“Xerces解析器與JDK 1.5發行版捆綁在一起。所以你不需要單獨下載解析器。”

但是,當我在Eclipse Helios中編寫以下行時,即使我的系統中有Java 1.6,它也會產生編譯時錯誤。

import org.apache.xml.serialize.XMLSerializer;

為什么會這樣?

Xerces確實與JDK捆綁在一起,但您應該將它與javax.xml.parsers下的JAXP API一起使用。 檢查下面程序的輸出。

此外,要序列化XML Document ,您應該使用DOM Level 3加載和保存(存在於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);
    }
}

它將在IIRC, com.sun.org.apache.xml.serialize.XMLSerializer 但是,這些都是私人課程,可能隨時改變。 我建議使用標准的公共API( javax.*和朋友)。 (使用不帶任何XSLT的轉換API。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM