Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serialize DOM to FileOutputStream using Xerces

Tags:

java

dom

xml

xerces

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?

like image 307
whitehat Avatar asked Nov 24 '11 13:11

whitehat


2 Answers

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);
    }
}
like image 92
Ludovic Kuty Avatar answered Nov 16 '22 08:11

Ludovic Kuty


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

like image 1
Tom Hawtin - tackline Avatar answered Nov 16 '22 10:11

Tom Hawtin - tackline