更改com.sun.org.apache.xml.internal.serialize.XMLSerializer& com.sun.org.apache.xml.internal.serialize.OutputFormat [英] Change the com.sun.org.apache.xml.internal.serialize.XMLSerializer & com.sun.org.apache.xml.internal.serialize.OutputFormat

查看:1443
本文介绍了更改com.sun.org.apache.xml.internal.serialize.XMLSerializer& com.sun.org.apache.xml.internal.serialize.OutputFormat的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 com.sun.org.apache.xml.internal.serialize.XMLSerializer com.sun.org.apache.xml.internal .serialize.OutputFormat 在使用java 1.6编译时会导致一些错误。
我发现的解决方案是使用 org.apache.xml.serialize.XMLSerializer org.apache.xml.serialize.OutputFormat 添加xerces后。
问题是这些类已被弃用。在不触摸代码的情况下,可以使用什么来替换它们?
Thnx
这是我使用的依赖关系:

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>


推荐答案

我们可以使用包组织中的LSSerializer类。 w3c.dom.ls

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;
 }

这篇关于更改com.sun.org.apache.xml.internal.serialize.XMLSerializer&amp; com.sun.org.apache.xml.internal.serialize.OutputFormat的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆