不推荐使用 XMLSerializer 和 OutputFormat
XMLSerializer & OutputFormat Deprecated
我正在尝试从 Java 左右的专家那里获得一些帮助 S.O。关于这个问题。我在一个长期项目中遇到了 XMLSerializer 和 OutputFormat 的旧实现...我想知道是否有人可以提供有关该做什么的指示,我们将不胜感激...
我尝试了这个 aproach 但我无法用 LSSerializer 替换...
问题...
所以基本上有人在项目中使用直接来自 IBM 内部 JRE 的 class XMLSerializer & OutputFormat...
我如何通过使用 org.w3c 将这种和平的代码转换为不受 WAS ( Websphere Aplication Server ) 影响的依赖项?
...
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
...
public String toString() {
StringWriter res = new StringWriter();
OutputFormat format = new OutputFormat(doc);//doc is type org.w3c.dom.Document
format.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(res, format);
try {
serializer.serialize(doc);
} catch (IOException e) {
res.write(e.getMessage());
e.printStackTrace();
}
return res.toString();
}
编辑
后来我设法用我之前提到的方法做到了...这几乎是一个副本...
...
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
...
public String toString() {
StringWriter stringWriter = new StringWriter();
String subscrXML=null;
DOMImplementationRegistry registry = null;
DOMImplementationLS impls = null;
LSOutput domOutput = null;
LSSerializer domWriter = null;
try {
registry = DOMImplementationRegistry.newInstance();
impls = (DOMImplementationLS)registry.getDOMImplementation("LS");
//Prepare the output
domOutput = impls.createLSOutput();
domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());
domOutput.setCharacterStream(stringWriter);
domOutput.setEncoding("UTF-8");
//Prepare the serializer
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(doc, 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 (ClassCastException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return subscrXML;
}
导入 org.w3c.dom.ls.* 并改用 LLSSerializer!您还需要创建一个 DOMImplementationLS 来调用,以便您可以创建序列化程序。
我正在尝试从 Java 左右的专家那里获得一些帮助 S.O。关于这个问题。我在一个长期项目中遇到了 XMLSerializer 和 OutputFormat 的旧实现...我想知道是否有人可以提供有关该做什么的指示,我们将不胜感激...
我尝试了这个 aproach 但我无法用 LSSerializer 替换...
问题...
...
import org.apache.xml.serialize.OutputFormat;
import org.apache.xml.serialize.XMLSerializer;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
...
public String toString() {
StringWriter res = new StringWriter();
OutputFormat format = new OutputFormat(doc);//doc is type org.w3c.dom.Document
format.setIndenting(true);
XMLSerializer serializer = new XMLSerializer(res, format);
try {
serializer.serialize(doc);
} catch (IOException e) {
res.write(e.getMessage());
e.printStackTrace();
}
return res.toString();
}
编辑
后来我设法用我之前提到的方法做到了...这几乎是一个副本...
...
import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.DOMException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;
...
public String toString() {
StringWriter stringWriter = new StringWriter();
String subscrXML=null;
DOMImplementationRegistry registry = null;
DOMImplementationLS impls = null;
LSOutput domOutput = null;
LSSerializer domWriter = null;
try {
registry = DOMImplementationRegistry.newInstance();
impls = (DOMImplementationLS)registry.getDOMImplementation("LS");
//Prepare the output
domOutput = impls.createLSOutput();
domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());
domOutput.setCharacterStream(stringWriter);
domOutput.setEncoding("UTF-8");
//Prepare the serializer
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(doc, 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 (ClassCastException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return subscrXML;
}
导入 org.w3c.dom.ls.* 并改用 LLSSerializer!您还需要创建一个 DOMImplementationLS 来调用,以便您可以创建序列化程序。