我试图从S.O.周围的Java专家那里得到一些关于这个问题的帮助。我在一个很长的项目中偶然发现了一个XMLSerializer & OutputFormat的旧实现.我想知道是否有人能给出一个指示该做什么的指针,一个意见会非常感谢.
我试过这个aproach,但我无法用LSSerializer代替.
问题是..。

因此,基本上有人在项目中使用的类XMLSerializer & OutputFormat直接来自于internal .如何通过使用org.w3c将这种平静的代码转换为没有WAS ( Websphere Aplication )的依赖关系?
...
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();
}编辑
后来,我设法用我提到的earlier...here来做这件事,它几乎是一个复制品.
...
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;
}发布于 2019-07-03 19:28:47
导入org.w3c.dom.ls.*并使用LLSSerializer!您还需要创建一个DOMImplementationLS来调用,这样就可以创建序列化程序。
https://stackoverflow.com/questions/55729019
复制相似问题