You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@xalan.apache.org by bu...@apache.org on 2002/06/19 17:13:54 UTC

DO NOT REPLY [Bug 10009] New: - indentation not working with Node.appendChild()

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG 
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10009>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND 
INSERTED IN THE BUG DATABASE.

http://nagoya.apache.org/bugzilla/show_bug.cgi?id=10009

indentation not working with Node.appendChild()

           Summary: indentation not working with Node.appendChild()
           Product: XalanJ2
           Version: 2.3
          Platform: PC
        OS/Version: Windows NT/2K
            Status: NEW
          Severity: Normal
          Priority: Other
         Component: org.apache.xalan
        AssignedTo: xalan-dev@xml.apache.org
        ReportedBy: apundir@gmx.net


I am working with xalan-2.3.1.jar file. I tried following code to generate the 
output.
		try {
			DocumentBuilderFactory dbf = 
DocumentBuilderFactory.newInstance();
			dbf.setNamespaceAware(true);
			//dbf.setValidating(true);
			//dbf.setIgnoringElementContentWhitespace(true);
			DocumentBuilder db = dbf.newDocumentBuilder();
			Document dm = db.parse(new File("myinput.xml"));
			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
("new_added_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);
		} catch (Exception e) {}


Here's my output
<?xml version="1.0" encoding="UTF-8"?>
<Inventory_Attributes>
  <Products>
    <Product name="my_product">
      <Version>A</Version>
      <Version>B</Version>
      <Version>C</Version>
    <Version>new_added_version</Version>
    </Product>
  </Products>
</Inventory_Attributes>


My input file was
<Inventory_Attributes>
  <Products>
    <Product name="my_product">
      <Version>A</Version>
      <Version>B</Version>
      <Version>C</Version>
    </Product>
  </Products>
</Inventory_Attributes>

See the new Element "<Version>new_added_version</Version>" is not indented at 
all. However if I substitue the "productNode.appendChild(newElement);" with
"productNode.insertBefore(newElement,productNode.getLastChild());"
Indentation work out as expected. Here's the output with modified code

<?xml version="1.0" encoding="UTF-8"?>
<Inventory_Attributes>
  <Products>
    <Product name="my_product">
      <Version>A</Version>
      <Version>B</Version>
      <Version>C</Version>
      <Version>new_added_version</Version>
    </Product>
  </Products>
</Inventory_Attributes>

But problem over here is that new node is added AT THE END (it should be just 
before last element "<Version>C</Version>" in above case).