1

关于这个问题,我正试图从 SO 周围的 Java 专家那里获得一些帮助。我在一个长期项目中遇到了 XMLSerializer 和 OutputFormat 的旧实现......我想知道是否有人可以指点该做什么,非常感谢您的意见......

我尝试了这种方法,但我无法用 LSSerializer 替换...

问题...

在此处输入图像描述 所以基本上有人在项目中使用了直接来自 IBM 内部 JRE 的XMLSerializer 和 OutputFormat类......我如何将这种和平的代码转换为不受 WAS(Websphere Aplication Server)和使用 org.w3c 的依赖项?

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

1 回答 1

1

导入 org.w3c.dom.ls.* 并改用 LLSSerializer!您还需要创建一个 DOMImplementationLS 来调用,以便创建序列化程序。

于 2019-07-03T19:28:47.237 回答