You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by Avnish Pundir <ap...@cisco.com> on 2002/06/18 22:00:26 UTC
indentation with modified docs
Hi,
I am having problem while printing edited XML documents. I have a XML
document which is already indented properly with spaces. Now I am parsing
the document and adding a few new child nodes to it. But when I print the
document, indentation doesn't work out properly.
Here's the code I m using. I have tried all commented code as well.
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
//dbf.setValidating(true);
//dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
Document dm = db.parse(new File(configFile));
Node productsNode = dm.getElementsByTagName("Products").item(0);
// My own method to find specified node
Element productNode =
(Element) XmlUtils
.getElementsByAttributeName((Element) productsNode, "Product", "name",
"my_product")
.elementAt(0);
Element newElement = dm.createElement("Version");
Text newTextNode = dm.createTextNode(version);
newElement.appendChild(newTextNode);
productNode.appendChild(newElement);
//dm.normalize();
DOMSource source = new DOMSource(dm);
DOMResult domResult = new DOMResult();
Transformer tfm = TransformerFactory.newInstance().newTransformer();
tfm.setOutputProperty(OutputKeys.INDENT, "yes");
tfm.setOutputProperty(OutputKeys.METHOD, "xml");
tfm.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tfm.setOutputProperty("{http://xml.apache.org/xslt}indent-amount","2");
StreamResult result = new StreamResult(System.out);
tfm.transform(source, result);
I am getting this output
<Inventory_Attributes>
<Products>
<Product name="my_product">
<Version>A</Version>
<Version>B</Version>
<Version>C</Version>
<Version>Newly_added_product</Version>
</Product>
</Products>
</Inventory_Attributes>
Whereas the input was
<Inventory_Attributes>
<Products>
<Product name="my_product">
<Version>A</Version>
<Version>B</Version>
<Version>C</Version>
</Product>
</Products>
</Inventory_Attributes>
(see the wrong indentation of "<Version>Newly_added_product</Version>" tags)
I have tried different count of indent-amount without success. I guess I m
missing something minor over here. Any help would be greatly appreciated.
Thanks
Avnish