[jdom-interest] Trouble using DOMOutputter

Josh Natarajan Joshua.Natarajan at buzzsaw.com
Thu Feb 8 09:48:35 PST 2001


basically I am trying to create a JDOM directly and pass it to the
DOMOutputter. Somehow the 
Document resulting from it is unusable by the transformer. here is a test
case...

this test case does three things
1) builds the JDOM directly and does a transform
2) builds a DOM directly from the Builder by calling parse on a xml file and
then does a transform
3) dumps the source and the result DOM objects

<if you scroll to the bottom i have include the console dump>
domResult1 is not the the same as domResult2...why?
actually domSource1 is not exactly the same as domSource2.
notice the linebreaks put by the serializer in domSource2 but not in
domSource1...any help would be appreciated!

Thanks!

Josh
joshua.natarajan at buzzsaw.com
--------xsltest.java------

import java.io.*;
import javax.xml.parsers.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.dom.DOMResult;
import org.apache.xalan.xslt.*;
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import org.apache.xalan.serialize.*;
import org.apache.xalan.templates.OutputProperties;

public class xsltest
{

    public static void main(String[] args)
    {
        try
        {
            Transformer oTransformer = null;
            TransformerFactory tFactory = TransformerFactory.newInstance();

            if(tFactory.getFeature(DOMSource.FEATURE) &&
tFactory.getFeature(DOMResult.FEATURE))
            {
                oTransformer = tFactory.newTransformer(new
StreamSource("ThinContact.xsl"));
        
                Element e = new Element("ThinContact");
                e.addContent(new Element("CompanyName").setText("My Company
Inc"));
                e.addContent(new Element("Phone").setText("234-3456"));
                e.addContent(new
Element("Email").setText("joshua.natarajan at mycompany.com"));
                e.addContent(new Element("Fax").setText("456-5678"));
                e.addContent(new Element("LastName").setText("Natarajan"));
                e.addContent(new Element("FirstName").setText("Joshua"));
                e.addContent(new Element("Role").setText("Janitor"));
        
                Document jdomDoc = new Document(e);
        
                DOMOutputter outputter = new DOMOutputter();
                org.w3c.dom.Document dom1 = outputter.output(jdomDoc);

                DOMSource domSource1 = new DOMSource(dom1);
                domSource1.setSystemId("ThinContact.xml");

                DocumentBuilderFactory dFactory =
DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dFactory.newDocumentBuilder();
                org.w3c.dom.Document dom2 =
dBuilder.parse("ThinContact.xml");
          
                DOMSource domSource2 = new DOMSource(dom2);
                domSource2.setSystemId("ThinContact.xml");
    
                DOMResult domResult1 = new DOMResult();
                DOMResult domResult2 = new DOMResult();

                oTransformer.transform(domSource1, domResult1);
                oTransformer.transform(domSource2, domResult2);

                Serializer serializer =
SerializerFactory.getSerializer(OutputProperties.getDefaultMethodProperties(
"xml"));
                serializer.setOutputStream(System.out);
                System.out.println("\n-------domSource1--------\n");
 
serializer.asDOMSerializer().serialize(domSource1.getNode());
                System.out.println("\n-------domResult1--------\n");
 
serializer.asDOMSerializer().serialize(domResult1.getNode());
                System.out.println("\n\n\n");
                System.out.println("\n-------domSource2--------\n");
 
serializer.asDOMSerializer().serialize(domSource2.getNode());
                System.out.println("\n-------domResult2--------\n");
 
serializer.asDOMSerializer().serialize(domResult2.getNode());

           }
           else
           {
               System.out.println("oops DOM feature not supported!");
           }
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }
}

      
--------------ThinContact.xsl--------------

<?xml version="1.0" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method ="html" indent="yes"/>

      <xsl:template match="/">
      <junk>
          <xsl:apply-templates select="ThinContact"/>
      </junk>
      </xsl:template>
      
      <xsl:template match="ThinContact">
                <tr>
                 <td bgcolor="Black" colspan="5"><img
src="../images/1x1clear.gif" width="1" height="1" alt=""
border="0"></img></td>
                </tr>
                <tr>
                  <td valign="top"><input type="checkbox" name="namecheck"
value=""></input></td>
                  <td valign="top"> <xsl:value-of select="Role"/></td>

                      <td><a href="#"> <xsl:value-of select="CompanyName"/>
</a><br></br> 
                    <xsl:value-of select="LastName"/>, <xsl:value-of
select="FirstName"/> </td>

                  <td>P: <xsl:value-of select="Phone"/> <br></br> F:
<xsl:value-of select="Fax"/>  </td>
                  <td valign="top"> <xsl:value-of select="Email"/> </td>
                </tr>
     </xsl:template>
</xsl:stylesheet>

----ThinContact.xml------
<?xml version="1.0" encoding="UTF-8"?>
<ThinContact>
<LastName>Natarajan</LastName>
<FirstName>Joshua</FirstName>
<CompanyName>buzzsaw.com</CompanyName>
<Phone>234-2345</Phone>
<Fax>456-5433</Fax>
<Role>Programmer</Role>
<Email>joshua.natarajan at buzzsaw.com</Email>
</ThinContact>

-------Output on the console------


-------domSource1--------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ThinContact><CompanyName>My Company
Inc</CompanyName><Phone>234-3456</Phone><Email>joshua.natarajan at mycompany.co
m</Email><Fax>456-5678</Fax><LastName>Natarajan</LastName><FirstName>Joshua<
/FirstName><Role>Janitor</Role></ThinContact>
-------domResult1--------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<junk/>


-------domSource2--------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ThinContact>
<LastName>Natarajan</LastName>
<FirstName>Joshua</FirstName>
<CompanyName>buzzsaw.com</CompanyName>
<Phone>234-2345</Phone>
<Fax>456-5433</Fax>
<Role>Programmer</Role>
<Email>joshua.natarajan at buzzsaw.com</Email>
</ThinContact>
-------domResult2--------
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<junk><tr><td bgcolor="Black" colspan="5"><img alt="" border="0" height="1"
src="../images/1x1clear.gif" width="1"/></td></tr><tr><td
valign="top"><input name="namecheck" type="checkbox" value=""/></td><td
valign="top">Programmer</td><td><a href="#">buzzsaw.com</a><br/>Natarajan,
Joshua</td><td>P: 234-2345<br/> F: 456-5433</td><td
valign="top">joshua.natarajan at buzzsaw.com</td></tr></junk>




More information about the jdom-interest mailing list