0

私のコードの何が問題なのかを教えてください。サンプルはこちらのリンクから試してみました。OS:Windows7

コード 1 :

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

例外 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)

コード 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);

例外 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)

学生.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);
}
}

学生.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 );
    }
}
4

6 に答える 6

1

この行は問題があると思います:

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

リソースが見つからないため、null が返されます。

于 2013-09-13T07:33:15.280 に答える
0

ファイルを現在のフォルダーまたは uri 形式で配置します。

//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"));
于 2014-03-13T09:13:57.713 に答える