Solved

Problem with commons digester for XML parsing

Posted on 2009-05-14
52
280 Views
Last Modified: 2013-11-23
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

0
Comment
Question by:aman0711
  • 25
  • 21
  • 6
52 Comments
 
LVL 10

Author Comment

by:aman0711
ID: 24387882
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24389893
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
 
LVL 10

Author Comment

by:aman0711
ID: 24390066
I want to use it Charles. but i dont know where to start with it?

just follow any xmlBeans tutorial? that would do?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24390119
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
 
LVL 10

Author Comment

by:aman0711
ID: 24390128
Hi Charles,
                   I am a little confused here. main class? of jaxb? or xmlBeans?
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 250 total points
ID: 24390455
No. The main class that serves as a demo entry point for the whole TXNDATA api.
0
 
LVL 92

Expert Comment

by:objects
ID: 24390654
>                   I am a little confused here. main class? of jaxb? or xmlBeans?

I'm not surprised :)

0
 
LVL 92

Expert Comment

by:objects
ID: 24390660
looks like you are fairly close though, will have a look at it today for you.

0
 
LVL 10

Author Comment

by:aman0711
ID: 24390841
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
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 24392856
Have a look at the javadoc for the TXNDATA api i sent you. Its all in one jar
0
 
LVL 10

Author Comment

by:aman0711
ID: 24394438
oh I thought you sent me the edited schema.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24394450
I sent you (or gave you a link to) a complete API with javadoc and test main class
0
 
LVL 10

Author Comment

by:aman0711
ID: 24394637
I am Sorry Charles, I guess I didnt get it or just overlooked. Will go through your email again
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24394984
It could be in one of the questions actually
0
 
LVL 10

Author Comment

by:aman0711
ID: 24395299
Ohk. just got in the office, will go through the questions.
Thanks Charles :-)
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 250 total points
ID: 24395400
Here it is again. Start with the TXNDATAFEED Javadoc

https://filedb.experts-exchange.com/incoming/ee-stuff/7639-jaxb-TXNDATAFEED.jar
0
 
LVL 10

Author Comment

by:aman0711
ID: 24395545
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24395915
Yes, should be fine to import
0
 
LVL 10

Author Comment

by:aman0711
ID: 24395935
Cool. Thanks :-)
Let me import and get back to you
0
 
LVL 10

Author Comment

by:aman0711
ID: 24396458
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
 
LVL 10

Author Comment

by:aman0711
ID: 24396718
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24396756
>>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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24396777
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24396801
TXNDATAFEED-bin.jar is what you need to add, together with the commons jars
0
 
LVL 10

Author Comment

by:aman0711
ID: 24396986
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24397054
>>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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
LVL 10

Author Comment

by:aman0711
ID: 24397137

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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24397271
Yes, you want to add the *bin* jar to the project build path
0
 
LVL 10

Author Comment

by:aman0711
ID: 24397308
Cool, so I guess I am close now.
This is where I am. Any other jar missing now?

structure.PNG
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24398556
jaxb is missing - you need to add that too
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24398611
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
 
LVL 10

Author Comment

by:aman0711
ID: 24398662
Hi Charles,
           I moved the compiler to 6.0. but still those errors
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24398785
You need to add version 1.6 of Java to the project - i can assure you those classes are present there ;-)
0
 
LVL 10

Author Comment

by:aman0711
ID: 24398809
Hmmm...
I guess thats what I did :(
I moved my whole project to 6.0 :-(
do we have external jars for those classes?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24398836
>>do we have external jars for those classes?

No. They're in rt.jar (sun)
0
 
LVL 10

Author Comment

by:aman0711
ID: 24398844
Hmmm... so guess I am stuck again :-(
let me build a new project with 1.6
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24400155
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
 
LVL 10

Author Comment

by:aman0711
ID: 24400314
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
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24400436
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
 
LVL 10

Author Comment

by:aman0711
ID: 24400454
If u have sm free time mick, then plz help me out finishing it up altogether :)
0
 
LVL 92

Expert Comment

by:objects
ID: 24400457
Its in my intray :)

0
 
LVL 10

Author Comment

by:aman0711
ID: 24400491
Thanks Mick :)
Need your help badly.. My manager will really kill me on monday.. After that u won't hear from me :(
0
 
LVL 10

Author Comment

by:aman0711
ID: 24401334
I am getting everything in the list but data is little different :(
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24402068
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
 
LVL 10

Author Comment

by:aman0711
ID: 24404952
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 24405300
>>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
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24405444
so did you decide to use xmlbeans instead?

0
 
LVL 10

Author Comment

by:aman0711
ID: 24408810
No I didnt Mick..
I worked on Commons digester all weekend... couldnt get the xmlbeans to work on my comp at all :-(
0
 
LVL 10

Author Comment

by:aman0711
ID: 24408821
Please help me out after that code Mick.. :(
0
 
LVL 10

Author Comment

by:aman0711
ID: 24408834
if you dont see my message after tomorrow then take it that my manager killed me :p lol
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24410046
My solution uses JAXB, which is in the latest JDK, not XMLBeans
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 24645698
:-)
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now