Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 更改com.sun.org.apache.xml.internal.serialize.XMLSerializer&;com.sun.org.apache.xml.internal.serialize.OutputFormat_Java_Xml_Dependencies_Xerces - Fatal编程技术网

Java 更改com.sun.org.apache.xml.internal.serialize.XMLSerializer&;com.sun.org.apache.xml.internal.serialize.OutputFormat

Java 更改com.sun.org.apache.xml.internal.serialize.XMLSerializer&;com.sun.org.apache.xml.internal.serialize.OutputFormat,java,xml,dependencies,xerces,Java,Xml,Dependencies,Xerces,使用com.sun.org.apache.xml.internal.serialize.XMLSerializer和com.sun.org.apache.xml.internal.serialize.OutputFormat在使用java 1.6编译时会导致一些错误。 我找到的解决方案是在添加xerces后使用org.apache.xml.serialize.XMLSerializer和org.apache.xml.serialize.OutputFormat。 问题是这些类已被弃用。在不接触代

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

<dependency>
    <groupId>xerces</groupId>
    <artifactId>xercesImpl</artifactId>
    <version>2.9.1</version>
</dependency>

薛西斯
干细胞移植
2.9.1

我们可以使用org.w3c.dom.ls包中的LSSerializer类

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;
 }
公共字符串toXML(节点源){
字符串subscrXML=null;
StringWriter StringWriter=新StringWriter();
试一试{
//获取实现
DOMImplementationRegistry=DOMImplementationRegistry.newInstance();
domImplementals impls=(domImplementals)registry.getdomImplementals(“LS”);
//准备输出
LSOutput domOutput=impls.createLSOutput();
setEncoding(java.nio.charset.charset.defaultCharset().name());
setCharacterStream(stringWriter);
domOutput.setEncoding(编码);
//准备序列化程序
LSSerializer domWriter=impls.createLSSerializer();
DOMConfiguration domConfig=domWriter.getDomConfig();
setParameter(“format pretty print”,true);
setParameter(“元素内容空白”,true);
domWriter.setNewLine(“\r\n”);
setParameter(“cdata节”,Boolean.TRUE);
//最后,写
write(源,domOutput);
subscrXML=domOutput.getCharacterStream().toString();
DOMStringList dsl=domConfig.getParameterNames();
System.out.println(subscrXML);
/*
//只是出于好奇。。。。

对于(int i=0;i),此解决方案的问题是无法指定将CDATA放在xml上的位置。对此有更多信息。