commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Croci Francesco (ID)" <fcr...@id.ethz.ch>
Subject New to digester and need help
Date Mon, 16 Mar 2009 08:18:49 GMT
Hi to all, 

I'm new to digester and I have the following problem (a little long but
it is all code...): 

I have such an XML File to parse: 

<?xml version='1.0' encoding='UTF-8'?> 
<OAI-PMH xmlns='http://www.openarchives.org/OAI/2.0/'
<http://www.openarchives.org/OAI/2.0/%27>
xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'
<http://www.w3.org/2001/XMLSchema-instance%27> 
  xsi:schemaLocation='http://www.openarchives.org/OAI/2.0/
<http://www.openarchives.org/OAI/2.0/>
http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd'
<http://www.openarchives.org/OAI/2.0/OAI-PMH.xsd%27> > 
  <responseDate>2009-03-12T14:00:14Z</responseDate> 
  <request verb="ListRecords" metadataPrefix="oai_dc"
set="2009">http://url.url.org/abc.asp <http://url.url.org/abc.asp>
</request> 
  <ListRecords> 
    <record> 
      <header> 
        <identifier>oai:oai.ethz.ch:e-collection/005583701</identifier> 
        <datestamp>2008-12-08</datestamp> 
        <setSpec>2009</setSpec> 
      </header> 
      <metadata> 
        <oai_dc:dc
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
<http://www.openarchives.org/OAI/2.0/oai_dc/>
http://www.openarchives.org/OAI/2.0/oai_dc.xsd
<http://www.openarchives.org/OAI/2.0/oai_dc.xsd> "
xmlns="http://purl.org/dc/elements/1.1/
<http://purl.org/dc/elements/1.1/> "
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance
<http://www.w3.org/2001/XMLSchema-instance> "
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/
<http://www.openarchives.org/OAI/2.0/oai_dc/> "> 
                <title>Universita?res Wissen teilen Forschende im Dialog
</title> 
        <creator>Ruegger, Hans-Ulrich </creator> 
        <type>text</type> 
        <publisher>Zuerichvdf Hochschulverlag an der ETH
Zuerich</publisher> 
        <date>2009</date> 
        <language>ger</language> 
        <subject>Wissenschaft</subject> 
        <subject>Forschung</subject> 
        <subject>Kooperation</subject> 
        <subject>Wissenstransfer</subject> 
        <identifier>http://url.url.url.org/blabla
<http://url.url.url.org/blabla> </identifier> 
        <identifier>http://url.url.url.org/blabla.pdf
<http://url.url.url.org/blabla.pdf> </identifier> 
        <identifier></identifier> 
        </oai_dc:dc> 
      </metadata> 
    </record> 
    ...other records 
  </ListRecords> 
</OAI-PMH>

 

Then I wrote my parser: 



First version without rules config file
...

    public ListRecordsParser() throws IOException, SAXException

    {

        // instantiate Digester and disable XML validation

        Digester digester = new Digester();

        digester.setValidating(false);

        

        // instantiate ListRecords class

        digester.addObjectCreate("OAI-PMH/ListRecords",
ListRecords.class );

        

        // instantiate Record class

        digester.addObjectCreate("OAI-PMH/ListRecords/record",
Record.class );

                

        // instantiate Header class

        digester.addObjectCreate("OAI-PMH/ListRecords/record/header",
Header.class );

        digester.addBeanPropertySetter(
"OAI-PMH/ListRecords/record/header/identifier", "identifier" );

        digester.addBeanPropertySetter(
"OAI-PMH/ListRecords/record/header/datestamp", "dateStamp" );

        digester.addBeanPropertySetter(
"OAI-PMH/ListRecords/record/header/setSpec", "setSpec" );

        

  digester.addSetNext( "OAI-PMH/ListRecords/record", "setHeader" );

 

        // instantiate MetaData class

        digester.addObjectCreate("OAI-PMH/ListRecords/record/metadata",
MetaData.class );

        digester.addBeanPropertySetter(
"OAI-PMH/ListRecords/record/metadata/title", "title" );

        digester.addBeanPropertySetter(
"OAI-PMH/ListRecords/record/metadata/creator", "creator" );

        digester.addBeanPropertySetter(
"OAI-PMH/ListRecords/record/metadata/publisher", "publisher" );

        

        digester.addSetNext( "OAI-PMH/ListRecords/record", "setMetaData"
);

        

        digester.addSetNext( "OAI-PMH/ListRecords", "addRecord" );

                

        File inputFile = new File("OAI.xml");

     

        // now that rules and actions are configured, start the parsing
process

        ListRecords lr = (ListRecords)digester.parse(inputFile);

    

        log.debug("Documents list: " + lr.toString());

...

 

With this version I get the following error:

...

2009-03-16 09:12:07,225 [main] DEBUG
org.apache.commons.digester.Digester(org.apache.commons.digester.SetNext
Rule.end(SetNextRule.java:195))  -
[SetNextRule]{OAI-PMH/ListRecords/record} Call
ethz.oai.helper.ListRecords.setMetaData(null

--- Header ---

null

--- MetaData ---

null

)

2009-03-16 09:12:07,225 [main] ERROR
org.apache.commons.digester.Digester(org.apache.commons.digester.Digeste
r.endElement(Digester.java:1334))  - End event threw exception

java.lang.NoSuchMethodException: No such accessible method:
setMetaData() on object: ethz.oai.helper.ListRecords

      at
org.apache.commons.beanutils.MethodUtils.invokeMethod(MethodUtils.java:2
14)

      at
org.apache.commons.digester.SetNextRule.end(SetNextRule.java:217)

      at org.apache.commons.digester.Rule.end(Rule.java:253)

      at
org.apache.commons.digester.Digester.endElement(Digester.java:1332)

      at org.apache.xerces.parsers.AbstractSAXParser.endElement(Unknown
Source)

      at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanEndElement(Unk
nown Source)

      at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDis
patcher.dispatch(Unknown Source)

      at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkno
wn Source)

      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)

      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)

      at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)

      at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
Source)

      at
org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)

      at org.apache.commons.digester.Digester.parse(Digester.java:1842)

      at
ethz.oai.helper.ListRecordsParser.<init>(ListRecordsParser.java:62)

      at
ethz.oai.MetaDataCollector.getDocumentsData(MetaDataCollector.java:129)

      at
ethz.test.MetaDataCollectorTest.testGetMetaData(MetaDataCollectorTest.ja
va:57)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)

      at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)

      at java.lang.reflect.Method.invoke(Method.java:597)

      at
org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethod
Runner.java:99)

      at
org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRun
ner.java:81)

      at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAf
terRunner.java:34)

      at
org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.j
ava:75)

      at
org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45
)

      at
org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestC
lassMethodsRunner.java:66)

      at
org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRu
nner.java:35)

      at
org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRun
ner.java:42)

      at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAf
terRunner.java:34)

      at
org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)

      at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4Tes
tReference.java:45)

      at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.ja
va:38)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:460)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:673)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
ner.java:386)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
nner.java:196)

 

Second version with rules config file:

 

Config file:

 

<?xml version="1.0" encoding="UTF-8"?>

<digester-rules>

   <object-create-rule pattern="OAI-PMH/ListRecords" classname="
ListRecords" />

   

   <pattern value="OAI-PMH/ListRecords/record">

      <object-create-rule classname=" Record" />

        <pattern value="OAI-PMH/ListRecords/record/header">

            <object-create-rule classname=" Header" />

                <bean-property-setter-rule pattern="identifier"/>

                <bean-property-setter-rule pattern="dateStamp"/>

                <bean-property-setter-rule pattern="setSpec"/>

        </pattern>

        <set-next-rule methodname="setHeader" />

        <pattern value="OAI-PMH/ListRecords/record/metadata">

            <object-create-rule classname=" MetaData" />

                <bean-property-setter-rule pattern="title"/>

                <bean-property-setter-rule pattern="creator"/>

                <bean-property-setter-rule pattern="publisher"/>

        </pattern>

        <set-next-rule methodname="setMetaData" />

   </pattern>

   <set-next-rule methodname="addRecord" />

</digester-rules>

 

 

Parser:

 

    public ListRecordsParser(InputStream response) throws IOException,
SAXException

    {

        

        try

        {

            File inputFile = new File("OAI.xml");

            File digesterRules = new File("OAIDigesterRules.xml");

            

            boolean a = inputFile.exists();

            boolean b = digesterRules.exists();

            

            if(inputFile.exists() && digesterRules.exists())

            {

                Digester digester = new
DigesterLoader().createDigester(digesterRules.toURI().toURL());

                

                // now that rules and actions are configured, start the
parsing process

                ListRecords lr = (ListRecords)digester.parse(inputFile);

 

                log.debug("Documents list: " + lr.toString());

            }

        }

        catch (Exception e)

        {

            // TODO Auto-generated catch block

            log.error(e.getMessage());

        }

    }

 

And with this version I get:

 

2009-03-16 09:14:48,008 [main] ERROR
org.apache.commons.digester.Digester(org.apache.commons.digester.Digeste
r.startElement(Digester.java:1565))  - Begin event threw exception

java.lang.ClassNotFoundException: ListRecords

      at java.net.URLClassLoader$1.run(URLClassLoader.java:200)

      at java.security.AccessController.doPrivileged(Native Method)

      at java.net.URLClassLoader.findClass(URLClassLoader.java:188)

      at java.lang.ClassLoader.loadClass(ClassLoader.java:307)

      at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)

      at java.lang.ClassLoader.loadClass(ClassLoader.java:252)

      at
org.apache.commons.digester.ObjectCreateRule.begin(ObjectCreateRule.java
:205)

      at org.apache.commons.digester.Rule.begin(Rule.java:175)

      at
org.apache.commons.digester.Digester.startElement(Digester.java:1563)

      at
org.apache.xerces.parsers.AbstractSAXParser.startElement(Unknown Source)

      at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanStartElement(U
nknown Source)

      at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl$FragmentContentDis
patcher.dispatch(Unknown Source)

      at
org.apache.xerces.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unkno
wn Source)

      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)

      at org.apache.xerces.parsers.XML11Configuration.parse(Unknown
Source)

      at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)

      at org.apache.xerces.parsers.AbstractSAXParser.parse(Unknown
Source)

      at
org.apache.xerces.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)

      at org.apache.commons.digester.Digester.parse(Digester.java:1842)

      at
ethz.oai.helper.ListRecordsParser.<init>(ListRecordsParser.java:80)

      at
ethz.oai.MetaDataCollector.getDocumentsData(MetaDataCollector.java:129)

      at
ethz.test.MetaDataCollectorTest.testGetMetaData(MetaDataCollectorTest.ja
va:57)

      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

      at
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.jav
a:39)

      at
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessor
Impl.java:25)

      at java.lang.reflect.Method.invoke(Method.java:597)

      at
org.junit.internal.runners.TestMethodRunner.executeMethodBody(TestMethod
Runner.java:99)

      at
org.junit.internal.runners.TestMethodRunner.runUnprotected(TestMethodRun
ner.java:81)

      at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAf
terRunner.java:34)

      at
org.junit.internal.runners.TestMethodRunner.runMethod(TestMethodRunner.j
ava:75)

      at
org.junit.internal.runners.TestMethodRunner.run(TestMethodRunner.java:45
)

      at
org.junit.internal.runners.TestClassMethodsRunner.invokeTestMethod(TestC
lassMethodsRunner.java:66)

      at
org.junit.internal.runners.TestClassMethodsRunner.run(TestClassMethodsRu
nner.java:35)

      at
org.junit.internal.runners.TestClassRunner$1.runUnprotected(TestClassRun
ner.java:42)

      at
org.junit.internal.runners.BeforeAndAfterRunner.runProtected(BeforeAndAf
terRunner.java:34)

      at
org.junit.internal.runners.TestClassRunner.run(TestClassRunner.java:52)

      at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4Tes
tReference.java:45)

      at
org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.ja
va:38)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:460)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTe
stRunner.java:673)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRun
ner.java:386)

      at
org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRu
nner.java:196)

2009-03-16 09:14:48,008 [main] ERROR
ethz.oai.helper.ListRecordsParser(ethz.oai.helper.ListRecordsParser.<ini
t>(ListRecordsParser.java:88))  - Error at line 5 char 16: ListRecords

 

Can someone tell me what I miss in both cases, please?

 

Thanx

Francesco

 

 

 

Here the various objects classes:

 

public class ListRecords

{

    /**

     * 

     */

    private Vector<Record> records;

 

    /**

     * @param record the record to add

     */

    public void addRecord(Record record)

    {

        if(record != null)

        {

            this.records.addElement(record);

        }

    }

 

    /**

     * @return the records

     */

    public Vector<Record> getRecord()

    {

        return this.records;

    }

 

    public String toString()

    {

        String newline = System.getProperty("line.separator");

        StringBuffer buf = new StringBuffer();

 

        buf.append("--- Record ---").append(newline);

        for(Record record : this.records)

        {

            buf.append(record).append(newline);

        }

 

        return buf.toString();

    }

}

 

 

public class Record

{

    /**

     * 

     */

    private Header header;

    /**

     * 

     */

    private MetaData metaData;

 

    /**

     * @param header the header to set

     */

    public void setHeader(Header header)

    {

        this.header = header;

    }

 

    /**

     * @return the header

     */

    public Header getHeader()

    {

        return header;

    }

 

    /**

     * @param metaData the metaData to set

     */

    public void setMetaData(MetaData metaData)

    {

        this.metaData = metaData;

    }

 

    /**

     * @return the metaData

     */

    public MetaData getMetaData()

    {

        return metaData;

    }

 

    public String toString()

    {

        String newline = System.getProperty("line.separator");

        StringBuffer buf = new StringBuffer();

        

        buf.append(header).append(newline);

        buf.append("--- Header ---").append(newline);

        buf.append(header).append(newline);

        

 

        buf.append("--- MetaData ---").append(newline);

        buf.append(metaData).append(newline);

        

        return buf.toString();

    }

}

 

 

public class Header

{

    /**

     * 

     */

    private String identifier;

    /**

     * 

     */

    private String dateStamp;

    /**

     * 

     */

    private String setSpec;

    

    

    /**

     * @return the identifier

     */

    public String getIdentifier()

    {

        return identifier;

    }

    

    /**

     * @param identifier the identifier to set

     */

    public void setIdentifier(String identifier)

    {

        this.identifier = identifier;

    }

    

    /**

     * @return the dateStamp

     */

    public String getDateStamp()

    {

        return dateStamp;

    }

    

    /**

     * @param dateStamp the dateStamp to set

     */

    public void setDateStamp(String dateStamp)

    {

        this.dateStamp = dateStamp;

    }

    

    /**

     * @return the setSpec

     */

    public String getSetSpec()

    {

        return setSpec;

    }

    

    /**

     * @param setSpec the setSpec to set

     */

    public void setSetSpec(String setSpec)

    {

        this.setSpec = setSpec;

    }

    

    public String toString()

    {

        String newline = System.getProperty("line.separator");

        StringBuffer buf = new StringBuffer();

        final String TAB = "    ";

 

        buf.append("Header ( ");

        buf.append("identifier = ").append(this.identifier).append(TAB);

        buf.append("dateStamp = ").append(this.dateStamp).append(TAB);

        buf.append("setSpec = ").append(this.setSpec).append(TAB);

        buf.append(" )").append(newline);

        

        return buf.toString();

    }

}

 

 

public class MetaData

{

    /**

     * 

     */

    private String title;

    /**

     * 

     */

    private String creator;

    /**

     * 

     */

    private String publisher;

    

    

    /**

     * @return the title

     */

    public String getTitle()

    {

        return title;

    }

    

    /**

     * @param title the title to set

     */

    public void setTitle(String title)

    {

        this.title = title;

    }

    

    /**

     * @return the creator

     */

    public String getCreator()

    {

        return creator;

    }

    

    /**

     * @param creator the creator to set

     */

    public void setCreator(String creator)

    {

        this.creator = creator;

    }

    

    /**

     * @return the publisher

     */

    public String getPublisher()

    {

        return publisher;

    }

    

    /**

     * @param publisher the publisher to set

     */

    public void setPublisher(String publisher)

    {

        this.publisher = publisher;

    }

 

    public String toString()

    {

        String newline = System.getProperty("line.separator");

        StringBuffer buf = new StringBuffer();

        final String TAB = "    ";

 

        buf.append("MetaData ( ");

        buf.append("title = ").append(this.title).append(TAB);

        buf.append("creator = ").append(this.creator).append(TAB);

        buf.append("publisher = ").append(this.publisher).append(TAB);

        buf.append(" )").append(newline);

        

        return buf.toString();

    }

}

 

 

 


Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message