A
Adam Funk
I have a web-service client that gets an org.w3c.dom.Element out of
the service's response. (The service contains an application that
produces an XML document and returns its root element as the "answer"
in the response.) I need to turn that Element into a String to
display in the client GUI and to save as a file on the client machine.
This Element has the xml:space="preserve" attribute (added by the
application in the service).
So far I have tried two things:
javax.xml.transform.Source domSource = new DOMSource(xmlElement);
StringWriter sw = new StringWriter();
javax.xml.transform.stream.StreamResult streamResult = new StreamResult(sw);
javax.xml.transform.Transformer identityTransform = transformerFactory.newTransformer();
identityTransform.transform(domSource, streamResult);
return sw.toString();
org.jdom.output.XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
org.jdom.input.DOMBuilder builder = new DOMBuilder();
return outputter.outputString(builder.build(xmlElement));
Both approaches delete a lot of whitespace inside the root element,
but I don't want this to happen. (The root element still has the
xml:space="preserve" attribute.)
I've had a look around on the web and found only pages telling you how
to get rid of whitespace, but not how to force it to stay. This one
[1] says that preserving whitespace is the default anyway.
I'd appreciate any debugging suggestions or alternative approaches.
Thanks,
Adam
[1]
http://www.xml.com/pub/a/2001/11/07/whitespace.html
the service's response. (The service contains an application that
produces an XML document and returns its root element as the "answer"
in the response.) I need to turn that Element into a String to
display in the client GUI and to save as a file on the client machine.
This Element has the xml:space="preserve" attribute (added by the
application in the service).
So far I have tried two things:
javax.xml.transform.Source domSource = new DOMSource(xmlElement);
StringWriter sw = new StringWriter();
javax.xml.transform.stream.StreamResult streamResult = new StreamResult(sw);
javax.xml.transform.Transformer identityTransform = transformerFactory.newTransformer();
identityTransform.transform(domSource, streamResult);
return sw.toString();
org.jdom.output.XMLOutputter outputter = new XMLOutputter(Format.getRawFormat());
org.jdom.input.DOMBuilder builder = new DOMBuilder();
return outputter.outputString(builder.build(xmlElement));
Both approaches delete a lot of whitespace inside the root element,
but I don't want this to happen. (The root element still has the
xml:space="preserve" attribute.)
I've had a look around on the web and found only pages telling you how
to get rid of whitespace, but not how to force it to stay. This one
[1] says that preserving whitespace is the default anyway.
I'd appreciate any debugging suggestions or alternative approaches.
Thanks,
Adam
[1]
http://www.xml.com/pub/a/2001/11/07/whitespace.html