Issue with Dom4j parser

Hi All,

I am trying to parse the below XMLfile using org.dom4j api.

I am using the below code to parse this

SAXReader saxReader = new SAXReader();
            Document document = saxReader.read(new File("C://ecllipse//workspace//Hybernate//input//portal.xml"));
            List list = document.selectNodes("//taxonomy/*");

Iterator iter = list.iterator();

            while (iter.hasNext()) {
                     Object obj= iter.next();
//                     org.dom4j.tree.DefaultElement element = (org.dom4j.tree.DefaultElement) obj;

                     System.out.println(obj.getClass());
                     System.out.println(obj);
                  }


In the above script, when I do sysout of the obj, i am getting only frist level tag information under "Taxonomy" tag. What I want is that, each time i get a category tag as "obj", i want the complete tree under the category tag to be available in "obj"

Can any one provide some pointers to this
*********************************************************************************************
<?xml version="1.0" encoding="ISO-8859-1"?>

    <taxonomy name="Portal_TX" className="Generic" type="taxonomy"
        internalId="12345" taxonomyVersion="1.0">
        <details title="Portal Taxonomy">
            <description></description>
            <owners>
                <owner name="testuser"/>
            </owners>
            <operations/>
            <languageInfo/>
        </details>
        <category name="Portal_Category1" className="Generic"
            type="category" internalId="32423423">
            <details title="testing">
                <description></description>
                <owners>
                    <owner name="testuser"/>
                </owners>
                <operations/>
                <languageInfo/>
            </details>
        </category>
        <category name="Portal_Category2" className="Generic"
            type="category" internalId="234324234">
            <details title="">
                <description></description>
                <owners>
                    <owner name="testuser"/>
                </owners>
                <operations/>
                <languageInfo/>
            </details>
        </category>
    </taxonomy>
*********************************************************************************
srinivasysr2003Asked:
Who is Participating?
 
Ajay-SinghConnect With a Mentor Commented:
> System.out.println(obj);

It calls to toString of obj, which prints just the node its on. To print the
whole tree, you should use the following:
 
    OutputFormat format = OutputFormat.createPrettyPrint();

    XMLWriter writer = new XMLWriter( out, format );

    writer.write( document );

    writer.close();
0
All Courses

From novice to tech pro — start learning today.