Search code examples
javaxmlserializeroutputformat

XMLSerializer & OutputFormat Deprecated


I'm trying to get some help from Java experts around S.O. regarding this issue. I came across an old implementation for a XMLSerializer & OutputFormat in a long time project...I was wondering if someone could give a pointer on what to do, an opinion would be highly appreciated...

I tried this aproach but i couldn't manage to replace with LSSerializer...

The problem...

enter image description here So basically someone used in the project the class XMLSerializer & OutputFormat directly from the IBM internal JRE... How can i convert this peace of code to a dependency free from WAS ( Websphere Aplication Server ) and by using 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();
}

EDIT

Later i managed to do it with the aproach i mencioned earlier...here it is its almost a copy...

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

Solution

  • Import org.w3c.dom.ls.* and use LLSSerializer instead! You'll also need to make a DOMImplementationLS to call so you can create the serializer.