Not sure if this mad it. I'm sinding it again...

-Ben


-----Original Message-----
From: Ben Sifuentes [mailto:[EMAIL PROTECTED]]
Sent: Monday, June 11, 2001 11:18 AM
To: Xerces-J-Dev
Subject: Dom TreeWalker


I have a XML & XSL definition that looks like so:

XML
---
<directions>
   <direction>line1</direction>
   <direction>line1</direction>
   <direction>line1</direction>
</directions>

XSL
---
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
<xsl:output omit-xml-declaration=\"yes\" />");
<xsl:template match="/">
<xsl:for-each select="/directions/direction">
<xsl:value-of select="." />
<xsl:text> </xsl:text>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


I want to manualy walk the dom tree and get each element of <direction>
Now I thought I could use the TreeWalker class but when I look at this class
I see a traverse() but, this method walks the entire tree and returns the
results. What I wanted was to walk the tree and have a method return the
element it finds and then manualy go to the next element and retrieve it's
results and so on..

How is this done?

public class TransformToDom
{
        public static void main(String[] args)
    throws java.io.IOException,
           java.net.MalformedURLException,
           org.xml.sax.SAXException
        {
    // Create an XSLT processor.
    XSLTProcessor processor = XSLTProcessorFactory.getProcessor();

    // Create input source documents.
    XSLTInputSource xmlID = new XSLTInputSource("directions.xml");
    XSLTInputSource stylesheetID = new XSLTInputSource("directions.xsl");

    // Create a DOM Document node to attach the result nodes to.
    Document out = new org.apache.xerces.dom.DocumentImpl();
    XSLTResultTarget resultTarget = new XSLTResultTarget(out);

    // Process the source tree and produce the result tree.
    processor.process(xmlID, stylesheetID, resultTarget);

    // Use the FormatterToXML and TreeWalker to print the DOM to System.out
    // Note: Not yet sure how to get the Xerces Serializer to  handle
    // arbitrary nodes.
    FormatterToXML fl = new FormatterToXML(new
FileOutputStream("directions.out"));
    TreeWalker tw = new TreeWalker(fl);
    tw.traverse(out);
          System.out.println("************* The result is in directions.out
*************");
        }
}

Thanks,
-Ben

Reply via email to