You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xerces.apache.org by Allison M Santoro <as...@us.ibm.com> on 2001/02/07 21:00:23 UTC

printing DOM tree

Based on the generateDom.java example I have built a simple DOM tree with a
few elements and a CDATA section.   I have serialized to print it a file.
For the most part I don't car about indentation only that each element has
its own line.  so if I use the code below I get what I want with the
exception of the CDATA section.  I want the CDATA section to print starting
on its own line.  Is there a way to do this.

               Element root = XMLdoc.createElement("Header");     // Create
Root Element
               Element item = XMLdoc.createElement("BODY");       // Create
element
               item.appendChild( XMLdoc.createTextNode(temp));
               root.appendChild( item );  // attach element to Root element
               item = XMLdoc.createElement("Subject");       // Create
element
               item.appendChild( XMLdoc.createTextNode(temp2));
               root.appendChild( item );  // attach element to Root element
               item=XMLdoc.createElement("newCatagories");
               root.appendChild( item );
               Element item2 = XMLdoc.createElement("Catagories");       //
Create element
               item2.appendChild( XMLdoc.createTextNode(temp3));
               item.appendChild( item2 );  // attach element to Root
element
               item.appendChild(XMLdoc.createCDATASection(templine));
               XMLdoc.appendChild( root );    // attach root to document
               OutputFormat format  = new OutputFormat(XMLdoc);
//Serialize DOM
               format.setIndent(4);
               format.setLineWidth(0);

I get the following output:

<?xml version="1.0" encoding="UTF-8"?>
<Header>
    <BODY>text</BODY>
    <Subject>text</Subject>
    <newCatagories>
        <Catagories>new Category</Catagories><![CDATA
[......................
     ..................................................
]]></newCatagories>
</Header>

What I want is:

<?xml version="1.0" encoding="UTF-8"?>
<Header>
    <BODY>text</BODY>
    <Subject>text</Subject>
    <newCatagories>
        <Catagories>new Category</Catagories>
        <![CDATA[......................
     ..................................................
        ]]>
   </newCatagories>
</Header>

Thank you,
Allison



Allison M. Santoro
IBM Global Services
580 Walnut Cincinnati, Ohio
513-762-2367 T/L 663-2367