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

前端 未结 1 1001
情书的邮戳
情书的邮戳 2020-12-04 01:50

Using com.sun.org.apache.xml.internal.serialize.XMLSerializer and com.sun.org.apache.xml.internal.serialize.OutputFormat causes some errors when co

相关标签:
1条回答
  • 2020-12-04 02:42

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