java读取jgn,Java-使用Jena APi-从RDF文件获取数据

My question concerns the class Person with the datatype properties hasFirstName, hasLastName, hasDateOfBirth, hasGender.

I'm using Java and Jena API.

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

H

04/02/1980

DS

Bruno

I want to write this line below if gender is female :

[label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+shape2+"]

so if there is, for example, 3 females the file must contain 3 lines with that format. The shape value( and then the output line) will depend on the gender, that's why i cannot not use the same line for both genders. Shape2 if female and shape if male.

For each person whose gender is male I want to output this line below:

[label= \"" +firstName+ " \"\n\n\"D.Naiss:"+dnai1+"\", "+**shape**+"]

The problem I have is that he outputs only one woman and one man with the corresponding line. However, I have more than one woman and man in my rdf file.

Here is the relevant code. Can you tell me what should I modify to solve this?

Thank you very much.

public void accessProp() {

readFile(inputFile); // rdf

String fname;

String dd;

String gen;

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);

}

}

解决方案

Without knowing Jena, I do not see any place in your code where you only select the male entries.

Check that while (instances.hasNext()) { loop to see what instances it loops through.

Because you write for each of that instances a line, the writeLine() method writes both, male and female entries, it might be that

ExtendedIterator instances = onto.person.listInstances();

returns the two male and female entries you see in your file.

Also, your example RDF entry has a value of H for gender, but in your code you are using M and Fto check it.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值