使用xml / rdf文档中的xpath检索元素数据

时间:2013-05-27 07:38:27

标签: java dom xpath saxparser

我有以下rdf / xml数据,我想获取元素rtc_cm:Category的值https://test.yahoo.com:9443/ccm/resource/itemOid/com.Category/_ivX6csaREeK41JJsvCkJ_A

<rdf:RDF>
  <rdf:Description rdf:about="https://server:9443/ccm/oslc/categories">
    <rdfs:member>
      <rtc_cm:Category rdf:about="https://server:9443/ccm/resource/itemOid/com.Category/_ivX6csaREeK41JJsvCkJ_A">
        <rtc_cm:hierarchicalName rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Test 2</rtc_cm:hierarchicalName>
      </rtc_cm:Category>
    </rdfs:member>
    <rdfs:member>
      <rtc_cm:Category rdf:about="https://server:9443/ccm/resource/itemOid/com.Category/_iV5DcMaREeK41JJsvCkJ_A">
        <rtc_cm:hierarchicalName rdf:datatype="http://www.w3.org/2001/XMLSchema#string">Unassigned</rtc_cm:hierarchicalName>
      </rtc_cm:Category>
    </rdfs:member>
  </rdf:Description>
  <oslc:ResponseInfo rdf:about="https://server:9443/ccm/oslc/categories?oslc.select=rtc_cm:hierarchicalName&oslc.where=rtc_cm:projectArea=%22_fE-FT8aREeK41JJsvCkJ_A%22">
    <dcterms:title>Categories</dcterms:title>
    <oslc:totalCount>2</oslc:totalCount>
  </oslc:ResponseInfo>
</rdf:RDF>

1 个答案:

答案 0 :(得分:2)

使用RDF / XML数据时,我不建议使用XPath来定位元素。 RDF / XML不是普通的XML文件,实际结构可能因服务器端库确定的是在RDF / XML中表达内容的最佳方式而有所不同。我的建议是利用客户端库来获取RDF / XML格式,并利用其API来查找元素(节点)。即使用诸如Apache Jena之类的库并使用以下形式的调用:

Property rdfTypeProp = model.getProperty(
  "http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
Resource categoryNode = model.getResource(
  "https://jazz.net/xmlns/prod/jazz/rtc/cm/1.0/Category");
StmtIterator categoryIterator = model.listStatements(
  new SimpleSelector(null, rdfTypeProp , categoryNode));

请注意,semanticweb.com上有similar thread