我正在尝试从 XML 文件中提取信息,并且能够在没有其属性的情况下提取值。
代码:
public class NRusEntity {
private String code;
private String name;
private String saltForm;
getters and setters
...
解析器类:
...
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 看起来像这样:
<?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>
如何提取 URI 值
"http://zlib.com#Ni_Hydro"
从那个 XML 行
<Has_Format rdf:resource="http://zlib.com#Ni_Hydro"/>