Uploaded image for project: 'XalanJ2'
  1. XalanJ2
  2. XALANJ-1060

indentation not working with Node.appendChild()

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Resolution: Incomplete
    • 2.3
    • None
    • None
    • Operating System: Windows NT/2K
      Platform: PC
    • 10009

    Description

      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).

      Attachments

        Activity

          People

            Unassigned Unassigned
            apundir@gmx.net Avnish Pundir
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: