How to create XML string for an object matching the result of a marshal?

I'm trying to create a XML string representation of an object which exactly matches the XML which results from marshalling the object into a request. I'm almost there except that the XML string I come up with is missing the attribute "standalone=\"yes\"" in the topmost xml element, and this attribute is present in the marshalled XML. I'm using the XML string to create a payload signature which must match exactly with the XML payload of the request, so the two XML representations of the object really do need to be identical.

Here's what I'm doing to create the XML from the object:

private String myObjectToXmlString (final MyObject myObject)

throws Exception{

try{

DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

Document document = documentBuilder.newDocument();

myObjectMarshaller.marshal(myObject, document);

Source source =new DOMSource(document);

StringWriter stringWriter =new StringWriter();

Result result =new StreamResult(stringWriter);

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = factory.newTransformer();

transformer.transform(source, result);

return stringWriter.getBuffer().toString();

}catch (Exception ex){

thrownew Exception("Unable to convert a MyObject object to an XML String -- " + ex.toString(), ex);

}

}

The XML which is produced looks like this:

<?xml version=\"1.0\" encoding=\"UTF-8\"?>

<ns2:myObject xmlns:ns2=\"http://sunconnection.sun.com/xml\">

<myObjectId>0</myObjectId>

<objectType>1</objectType>

<description>Test Object</description>

</ns2:myObject>

The payload object is marshalled to the request like so:

// marshall the MyObject to the output stream

OutputStream outputStream = myHttpUrlConnection.getOutputStream();

myObjectMarshaller.marshal(myObject, outputStream);

outputStream.flush();

outputStream.close();

When I view the XML payload of the request it looks like this:

<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>

<ns2:myObject xmlns:ns2=\"http://sunconnection.sun.com/xml\">

<myObjectId>0</myObjectId>

<objectType>1</objectType>

<description>Test Object</description>

</ns2:myObject>

As you can see the two XML representations of the object are identical except for the standalone attribute in the xml element.

It'd be a hack to make the assumption that the standalone attribute will always be there in the marshalled object and to just hard code it into the XML string returned by my object to XML method. Since I'm using the same Marshaller to create the XML string as I am to perform the marshalling to the request I assume that there's some way to make it give the same XML in both cases, and that I'm doing something wrong in my method which converts the object to an XML string. If anyone can see where I'm going wrong in that method (myObjectToXmlString() above), or can suggest a better way of doing this, then I'll certainly appreciate the insight.

--James

[3964 byte] By [monocongoa] at [2007-11-27 9:35:12]
# 1

It turns out that I was taking the wrong approach in my object to XML string method. A cleaner/better way to do it, which gives the correct result, is this:

private String myObjectToXmlString (final MyObject myObject)

throws Exception {

try {

ByteArrayOutputStream stream = new ByteArrayOutputStream();

myObjectMarshaller.marshal(myObject, stream);

return stream.toString();

} catch (Exception ex) {

throw new Exception("Unable to convert a MyObject object to an XML String -- " + ex.toString(), ex);

}

}

--James

monocongoa at 2007-7-12 23:01:37 > top of Java-index,Java Essentials,Java Programming...