views:

253

answers:

1

I'm looking to create XML Document objects in Java and serialize them to a byte array (prior to sending them across a TCP connection). I currently have code that looks like this:

public byte [] EncapsulateThingy( ThingyType thingy )
{
    parser.reset(); // parser is a pre-existing DocumentBuilder object
    Document doc = parser.newDocument();
    doc.appendChild( doc.createElement("Thingy") );
    // ...  add nodes to doc to represent thingy
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream( 8192 ); 

    //
    // Missing: Write doc to outputStream with xml version 1.0 and UTF-8 
    // encoding.
    //

    return outputStream.toByteArray();
}

The Sun Java documentation has info on a set of interfaces which seems to start with DomImplementationLS for loading and saving XML, which I could use to fill in the missing piece above handily. But I can't figure out how to create an object which implements DomImplementationLS.

My ultimate goal is to serialize and deserialize very simple objects to XML encoded in byte arrays, so I can transmit them across a network. I am interested in keeping the solution lightweight, so that it can handle a high throughput of messages.

I am interested in alternate solutions, so long as they let me specify the exact XML structure which gets sent.

I of course have to provide the deserialization when these XML messages are consumed, but there is plenty of documentation and toturials available online for reading XML, but not much for writing it.

I would prefer solutions that are included in Java 6 without adding packages.

+1  A: 

Hope this helps:

import java.io.ByteArrayOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.junit.Test;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class DomLsTest {

    @Test
    public void testDomLs() throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        DOMImplementation di = db.getDOMImplementation();
        Document d = di.createDocument("", "foo", null);
        Element e = d.createElement("bar");
        d.getDocumentElement().appendChild(e);
        DOMImplementationLS ls = (DOMImplementationLS) di;
        LSOutput lso = ls.createLSOutput();
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        lso.setByteStream(baos);
        LSSerializer lss = ls.createLSSerializer();
        lss.write(d, lso);
        System.out.println(baos.toString());
    }

}

So, for your code, you would need to do something like:

DOMImplementationLS ls = (DOMImplementationLS) parser.getDOMImplementation();
LSOutput lso = ls.createLSOutput();
lso.setByteStream(...);
LSSerializer lss = ls.createSerializer();
lss.write(..., lso);
toolkit
Lol. It works.The DBF-bone's connected to the DB-bone. The DB-bone's connected to the DOMI-bone. The DONI-bone's connected to the LS-bone....
deemer