Serialize DOM in FileOutputStream using Xerces

I use this link to create an XML file using the DOM. It states that "the Xerces parser comes with the JDK 1.5 distribution. Therefore, you do not need to download the parser separately."

However, when I write the next line in my Eclipse Helios, it gives a compile-time error, although I have Java 1.6 on my system.

import org.apache.xml.serialize.XMLSerializer; 

Why is this so?

+8
java dom xml xerces
source share
2 answers

Xerces is really related to the JDK, but you should use it with the JAXP API in javax.xml.parsers . Check the program exit below.

In addition, to serialize the XML Document , you must use the DOM Level 3 Load and Save (present in the JDK) or XSLT transform without a stylesheet (identity transform). The rest is implementation specific. Xerces XMLSerializer is deprecated:

Outdated. This class was deprecated in Xerces 2.9.0. It is recommended that new applications use the DSS Level 3 LSSerializer or the JAXP Transformation API for XML (TrAX) to serialize XML. See the Xerces documentation for more information.

Here is an example of serialization with a DOM level of 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 identity conversion example:

 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 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); } } 
+26
source share

It will be included, IIRC, com.sun.org.apache.xml.serialize.XMLSerializer . However, these are private classes and may change at any time. Instead, I suggest using the standard public APIs ( javax.* And friends). (Use the conversion API without XSLT.)

+1
source share

All Articles