Change the com.sun.org.apache.xml.internal.serialize.XMLSerializer amp; com.sun.org.apache.xml.internal.serialize.OutputFormat(更改 com.sun.org.apache.xml.internal.serialize.XMLSerializer amp;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 编译时出现一些错误.我找到的解决方案是在添加 xerces 后使用 org.apache.xml.serialize.XMLSerializer 和 org.apache.xml.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>
推荐答案
我们可以使用 org.w3c.dom.ls 包中的 LSSerializer 类
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;
}
这篇关于更改 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
基础教程推荐
- 如何在相机中应用自定义滤镜 [Surfaceview 预览]. 2022-01-01
- 将 double 转换为 Int,向下舍入 2022-01-01
- doFilter()是在servlet的工作完成之前还是之后执行的? 2022-01-01
- 在springboot中如何给mybatis加拦截器 2023-04-29
- JPA惰性列表上的流 2022-01-01
- 控制台应用程序中的 Java 键盘输入解析 2022-01-01
- 在java中使用xpath和selenium解析HTML表格数据 2022-01-01
- Maven:无效的目标版本:10 2022-01-01
- 将 Windows 证书导入 Java 2022-01-01
- Java ECDSAwithSHA256 签名长度不一致 2022-01-01
