fean
fean

Reputation: 546

Retrieve namespace value of XML using digester

I am trying to extract information from a XML file and able to extract values without its properties.

Code:

public class NRusEntity {
    private String code;
    private String name;
    private String saltForm;

    getters and setters
    ...

Parser Class:

       ...
        String filePath = FileUtility.getOwlFilePath();                     
        try {
            Digester digester = new Digester();
            digester.setValidating(false);

            //digester.setNamespaceAware(true);

            digester.addObjectCreate("rdf:RDF", NRus.class);
            digester.addObjectCreate("rdf:RDF/owl:Class", NRusEntity.class);

            digester.addCallMethod("rdf:RDF/owl:Class/Preferred_Name", "setName", 0);
            digester.addCallMethod("rdf:RDF/owl:Class/code", "setCode", 0);

            /**This commented part creates exception*/ 

            //digester.addCallMethod("rdf:RDF/owl:Class/Has_Salt_Form", "setSaltForm", 2);
            //digester.addCallParam("rdf:RDF/owl:Class/Has_Salt_Form", 0);
            //digester.addCallParam("rdf:RDF/owl:Class/Has_Salt_Form", 1, "rdf:resource");


            digester.addSetNext("rdf:RDF/owl:Class", "addEntry");
            File input = new File(filePath);
            digester.parse(input);
        } 
        ...

XML Looks like this:

<?xml version="1.0"?>

<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
 xmlns:owl="http://www.w3.org/2002/07/owl#">

    <owl:Class rdf:about="#z">
        <Preferred_Name rdf:datatype="http://www.w3.org/2001/XMLSchema#string">von</Preferred_Name>
        <code rdf:datatype="http://www.w3.org/2001/XMLSchema#string">XY221</code>
        <Has_Format rdf:resource="http://zlib.com#Ni_Hydro"/>
    </owl:Class>
    ...
</rdf:RDF>

How can I extract the URI value

"http://zlib.com#Ni_Hydro" 

from that XML line

<Has_Format rdf:resource="http://zlib.com#Ni_Hydro"/>

Upvotes: 0

Views: 184

Answers (1)

Barney
Barney

Reputation: 2836

I can't tell exactly as your XML does not appear to quite match your code: the commented out code refers to a Has_Salt_Form element but the rdf:resource element appears on a Has_Format element. However, I can see one potential problem which may help you progress:

I'm assuming your NRusEntity class setter is something like:

public void setSaltForm(String saltForm) {
  // assign saltForm, or whatever...
}

However, the digester code you have is:

digester.addCallMethod("rdf:RDF/owl:Class/Has_Salt_Form", "setSaltForm", 2);
digester.addCallParam("rdf:RDF/owl:Class/Has_Salt_Form", 0);
digester.addCallParam("rdf:RDF/owl:Class/Has_Salt_Form", 1, "rdf:resource");

This is looking for a setSaltForm method with two parameters (the first is the element body, the second the rdf:resource attribute), so will not match the simple setter, and you'll get something like "no such method" in the exception message.

So if you need the body content then try adding another set method:

public void setSaltForm(String content, String attrib) {
  // content will have the element content
  // attrib will have "http://zlib.com#Ni_Hydro"
}

Or if you don't need the content then drop it from the digester rules:

digester.addCallMethod("rdf:RDF/owl:Class/Has_Salt_Form", "setSaltForm", 1);
digester.addCallParam("rdf:RDF/owl:Class/Has_Salt_Form", 0, "rdf:resource");

If neither of those work can you add details of the version of digester you are using, and the exception you get.

Upvotes: 1

Related Questions