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");
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]);
lkuty
source share