如何使用Java中的XPath訪問OWL文檔?我有一個(gè)XML文件形式的OWL文檔。我想從這個(gè)文檔中提取元素。我的代碼適用于簡單的XML文檔,但它不適用于OWL XML文檔。我實(shí)際上是想獲得這個(gè)元素:/rdf:RDF/owl:Ontology/rdfs:label為此我做了這個(gè): DocumentBuilder builder = builderfactory.newDocumentBuilder();
Document xmlDocument = builder.parse(
new File(XpathMain.class.getResource("person.xml").getFile()));
XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();
XPath xPath = factory.newXPath();
XPathExpression xPathExpression = xPath.compile("/rdf:RDF/owl:Ontology/rdfs:label/text()");
String nameOfTheBook = xPathExpression.evaluate(xmlDocument,XPathConstants.STRING).toString();我也試過這樣只提取rdfs:label元素: XPathExpression xPathExpression = xPath.compile("//rdfs:label");
NodeList nodes = (NodeList) xPathExpression.evaluate(xmlDocument, XPathConstants.NODESET);但是這個(gè)節(jié)點(diǎn)列表是空的。請(qǐng)讓我知道我哪里出錯(cuò)了。我正在使用Java XPath API。
如何使用Java中的XPath訪問OWL文檔?
拉風(fēng)的咖菲貓
2019-08-28 15:04:54