[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 393
  • Last Modified:

Need help with Commons Digester for XML parsing

Hi Experts,
                   I just started with a sample application for XML parsing using Commons Digester. but i couldnt complie the very first program. I know i am missing the rules for XML file, and I dont know how to declare it and where to keep that XML rules file.
                  The tutorial is located at:

                 http://www.theserverside.com/tt/articles/article.tss?l=Digester

Till now I have coded, Listing 7-1, 7-2, 7-3

                 
0
aman0711
Asked:
aman0711
  • 17
  • 11
  • 10
2 Solutions
 
CEHJCommented:
Please post errors
0
 
aman0711Author Commented:
Hi CEHJ :-)
                      I was actually waiting for your response on this.

                       till now I made two java files. Student.java and DigestStudents.java along with students.xml

                      on executing DigestStudent.xml, I am getting the following error:
 
         

java.lang.IllegalArgumentException: InputStream to parse is null
at org.apache.commons.digester.Digester.parse(Digester.java:1882)
	at com.KeyNote.DigestStudents.digest(DigestStudents.java:38)
	at com.KeyNote.DigestStudents.main(DigestStudents.java:15)

Open in new window

0
 
CEHJCommented:
Make sure you're passing it the file properly. Best post the code in the snippet window
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
aman0711Author Commented:
These are the two files CEHJ, how and where do I declare the rules?

package com.KeyNote;
 
import java.util.Vector;
import org.apache.commons.digester.Digester;
 
public class DigestStudents {
    Vector students;
 
    public DigestStudents() {
        students= new Vector();
    }
 
    public static void main(String[] args) {
        DigestStudents digestStudents = new DigestStudents();
        digestStudents.digest();
    }
 
    private void digest() {
        try {
            Digester digester = new Digester();
            //Push the current object onto the stack
            digester.push(this);
 
            //Creates a new instance of the Student class
            digester.addObjectCreate( "students/student", Student.class );
 
            //Uses setName method of the Student instance
            //Uses tag name as the property name
            digester.addBeanPropertySetter( "students/student/name");
 
            //Uses setCourse method of the Student instance
            //Explicitly specify property name as 'course'
            digester.addBeanPropertySetter( "students/student/course", "course" );
 
            //Move to next student
            digester.addSetNext( "students/student", "addStudent" );
 
            DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                                .getClassLoader()
                                .getResourceAsStream("students.xml"));
 
            //Print the contents of the Vector
            System.out.println("Students Vector "+ds.students);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
 
    public void addStudent( Student stud ) {
        //Add a new Student instance to the Vector
        students.add( stud );
    }
}
 
 
*********************************************************************
 
 
package com.KeyNote;
 
public class Student {
    private String name;
    private String course;
 
    public Student() {
    }
 
    public String getName() {
        return name;
    }
 
    public void setName(String newName) {
        name = newName;
    }
 
    public String getCourse() {
        return course;
    }
 
    public void setCourse(String newCourse) {
        course = newCourse;
    }
    public String toString() {
        return("Name="+this.name + " & Course=" +  this.course);
    }
}

Open in new window

0
 
CEHJCommented:
You're declaring the rules inline by the looks. Try
.getResourceAsStream("/students.xml")); // Make sure that file is in the package root's parent, i.e. in the classpath

Open in new window

0
 
aman0711Author Commented:
Hmmm... I am using Eclipse CEHJ, how do I put that file in classpath
0
 
CEHJCommented:
Should be OK to put it in the project folder.
0
 
aman0711Author Commented:
OK, WILL DO IT RIGHT NOW
0
 
aman0711Author Commented:
sorry for the caps :)

0
 
aman0711Author Commented:
Hi CEHJ,
                  I put the XML file in the prject folder but still got the following error.

java.lang.IllegalArgumentException: InputStream to parse is null
	at org.apache.commons.digester.Digester.parse(Digester.java:1882)
	at com.KeyNote.DigestStudents.digest(DigestStudents.java:38)
	at com.KeyNote.DigestStudents.main(DigestStudents.java:15)

Open in new window

0
 
CEHJCommented:
Can you post the source file?
0
 
CEHJCommented:
I mean the xml
0
 
aman0711Author Commented:
Here it is

<?xml version="1.0" ?> 
- <students>
- <student>
  <name>Java Boy</name> 
  <course>JSP</course> 
  </student>
- <student>
  <name>Java Girl</name> 
  <course>EJB</course> 
  </student>
  </students>

Open in new window

0
 
objectsCommented:
put your code back to how it was

            DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                                .getClassLoader()
                                .getResourceAsStream("students.xml"));


and put your xml file in the same directory as your .java file

0
 
CEHJCommented:
Don't mix up resource files with your sources  - it's bad practice. Place it in the root as i mentioned or a resources folder of the root. If the former, i apologise for a typo earlier. Should be
DigestStudents ds = (DigestStudents) digester.parse(this.getClass().getResourceAsStream("/students.xml"));

Open in new window

0
 
objectsCommented:
You can't put it in the root of the project it won't get found (without changing your code), it needs to go in a source folder (similar to your classes). If you don't want to mix your clases with your xml then just create a separate source folder. Just putting it in the same folder in the class is the simplest for you to get it to work, then you can organise you files as you want.
But theres no need to really change your source its already fine, and keeping your xml in a similar structure to the classes that use them can be useful for organising them instead of just dumping them all in /.
You wouldn't put all your classes in the default package and the same applies for configuration.

Try moving it as I suggested and let me know if that works. We can then create a new source folder for it then if you want.

0
 
CEHJCommented:
>>Place it in the root as i mentioned or a resources folder of the root

If you'd like to get it right straight off, create an /xml folder off the project root. Then you can do


DigestStudents ds = (DigestStudents) digester.parse(this.getClass().getResourceAsStream("/xml/students.xml"));

Open in new window

0
 
objectsCommented:
> If you'd like to get it right straight off, create an /xml folder off the project root. Then you can do

I already suggested that

0
 
aman0711Author Commented:
Thanks folks.. let me try all your suggestions right now and get back to you
0
 
aman0711Author Commented:
@Objects, CEHJ,
 
                          same old code. Kept the XML file in the same folder as my .java files. still the same error
0
 
objectsCommented:
thats strange, try:

            DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                                .getResourceAsStream("students.xml"));

0
 
aman0711Author Commented:
Hmmm.. this time a diff error :)



org.xml.sax.SAXParseException: Content is not allowed in prolog.
	at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown Source)
	at org.apache.commons.digester.Digester.parse(Digester.java:1887)
	at com.KeyNote.DigestStudents.digest(DigestStudents.java:38)
	at com.KeyNote.DigestStudents.main(DigestStudents.java:15)

Open in new window

0
 
objectsCommented:
excellent, its parsing your xml now :)
The error now is because it does not like the content. Check the xml file
0
 
aman0711Author Commented:
At last.. took me one complete day :)
wouldnt have been possible without help.

XML seems to be fine Objects, I am attaching that here.

One more question, can you tell me the logic for this program. Esp. the DigestStudent.java

Student.java I kinda figured out as a bean with getter and setters for all the parent nodes. am I right on this? :)

<?xml version="1.0" ?> 
- <students>
- <student>
  <name>Java Boy</name> 
  <course>JSP</course> 
  </student>
- <student>
  <name>Java Girl</name> 
  <course>EJB</course> 
  </student>
  </students>

Open in new window

0
 
objectsCommented:
are those - signs in the file or are they just from copying it from browser?
check there aren't any  characters at the beginning of the file befire the <?

0
 
objectsCommented:
Yes Student is a bean
DigestStudents is just the class that does the work out stores the parsed data in its Vector

0
 
aman0711Author Commented:
wow Worked :-)
Objects I want a resume like yours :) maybe in another 40 years :P

Now my task is to parse a huge 160 mb XML file, and store data in Oracle. Just follow the same code?
0
 
objectsCommented:
haven't used it for big files but should do the trick, stax would be another to look at for that sort of processing.
0
 
aman0711Author Commented:
Hmmmm.. Yeah someone suggested me Stax but I didnt find a single detailed tutorial on that.
If you know any tutorial on that then please send it to me Objects :)
0
 
CEHJCommented:
>>
At last.. took me one complete day :)
wouldnt have been possible without help.
>>

Good - glad i could help


>>Student.java I kinda figured out as a bean with getter and setters for all the parent nodes. am I right on this? :)

Yes that's fine
0
 
aman0711Author Commented:
Hi CEHJ,
                   I forgot to ask you one more thing. The XML file I am going to parse , all its parents nodes as well as children have attributes. Where as this sample XML doesnt have any atrributes,, How would I take care of those attributes
0
 
CEHJCommented:
You can use org.apache.commons.digester.CallParamRule

It depends on what you want to do with the attribute
0
 
aman0711Author Commented:
Hmmm..  Actually this question has already got quite big.. let me post that as a seperate question.
0
 
aman0711Author Commented:
Thanks :-)
0
 
CEHJCommented:
:-)
0
 
objectsCommented:
hehe :)  glad I could sort it out for you. Feel free to contact me for assistance in the future.

0
 
aman0711Author Commented:
Thanks Objects :)
Will surely do, i was even looking at your tutoring module :-)
0
 
objectsCommented:
good stuff, drop me a message if you have any questions or anything that you cannot find.
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 17
  • 11
  • 10
Tackle projects and never again get stuck behind a technical roadblock.
Join Now