Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting xml document to string adds newline

Status
Not open for further replies.

dhiruster

Programmer
Feb 28, 2008
1
US
After PI node I get following output after converting from xml doc to string

<?xml version="1.0" encoding="UTF-8"?>
<?xp Request=007?>
<!DOCTYPE ShowCatalog SYSTEM "
<ShowCatalog>
<AcesVehicle>
<BaseVehicle id="7947"/>
<MfrBodyCode id="1772">VZV2</MfrBodyCode>
</AcesVehicle>
</ShowCatalog>

why is newline added after doctype? When I don't add PI node(?xp) then I don't get a new line.

Here is my code:
DocumentBuilder builder = factory.newDocumentBuilder();
DOMImplementation impl = builder.getDOMImplementation();
DocumentType docType = impl.createDocumentType("ShowCatalog", "SYSTEM", " xmldoc = impl.createDocument(null, "ShowCatalog", docType);
ProcessingInstruction pi = xmldoc.createProcessingInstruction("xp", "Request=007");
Element root = xmldoc.getDocumentElement();
root.getParentNode().insertBefore(pi, root);
Element e = null;
Node n = null;
Element aces = xmldoc.createElement("AcesVehicle");
root.appendChild(aces);
e = xmldoc.createElement("BaseVehicle");
e.setAttribute("id", "7947");
aces.appendChild(e);
e = xmldoc.createElement("MfrBodyCode");
e.setAttribute("id", "1772");
n = xmldoc.createTextNode("VZV2");
e.appendChild(n);
aces.appendChild(e);
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM," StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
DOMSource source = new DOMSource(xmldoc);
transformer.transform(source, result);
String xmlString = writer.toString();

Thanks,
Dhiruster
 
[0] I've got a chance to look into your lines. I can only confirm your observation. The cause of blank line (as "irrelevant" whitespace) seems due to the treatment of pi through the transformer.
[0.1] I've tested using custom identity transformation, the line is still there. So it is not the default id-transformation which causes it.

[1] If you're concerned about the blank line in the output destined to be saved to a file, you can for instance use the xerces-j XMLSerialize and control the output format via the OutputFormat, say, with ("xml","utf-8,true). The outcome is free of that implementation dependent spurious bank line.

[2] If the output is destined for further process as an xml document, serialized or whatever, then it might not be an issue. The blank line could well be insignificant.
 
As a side-note for general audience: In order that the document reflects exactly what intended before passing to the transformer, the doctype line seems erronous.

>DocumentType docType = impl.createDocumentType("ShowCatalog", "SYSTEM", "[ignore][/ignore]");

[tt]DocumentType docType = impl.createDocumentType("ShowCatalog", [red]null[/red], "[ignore][/ignore]");[/tt]

But if that document is to pass to the transformation, that erroneous doctype line would be overridden and hence no longer matters. If it passes directly to any other serializer, it matters.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top