Question

Please help me to know what is wrong with my code.I tried from this link for Sample. OS : Windows 7

Code 1 :

 DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                            .getClassLoader()
                            .getResourceAsStream("leo/test/digester/student/student.xml"));

Exception 1:

true:isFile:isHidden:false    
java.lang.IllegalArgumentException: InputStream to parse is null
at org.apache.commons.digester3.Digester.parse(Digester.java:1621)
at leo.test.digester.student.DigestStudents.digest(DigestStudents.java:41)
at leo.test.digester.student.DigestStudents.main(DigestStudents.java:16)

Code 2 :

 File f = new File ("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml");
 System.out.println(f.isFile()+":isFile:isHidden:"+f.isHidden());
 DigestStudents ds = (DigestStudents) digester.parse(f);

Exception 2:

true:isFile:isHidden:false
log4j:WARN No appenders could be found for logger (org.apache.commons.digester3.Digester).
log4j:WARN Please initialize the log4j system properly.
java.lang.NullPointerException
    at org.apache.commons.digester3.Digester.getXMLReader(Digester.java:790)
    at org.apache.commons.digester3.Digester.parse(Digester.java:1588)
    at org.apache.commons.digester3.Digester.parse(Digester.java:1557)
    at leo.test.digester.student.DigestStudents.digest(DigestStudents.java:41)
    at leo.test.digester.student.DigestStudents.main(DigestStudents.java:16)

Student.java

package leo.test.digester.student;

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);
}
}

student.xml

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

DigestStudents.java

package leo.test.digester.student;

import java.util.Vector;
import org.apache.commons.digester3.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" );

             File f = new File ("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml");
             System.out.println(f.isFile()+":isFile:isHidden:"+f.isHidden());


            DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                                .getClassLoader()
                                .getResourceAsStream("D:/workspace/RD_Axway/src/leo/test/digester/student/student.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 );
    }
}
Was it helpful?

Solution

ClassLoader#getResourceAsStream() is able to locate files relative to the "root" of the classpath. You need to replace this line:

 DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                       .getClassLoader()
                       .getResourceAsStream("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml"));

With

 DigestStudents ds = (DigestStudents) digester.parse(this.getClass()
                       .getClassLoader()
                       .getResourceAsStream("leo/test/digester/student/student.xml"));

OTHER TIPS

I believe this line is problematic:

DigestStudents ds = (DigestStudents) digester.parse(this.getClass().getClassLoader().getResourceAsStream("D:/workspace/RD_Axway/src/leo/test/digester/student/student.xml"));

The resource cannot be found, thus null is being returned.

ClassLoader.getResourceAsStream looks for files in your classpath. Change your getResourceAsStream to:

.getResourceAsStream("student.xml"));

Make sure that you specify the path

D:/workspace/RD_Axway/src/leo/test/digester/student/

in your classpath and then re-compile and run it again.

Both getResource and getResourceAsStream work on URIs relative to the classpath.

You'll need to modify the path to be something along the lines of

getResourceAsStream("/leo/test/digester/student/student.xml");

The whole idea of getting object that loaded our class is to be able to get resources independently from current system setup, from current absolute paths. You get null in getResourceAsStream because file couldn't be found. Just make path to student.xml relative, or absolute to classpath as Kayaman suggested.

this.getClass()
.getClassLoader()
.getResourceAsStream("path/to/file/student.xml")

You get file by classLoader.getResourceAsStream with the same path search order that was used to load current class. Please see javadoc for getResource:

java.lang.ClassLoader.getResource(String name)

Finds the resource with the given name. A resource is some data (images, audio, text, etc) that can be accessed by class code in a way that is independent of the location of the code.

The name of a resource is a '/'-separated path name that identifies the resource.

This method will first search the parent class loader for the resource; if the parent is null the path of the class loader built-in to the virtual machine is searched. That failing, this method will invoke findResource(String) to find the resource.

Thanks to this generalization, code is more portable, doesn't matter if you are on Windows or Linux, or even on some distributed system. More on loading classes and possible sources: java.lang.ClassLoader

Either place the file in current folder or uri format.

//placing the xml file in the current java file folder(which is not goog way of coding.

DigesterTest ds = (DigesterTest) digest.parse(this.getClass().getResourceAsStream(
             "test.xml"));
DigesterTest ds = (DigesterTest) digest.parse("file://exact location of the file");
//created a xml folder under java project 
DigesterTest ds = (DigesterTest) digest.parse(new File("xml/test.xml"));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top