从rdf文件获取数据 [英] Get data from rdf file

查看:191
本文介绍了从rdf文件获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能存在重复:

Java - 使用Jena APi - 从RDF文件获取数据

我使用Java和Jena API。

我有类 Person 使用数据类型属性 hasFirstName hasLastName hasDateOfBirth hasGender



以下是在RDF文件中表示一个人的方式。

 < rdf:说明rdf:about =http://www.fam.com/FAM#Bruno04/02/1980> 
< j.0:FAMhasGender> H< /j.0:FAMhasGender>
< j.0:FAMhasDateOfBirth> 04/02/1980< /j.0:FAMhasDateOfBirth>
< j.0:FAMhasLastName> DS< /j.0:FAMhasLastName>
< j.0:FAMhasFirstName>布鲁诺< /j.0:FAMhasFirstName>
< / rdf:说明>

我想为每个人获取名字,性别,出生日期和写信息在一个文本文件中。我的问题是,它只写在rdf文件中找到的第一个女人/男人,但有一个以上的女人和男人。

你能解释一下我怎么解决这个问题?
非常感谢。

  ExtendedIterator实例= to.person.listInstances(); 
个体实例= null;
个人firstInstance = null;
while(instances.hasNext()){
instance =(Individual)instances.next();

gen = instance.getPropertyValue(to.hasGender).toString();
fname = instance.getPropertyValue(to.hasFirstName).toString();
dd = instance.getPropertyValue(to.hasDateOfBirth).toString();

writeFile(fname,dd,genr);}


//写入文本文件
public void writeFile(String fn,String dbir,String gn){
String fileout =D:/file1.txt;
字符串firstName = fn;
String dateB = dbir;
String gender = gn;

BufferedWriter out;
try {
out = new BufferedWriter(new FileWriter(fileout,true));

if(gender.equals(F)){
out.write([label = \+ firstName +\\\\
\\\
\\ D.Naiss:+ dnai1 +\,+ shape +]);
} else if(gender.equals(M)){
out.write([label = \+ firstName +\\\\
\\\
\ D.Naiss:+ dnai1 +\,+ shape2 +]);
}

out.newLine();

//刷新并关闭流
out.close();
catch(IOException e){
System.out.println(有问题:+ e);


$ / code $


解决方案

问题与Jena或RDF没有任何关系,但是在你的编码中是一个逻辑错误。
$ b

你调用 writeFile()

你需要重构代码在while循环之前打开文件一次,只要将 writeFile()方法添加到该文件中(而不是关闭它),然后在while循环之后关闭文件。

另外,@Udo Kilmaschewski在你明显的重复中指出 genr 变量没有在你显示的代码中被定义所以你似乎没有正确地将性别从你的while循环传递给 writeFile()函数。


Possible Duplicate:
Java - Using Jena APi - Get data from RDF file

I'm using Java and Jena API.

I have the class Person with the datatype properties hasFirstName, hasLastName, hasDateOfBirth, hasGender.

Here is how one person is represented in my RDF file.

<rdf:Description rdf:about="http://www.fam.com/FAM#Bruno04/02/1980 ">
    <j.0:FAMhasGender>H</j.0:FAMhasGender>
    <j.0:FAMhasDateOfBirth>04/02/1980</j.0:FAMhasDateOfBirth>
    <j.0:FAMhasLastName>DS </j.0:FAMhasLastName>
    <j.0:FAMhasFirstName> Bruno</j.0:FAMhasFirstName>
 </rdf:Description>

I'd like to get for each person the firstname, gender, date of birth and write that information in a text file. The problem I have is that it only writes the first woman/man he finds in the rdf file, but there is more than one woman and man.

Can you please explain me how can I solve this? Thank you very much.

ExtendedIterator instances = onto.person.listInstances();
Individual instance = null;
Individual firstInstance = null;
while (instances.hasNext()) {
    instance = (Individual) instances.next();

    gen = instance.getPropertyValue(onto.hasGender).toString();
    fname = instance.getPropertyValue(onto.hasFirstName).toString();
    dd = instance.getPropertyValue(onto.hasDateOfBirth).toString();

    writeFile(fname, dd, genr);}


// Write text file
public void writeFile(String fn, String dbir, String gn) {
    String fileout = "D:/file1.txt";
    String firstName = fn;
    String dateB = dbir;
    String gender = gn;

    BufferedWriter out;
    try {
        out = new BufferedWriter(new FileWriter(fileout, true));

        if (gender.equals("F")) {
            out.write("[label= \"" + firstName + " \"\n\n\"D.Naiss:" + dnai1 + "\", " + shape + "]");
        } else if (gender.equals("M")) {
            out.write("[label= \"" + firstName + " \"\n\n\"D.Naiss:" + dnai1 + "\", " + shape2 + "]");
        }

        out.newLine();

        // flushes and closes the stream
        out.close();
    } catch (IOException e) {
        System.out.println("There was a problem:" + e);
    }
}

解决方案

Your problem has nothing to do with Jena or RDF but is rather a logic error in your coding.

You call writeFile() inside your while loop which opens a new file, writes the current entry and closes the file. So you are repeatedly overwriting your file with a single entry at one time so you will only ever end up with a single person in the file.

You need to refactor the code to open the file once before the while loop, have the writeFile() method simply add to that file (and not close it) and then close the file after the while loop.

Also as @Udo Kilmaschewski pointed out in your apparent duplicate the genr variable is not defined in the code you showed so you don't appear to be correctly passing the gender from your while loop to the writeFile() function.

这篇关于从rdf文件获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆