Problem with commons digester for XML parsing

Hi experts,
                  I am using commons digester for XML parsing. The problem I am facing is, My current code is not able to parse the child nodes.
                  It might be a silly mistake somewhere but I cant figure it out.

Below is the sample XML structure:

<TXN_MEASUREMENT agent="42483" slot="560735" datetime="2009-MAR-11 00:01:46" target="911165" agent_inst="44624" profile="0">
        <TXN_SUMMARY delta_msec="34455" element_count="465" trans_level_comp_msec="0" estimated_cache_delta_msec="32213" content_errors="1" delta_user_msec="35946" resp_bytes="2767717"/>
                <TXN_PAGE page_seq="1">
                        <TXN_PAGE_PERFORMANCE delta_msec="883" connect_delta="2" first_byte_msec="32" delta_user_msec="949" remain_packets_delta="20" first_packet_delta="107" system_delta="0" request_delta="0" dns_lookup_msec="1" start_msec="0" estimated_cache_delta_msec="442"/>
                        <TXN_PAGE_OBJECT element_count="54" page_bytes="284417"/>
                        <TXN_PAGE_STATUS content_errors="0"/>
                        <TXN_PAGE_DETAILS page="1">
                                <TXN_BASE_PAGE record_seq="1">
                                        <TXN_DETAIL_PERFORMANCE first_packet_delta="107" system_delta="0" connect_delta="2" request_delta="0" dns_delta="1" element_delta="130" remain_packets_delta="20"/>
                                        <TXN_DETAIL_OBJECT msmt_conn_id="1336" element_cached="0" request_bytes="332" conn_string_text="http://www.sample.com" content_type="51" header_bytes="1287" ip_address="121.0.0.1" object_text="/accounts/" content_bytes="4111"/>
                                        <TXN_DETAIL_STATUS status_code="200"/>
                                </TXN_BASE_PAGE>
                        </TXN_PAGE_DETAILS>
                </TXN_PAGE>

package com.summary;
 
import org.apache.commons.digester.Digester;
import java.io.*;
 
 
public class DigesterSummary {
    
public static void main(String[] args) {
      /*  if (args.length != 1) {
            usage();
            System.exit(-1);
        } */
 
        String filename = "C:"+File.separator+"KeyNoteDump"+File.separator+"XMLparser"+File.separator+"web"+File.separator+"20090312_0000.xml";
 
        // Create a Digester instance
        Digester d = new Digester();
 
        // Prime the digester stack with an object for rules to
        // operate on. Note that it is quite common for "this"
        // to be the object pushed.
       
        Summary book = new Summary();
        d.push(book);
 
        // Add rules to the digester that will be triggered while
        // parsing occurs.
        addRules(d);
 
        // Process the input file.
        try {
            java.io.File srcfile = new java.io.File(filename);
            d.parse(srcfile);
        }
        catch(java.io.IOException ioe) {
            System.out.println("Error reading input file:" + ioe.getMessage());
            System.exit(-1);
        }
        catch(org.xml.sax.SAXException se) {
            System.out.println("Error parsing input file:" + se.getMessage());
            System.exit(-1);
        }
 
 
        // Print out all the contents , as loaded from
        // the input file.
        book.print();
    }
 
    private static void addRules(Digester d) {
 
        //--------------------------------------------------
        // when we encounter a specific tag, do the following:
 
        // create a new instance of class "tag name", and push that
        // object onto the digester stack of objects
        d.addObjectCreate("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT", Txn_Measurement.class);
  
        d.addSetProperties("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT");
        
        d.addSetNext("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT", "addPerson");
        
       
 
        
        d.addObjectCreate("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT/TXN_SUMMARY", Txn_Summary.class);
        d.addSetNext("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT/TXN_SUMMARY", "addTxnSummary");
        d.addSetNestedProperties("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT/TXN_SUMMARY");
   
        
       // d.addCallMethod("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT/TXN_SUMMARY", "addTxnSummary",3);
        
        //d.addCallMethod("TXN_DATA_FEED/DP_TXN_MEASUREMENTS/TXN_MEASUREMENT/TXN_SUMMARY/delta_msec", "setDelta_msec",0);
    }
 
    private static void usage() {
        System.out.println("Usage: java Main example.xml");
    }
}

Open in new window

LVL 10
aman0711Asked:
Who is Participating?
 
CEHJConnect With a Mentor Commented:
No. The main class that serves as a demo entry point for the whole TXNDATA api.
0
 
aman0711Author Commented:
The output i am getting is :

you can see, all the attributes of TXN_summary tag are coming as null
------------------------------------------------------------------------------
40157    795118    2009-MAR-11 10:36:19     963949     41990    0

------------------------------------------------------------------------------
Size TXN_SUMMARY 1
 delta_msec           null
 delta_user_msec      null
 content_errors     null
------------------------------------------------------------------------------
40168    795118    2009-MAR-11 10:47:12     963949     42241    0

------------------------------------------------------------------------------
Size TXN_SUMMARY 1
 delta_msec           null
 delta_user_msec      null
 content_errors     null

0
 
CEHJCommented:
I haven't looked at this closely yet but am a little curious as to why you're not using the full and documented api for your schema that i gave you?
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
aman0711Author Commented:
I want to use it Charles. but i dont know where to start with it?

just follow any xmlBeans tutorial? that would do?
0
 
CEHJCommented:
I actually did it in jaxb eventually. You just need to use the api itself really. Take a look at the main class and use it with your own file
0
 
aman0711Author Commented:
Hi Charles,
                   I am a little confused here. main class? of jaxb? or xmlBeans?
0
 
objectsCommented:
>                   I am a little confused here. main class? of jaxb? or xmlBeans?

I'm not surprised :)

0
 
objectsCommented:
looks like you are fairly close though, will have a look at it today for you.

0
 
aman0711Author Commented:
Thanks Mick :-)

My code is parsing the parent tag. But child tag is coming as null.. if I get data in this, then I guess half of the problem is solved
0
 
CEHJConnect With a Mentor Commented:
Have a look at the javadoc for the TXNDATA api i sent you. Its all in one jar
0
 
aman0711Author Commented:
oh I thought you sent me the edited schema.
0
 
CEHJCommented:
I sent you (or gave you a link to) a complete API with javadoc and test main class
0
 
aman0711Author Commented:
I am Sorry Charles, I guess I didnt get it or just overlooked. Will go through your email again
0
 
CEHJCommented:
It could be in one of the questions actually
0
 
aman0711Author Commented:
Ohk. just got in the office, will go through the questions.
Thanks Charles :-)
0
 
CEHJConnect With a Mentor Commented:
Here it is again. Start with the TXNDATAFEED Javadoc

https://filedb.experts-exchange.com/incoming/ee-stuff/7639-jaxb-TXNDATAFEED.jar
0
 
aman0711Author Commented:
Hi Charles,

              Sorry for acting stupid again. but can we directly import these jar files in RAD or I have to extract it first
0
 
CEHJCommented:
Yes, should be fine to import
0
 
aman0711Author Commented:
Cool. Thanks :-)
Let me import and get back to you
0
 
aman0711Author Commented:
Hi Charles,
                     Its not allowing me import the project as such for some reason. Will try to extract it outside and then copy paste
0
 
aman0711Author Commented:
Hi Charles,
                   I got the files in the project. now I need to commons logging in the classpath? is there anything else that I need to include?

Project-folder.PNG
0
 
CEHJCommented:
>>Its not allowing me import the project

You simply add the jars to the project if they have binaries in. You can also point the IDE to the javadoc jar
0
 
CEHJCommented:
You wouldn't necessarily want, at this point, the TXNDATAFEED *source* files in the project tree, although again, you can point the project to where the source is. IOW it's mainly the binaries ones you need
0
 
CEHJCommented:
TXNDATAFEED-bin.jar is what you need to add, together with the commons jars
0
 
aman0711Author Commented:
Wow, neevr thought this could be a trouble again :-)
This is first tmie I am dealing with jar files for importing the project. Please bear with me :)
 
Tried to import the project (TXNDATAFEED-javadoc) , but eclipse wont let me import it.
0
 
CEHJCommented:
>>Tried to import the project (TXNDATAFEED-javadoc)

That's not a project. Simply jarred javadoc. You just tell Eclipse it's the docs for the TXNDATAFEED api
0
 
aman0711Author Commented:

Hmmm.. I feel sorry for myself now :-(
Thanks for your patience Charles :)
  ok let me start like this :
  After extracting the jaxb-TXNDATAFEED.jar, I got a folder names jaxb.
  Inside jaxb, I have lib folder and three jar files namely:
  - runme
  - TXNDATAFEED-javadoc
  - TXNDATAFEED-src
0
 
CEHJCommented:
Yes, you want to add the *bin* jar to the project build path
0
 
aman0711Author Commented:
Cool, so I guess I am close now.
This is where I am. Any other jar missing now?

structure.PNG
0
 
CEHJCommented:
jaxb is missing - you need to add that too
0
 
CEHJCommented:
That suggests you don't have the latest JDK - it's present in 1.6

Once you've done that you can import the source from the jaxb/runme.jar - on this occasion you *should* unjar it first.

Your first job after that should be to get an xml file other than the one i packaged to work with that software, such that it will unmarshal any txndatafeed xml file into objects
0
 
aman0711Author Commented:
Hi Charles,
           I moved the compiler to 6.0. but still those errors
0
 
CEHJCommented:
You need to add version 1.6 of Java to the project - i can assure you those classes are present there ;-)
0
 
aman0711Author Commented:
Hmmm...
I guess thats what I did :(
I moved my whole project to 6.0 :-(
do we have external jars for those classes?
0
 
CEHJCommented:
>>do we have external jars for those classes?

No. They're in rt.jar (sun)
0
 
aman0711Author Commented:
Hmmm... so guess I am stuck again :-(
let me build a new project with 1.6
0
 
objectsConnect With a Mentor Commented:
did you still want your digester code fixed up? or are you going to use xmlbeans still, or something entirely different again.
you seem to be changing your mind a lot, only going to waster your time doing that.

0
 
aman0711Author Commented:
No no no Mick :)
You weren't online that time and I fixed the last problem but didn't know how to proceed ahead ...
  I want it to be done anyhow :(
  I got all CEHJs code but don't know how to work with it :(
0
 
objectsConnect With a Mentor Commented:
just pick how you want to do it and stop wasting your time switching between methods.  what you are doing is straight forward. You seem to have spent ages going down the xmlbeans route and now for some reason you've stopped and started on a different route. Its just counter productive :)

0
 
aman0711Author Commented:
If u have sm free time mick, then plz help me out finishing it up altogether :)
0
 
objectsCommented:
Its in my intray :)

0
 
aman0711Author Commented:
Thanks Mick :)
Need your help badly.. My manager will really kill me on monday.. After that u won't hear from me :(
0
 
aman0711Author Commented:
I am getting everything in the list but data is little different :(
0
 
CEHJCommented:
Well you already have a working api (which i've tested) so why you'd want to try and make another using an api which is completely unsuited to the massive datesets you have is beyond me. Switching apis on the grounds of not being able to work with your IDE is not a very smart idea.
0
 
aman0711Author Commented:
Hey Charles,

                   Honestly I have no clue where I am. I wanted that thing to be done and with my skill sets its impossible for me to achieve. I got you online during my day time and Mick comes online at night. lol

                    So Charles, if one gets the JDK6, that api is all set to roll?
0
 
CEHJCommented:
>>if one gets the JDK6, that api is all set to roll?

Yes. I've tested it with a trimmed down version of the xml input
0
 
objectsConnect With a Mentor Commented:
so did you decide to use xmlbeans instead?

0
 
aman0711Author Commented:
No I didnt Mick..
I worked on Commons digester all weekend... couldnt get the xmlbeans to work on my comp at all :-(
0
 
aman0711Author Commented:
Please help me out after that code Mick.. :(
0
 
aman0711Author Commented:
if you dont see my message after tomorrow then take it that my manager killed me :p lol
0
 
CEHJCommented:
My solution uses JAXB, which is in the latest JDK, not XMLBeans
0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.