使用xml文件中的jena创建RDF三元组和RDF存储

时间:2012-04-22 21:31:33

标签: java xml rdf semantic-web jena

我使用以下java程序从xml文件中提取信息。

import java.io.File;
import java.net.URL;

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException; 

public class ExtractInfo {

public static void main(String argv []) {
    try {
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
        File file = new File("page.xml");
        Document doc = docBuilder.parse(file);

        // normalize text representation
        doc.getDocumentElement().normalize();
        System.out.println ("Root element of the doc is " + 
            doc.getDocumentElement().getNodeName());

        NodeList listOfPersons = doc.getElementsByTagName("person");
        int totalPersons = listOfPersons.getLength();
        System.out.println("Total no of people : " + totalPersons);

        for (int s=0; s<listOfPersons.getLength(); s++) {
            Node firstPersonNode = listOfPersons.item(s);
            if (firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {
                Element firstPersonElement = (Element)firstPersonNode;
                //-------
                NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                Element firstNameElement = (Element)firstNameList.item(0);

                NodeList textFNList = firstNameElement.getChildNodes();
                System.out.println("First Name : " + 
                       ((Node)textFNList.item(0)).getNodeValue().trim());
                //-------
                NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
                Element lastNameElement = (Element)lastNameList.item(0);

                NodeList textLNList = lastNameElement.getChildNodes();
                System.out.println("Last Name : " + 
                       ((Node)textLNList.item(0)).getNodeValue().trim());
                //----
                NodeList ageList = firstPersonElement.getElementsByTagName("age");
                Element ageElement = (Element)ageList.item(0);

                NodeList textAgeList = ageElement.getChildNodes();
                System.out.println("Age : " + 
                    ((Node)textAgeList.item(0)).getNodeValue().trim());
            }
        }
    } catch (SAXParseException err) {
        System.out.println ("** Parsing error" + ", line " 
            + err.getLineNumber () + ", uri " + err.getSystemId());
        System.out.println(" " + err.getMessage());
    } catch (SAXException e) {
        Exception x = e.getException ();
        ((x == null) ? e : x).printStackTrace();
    } catch (Throwable t) {
        t.printStackTrace();
    }
}

}

有人可以帮助我从提取的信息中生成RDF三元组,使用包含所有三元组的Jena 创建一个三元组商店。我对RDF和Jena都很陌生,所以我确实需要你的帮助, 提前致谢。

1 个答案:

答案 0 :(得分:0)

Resource resource=OntModel.createResourc(NameSpace+"Doutorado_em_Engenharia_de_Sistemas_e_Computacao");
Property prop=OntModel.createProperty(http://www.owl-ontologies.com/OntologyBase.owl#program_Provided_By);
Resource obj=OntModel.createResource(NameSpace+"Universidade_X");
OntMode.add(resource,prop,obj);

在应用它之前,您应该首先为您的本体创建一个OntModel实例。

http://answers.semanticweb.com/questions/11084/add-triples-in-an-ontology-using-jena-api