Serialize DOM to FileOutputStream using Xerces

后端 未结 2 502
难免孤独
难免孤独 2020-12-17 22:00

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

相关标签:
2条回答
  • 2020-12-17 22:15

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

    0 讨论(0)
  • 2020-12-17 22:18

    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);
        }
    }
    
    0 讨论(0)
提交回复
热议问题