Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

java - Change the com.sun.org.apache.xml.internal.serialize.XMLSerializer & com.sun.org.apache.xml.internal.serialize.OutputFormat

Using com.sun.org.apache.xml.internal.serialize.XMLSerializer and com.sun.org.apache.xml.internal.serialize.OutputFormat causes some errors when compiling using java 1.6. The solution I found is by using org.apache.xml.serialize.XMLSerializer and org.apache.xml.serialize.OutputFormat after adding xerces. The problem is that theses classes are deprecated. What can I use without to replace them without touching the code ? Thnx This is the dependency I used :

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.9.1</version>
</dependency>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

We can use the LSSerializer class from the package org.w3c.dom.ls

public String toXML(Node source) {

    String subscrXML=null;
    StringWriter stringWriter=new StringWriter();
     try {
        //Get the implementations

        DOMImplementationRegistry registry =  DOMImplementationRegistry.newInstance();

        DOMImplementationLS impls =  (DOMImplementationLS)registry.getDOMImplementation("LS");


        //Prepare the output
        LSOutput domOutput = impls.createLSOutput();
        domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());            
        domOutput.setCharacterStream(stringWriter);
        domOutput.setEncoding(ENCODING);
        //Prepare the serializer
        LSSerializer domWriter = impls.createLSSerializer();            
        DOMConfiguration domConfig = domWriter.getDomConfig();
        domConfig.setParameter("format-pretty-print", true);
        domConfig.setParameter("element-content-whitespace", true);
        domWriter.setNewLine("
");     
        domConfig.setParameter("cdata-sections", Boolean.TRUE);
        //And finaly, write
        domWriter.write(source, domOutput);
        subscrXML = domOutput.getCharacterStream().toString();
        DOMStringList dsl=domConfig.getParameterNames();
        System.out.println(subscrXML);
        /*
         // Just for curiosity.... 
         for(int i=0;i<dsl.getLength();i){
            System.out.println(dsl.item(i)" = ["domConfig.getParameter(dsl.item(i))"]");
        }*/
     } catch (Exception e) {
         e.printStackTrace();
     }
    return subscrXML;
 }

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...