Reading RDF data using Jena failing - java

The following code is to read a schema and a data file to find rdf.type of colin and Person. However, I am getting the error:
Exception in thread "main" org.apache.jena.riot.RiotException: [line: 1, col: 1 ] Content is not allowed in prolog. The code is given below:
public void reason(){
String NS = "urn:x-hp:eg/";
String fnameschema = "D://Work//EclipseWorkspace//Jena//data//rdfsDemoSchema.rdf";
String fnameinstance = "D://Work//EclipseWorkspace//Jena//data//rdfsDemoData.rdf";
Model schema = FileManager.get().loadModel(fnameschema);
Model data = FileManager.get().loadModel(fnameinstance);
InfModel infmodel = ModelFactory.createRDFSModel(schema, data);
Resource colin = infmodel.getResource(NS+"colin");
System.out.println("Colin has types");
for (StmtIterator i = infmodel.listStatements(colin, RDF.type, (RDFNode)null); i.hasNext(); ) {
Statement s = i.nextStatement();
System.out.println(s);
}
Resource Person = infmodel.getResource(NS+"Person");
System.out.println("\nPerson has types:");
for (StmtIterator i = infmodel.listStatements(Person, RDF.type, (RDFNode)null); i.hasNext(); ) {
Statement s = i.nextStatement();
System.out.println(s);
}
}
The file rdfsDemoData.rdf
#prefix eg: <urn:x-hp:eg/> .
<Teenager rdf:about="&eg;colin">
<mum rdf:resource="&eg;rosy" />
<age>13</age>
</Teenager>
The file rdfsDemoSchema.rdf
#prefix eg: <urn:x-hp:eg/> .
<rdf:Description rdf:about="&eg;mum">
<rdfs:subPropertyOf rdf:resource="&eg;parent"/>
</rdf:Description>
<rdf:Description rdf:about="&eg;parent">
<rdfs:range rdf:resource="&eg;Person"/>
<rdfs:domain rdf:resource="&eg;Person"/>
</rdf:Description>
<rdf:Description rdf:about="&eg;age">
<rdfs:range rdf:resource="&xsd;integer" />
</rdf:Description>

Your data is bad syntax. You are mixing Turtle and RDF/XML. RDF/XML does nto have #prefix - it uses XML's namespaces. It looks like you want an XML entity declaration like:
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY eg "urn:x-hp:eg/" >
]>
...

Related

OWLAPI : "ParserException" while converting String to Class Expression using ManchesterOWLSyntaxParser

I want to add new axiom into ontology, for that I created method which converts String [which is in Manchester OWL Syntax] into OWLClassExpression using ManchesterOWLSyntaxParser and later form new OWLAxiom and add to Ontology.
But I am getting following Exception (org.semanticweb.owlapi.manchestersyntax.renderer.ParserException) :-
Exception in thread "main" org.semanticweb.owlapi.manchestersyntax.renderer.ParserException: Encountered owl:real at line 1 column 12. Expected one of:
Datatype name
not
{
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl$ExceptionBuilder.build(ManchesterOWLSyntaxParserImpl.java:2441)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataRangePrimary(ManchesterOWLSyntaxParserImpl.java:813)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataUnionOf(ManchesterOWLSyntaxParserImpl.java:756)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataIntersectionOf(ManchesterOWLSyntaxParserImpl.java:737)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataRange(ManchesterOWLSyntaxParserImpl.java:729)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseDataRestriction(ManchesterOWLSyntaxParserImpl.java:695)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseNonNaryClassExpression(ManchesterOWLSyntaxParserImpl.java:584)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseIntersection(ManchesterOWLSyntaxParserImpl.java:488)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseUnion(ManchesterOWLSyntaxParserImpl.java:511)
at org.semanticweb.owlapi.manchestersyntax.parser.ManchesterOWLSyntaxParserImpl.parseClassExpression(ManchesterOWLSyntaxParserImpl.java:470)
at OWLAPI.convertStringToClassExpression(OWLAPI.java:29)
Following is my Ontology :-
<?xml version="1.0"?>
<!DOCTYPE rdf:RDF [
<!ENTITY owl "http://www.w3.org/2002/07/owl#" >
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<rdf:RDF xmlns="http://www.semanticweb.org/empty#"
xml:base="http://www.semanticweb.org/empty"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:owl="http://www.w3.org/2002/07/owl#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#">
<owl:Ontology rdf:about="http://www.semanticweb.org/empty"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Data properties
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/empty#name -->
<owl:DatatypeProperty rdf:about="http://www.semanticweb.org/empty#name"/>
<!--
///////////////////////////////////////////////////////////////////////////////////////
//
// Classes
//
///////////////////////////////////////////////////////////////////////////////////////
-->
<!-- http://www.semanticweb.org/empty#A -->
<owl:Class rdf:about="http://www.semanticweb.org/empty#A"/>
</rdf:RDF>
<!-- Generated by the OWL API (version 3.5.1) http://owlapi.sourceforge.net -->
My JAVA code is :-
// some code above to form OWLManager and Ontology
System.out.println(convertStringToClassExpression("name max 1 owl:real"));
private OWLClassExpression convertStringToClassExpression(String expression) {
ManchesterOWLSyntaxParser parser = OWLManager.createManchesterParser();
parser.setStringToParse(expression);
parser.setDefaultOntology(owlOntology); // my ontology
ShortFormEntityChecker checker = new ShortFormEntityChecker(getShortFormProvider());
parser.setOWLEntityChecker(checker);
return parser.parseClassExpression();
}
private BidirectionalShortFormProvider getShortFormProvider() {
Set<OWLOntology> ontologies = owlManager.getOntologies(); // my OWLOntologyManager
ShortFormProvider sfp = new ManchesterOWLSyntaxPrefixNameShortFormProvider(
owlManager.getOntologyFormat(owlOntology));
BidirectionalShortFormProvider shortFormProvider = new BidirectionalShortFormProviderAdapter(
ontologies, sfp);
return shortFormProvider;
}
But if I change my String from name max 1 owl:real to name max 1 xsd:string then there is no exception thrown from code. What's the problem ? How to avoid it ?
This is an OWLAPI bug, fixed in the current trunk versions. The fix will be released in 4.2.6 and 5.0.3, once they are completed and published on Maven Central.

How to seperate one RDF model into two models in Jena?

Now I have a RDF data, which contain two resources(I don't know whether it is correct to call the staff in rdf:description a resource), Now I want to separate the two resource into two rdf data in Jena, I do not know how to use the API to do it, The data example:
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:obs="http://localhost/SensorSchema/ontology#" >
<rdf:Description rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5">
<obs:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">65.00999166666666</obs:hasLatitude>
<obs:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1365156000000</obs:hasDate>
<obs:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int">212</obs:hasDirection>
<obs:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double">28.0</obs:hasVelocity>
<obs:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasAcceleration>
<obs:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">25.46780833333333</obs:hasLongitude>
<obs:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int">38</obs:hasArea>
<obs:hasDateTime>2013-04-05T13:00:00</obs:hasDateTime>
<obs:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int">51709293</obs:hasSender>
<rdf:type rdf:resource="http://localhost/SensorSchema/ontology#Observation"/>
<obs:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</obs:hasID>
<obs:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasDistance>
</rdf:Description>
<rdf:Description rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666">
<obs:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">65.00999166666666</obs:hasLatitude>
<obs:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long">1365156000000</obs:hasDate>
<obs:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int">500</obs:hasDirection>
<obs:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double">28.0</obs:hasVelocity>
<obs:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasAcceleration>
<obs:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double">25.46780833333333</obs:hasLongitude>
<obs:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int">38</obs:hasArea>
<obs:hasDateTime>2013-04-05T13:00:00</obs:hasDateTime>
<obs:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int">51709293</obs:hasSender>
<rdf:type rdf:resource="http://localhost/SensorSchema/ontology#Observation"/>
<obs:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int">1</obs:hasID>
<obs:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double">0.0</obs:hasDistance>
</rdf:Description>
</rdf:RDF>
I try to work like that:
ResIterator iter= OriginalModel.listSubjects();
int i=0;
while(iter.hasNext()) {
Resource subject = iter.next();
Model[i].?? // add the whole resource
i++;
}
but I don't know how to quickly add the resource to another model.
Once you have the subject, you can use listProperties to get a StmtIterator over the triples with that subject, and then you can use Model#add(StmtIterator) to add all those triples to a new Model.
public void splitModels() throws IOException {
// First, create a model and read the content
// into it. You probably already have this part,
// but we need it for a working example.
Model model = ModelFactory.createDefaultModel();
try (InputStream in = SplitModelExample.class.getResourceAsStream("/example.rdf")) {
RDFDataMgr.read(model, in, Lang.RDFXML);
}
// List the subjects in the model.
ResIterator subjects = model.listSubjects();
// For each subject, create another empty model that will
// contain the triples of which the subject is the subject.
// The #listProperties() method returns a StmtIterator over
// those triples, and Model#add(StmtIterator) adds all the
// triples to a model. Then we'll print out each submodel
// to make sure we're getting what we expect.
while (subjects.hasNext()) {
Resource subject = subjects.next();
Model subModel = ModelFactory.createDefaultModel();
subModel.add(subject.listProperties());
System.out.println("\n<!-- Submodel for "+subject+". -->");
RDFDataMgr.write(System.out, subModel, Lang.RDFXML);
}
}
<!-- Submodel for http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666. -->
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://localhost/SensorSchema/ontology#">
<j.0:Observation rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce16666">
<j.0:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>25.46780833333333</j.0:hasLongitude>
<j.0:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>65.00999166666666</j.0:hasLatitude>
<j.0:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>28.0</j.0:hasVelocity>
<j.0:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>1</j.0:hasID>
<j.0:hasDateTime>2013-04-05T13:00:00</j.0:hasDateTime>
<j.0:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
>1365156000000</j.0:hasDate>
<j.0:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasDistance>
<j.0:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>51709293</j.0:hasSender>
<j.0:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasAcceleration>
<j.0:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>38</j.0:hasArea>
<j.0:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>500</j.0:hasDirection>
</j.0:Observation>
</rdf:RDF>
<!-- Submodel for http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5. -->
<rdf:RDF
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:j.0="http://localhost/SensorSchema/ontology#">
<j.0:Observation rdf:about="http://localhost/SensorSchema/ontology#Observation_51709293_1_104519dd-63dc-4560-9286-8d621ce153c5">
<j.0:hasLatitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>65.00999166666666</j.0:hasLatitude>
<j.0:hasDate rdf:datatype="http://www.w3.org/2001/XMLSchema#long"
>1365156000000</j.0:hasDate>
<j.0:hasDirection rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>212</j.0:hasDirection>
<j.0:hasVelocity rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>28.0</j.0:hasVelocity>
<j.0:hasAcceleration rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasAcceleration>
<j.0:hasLongitude rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>25.46780833333333</j.0:hasLongitude>
<j.0:hasArea rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>38</j.0:hasArea>
<j.0:hasDateTime>2013-04-05T13:00:00</j.0:hasDateTime>
<j.0:hasSender rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>51709293</j.0:hasSender>
<j.0:hasID rdf:datatype="http://www.w3.org/2001/XMLSchema#int"
>1</j.0:hasID>
<j.0:hasDistance rdf:datatype="http://www.w3.org/2001/XMLSchema#double"
>0.0</j.0:hasDistance>
</j.0:Observation>
</rdf:RDF>

Jena multiple rdfs:label

I'm new to Jena and Owl I was given an ontology. I can open it with Protege 4.2 without any problems but when I try to open it with Jena I get:
Exception in thread "main" org.apache.jena.riot.RiotException: {E201} Multiple children of property element.
I have been looking a bit in my Ontology what it could be and I have noticed that some elements have more than one Label in a language for example:
<AnnotationAssertion>
<AnnotationProperty abbreviatedIRI="rdfs:label"/>
<AbbreviatedIRI>atc:A02BX02</AbbreviatedIRI>
<Literal xml:lang="no" datatypeIRI="&rdf;PlainLiteral">Sukralfat</Literal>
</AnnotationAssertion>
<AnnotationAssertion>
<AnnotationProperty abbreviatedIRI="rdfs:label"/>
<AbbreviatedIRI>atc:A02BX02</AbbreviatedIRI>
<Literal xml:lang="no" datatypeIRI="&rdf;PlainLiteral">antepsin</Literal>
</AnnotationAssertion>
<AnnotationAssertion>
<AnnotationProperty abbreviatedIRI="rdfs:label"/>
<AbbreviatedIRI>atc:A02BX02</AbbreviatedIRI>
<Literal datatypeIRI="&xsd;string">sucralfate</Literal>
</AnnotationAssertion>
<AnnotationAssertion>
<AnnotationProperty abbreviatedIRI="rdfs:label"/>
<AbbreviatedIRI>atc:A02BX02</AbbreviatedIRI>
<Literal xml:lang="no" datatypeIRI="&rdf;PlainLiteral">sukralfat</Literal>
</AnnotationAssertion>
Could this cause the problem? All the code I use works with other ontologies so I think it really comes from this ontology.
Do you know what could cause this exception?
Edit
So I got down to a minimized case and still get the same error:
<?xml version="1.0"?>
<!DOCTYPE Ontology [
<!ENTITY xsd "http://www.w3.org/2001/XMLSchema#" >
<!ENTITY xml "http://www.w3.org/XML/1998/namespace" >
<!ENTITY rdfs "http://www.w3.org/2000/01/rdf-schema#" >
<!ENTITY rdf "http://www.w3.org/1999/02/22-rdf-syntax-ns#" >
]>
<Ontology xmlns="http://www.w3.org/2002/07/owl#"
xml:base="http://www.ebi.ac.uk/Rebholz-srv/atc/public/ontologies/atc.owl"
xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"
xmlns:xsd="http://www.w3.org/2001/XMLSchema#"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:xml="http://www.w3.org/XML/1998/namespace"
ontologyIRI="http://www.ebi.ac.uk/Rebholz-srv/atc/public/ontologies/atc.owl">
<Prefix name="" IRI="http://www.ebi.ac.uk/Rebholz-srv/atc/public/ontologies/atc.owl#"/>
<Prefix name="atc" IRI="http://www.legemiddelverket.no/Legemiddelsoek/Sider/Default.aspx#"/>
<Prefix name="owl" IRI="http://www.w3.org/2002/07/owl#"/>
<Prefix name="rdf" IRI="http://www.w3.org/1999/02/22-rdf-syntax-ns#"/>
<Prefix name="xml" IRI="http://www.w3.org/XML/1998/namespace"/>
<Prefix name="xsd" IRI="http://www.w3.org/2001/XMLSchema#"/>
<Prefix name="rdfs" IRI="http://www.w3.org/2000/01/rdf-schema#"/>
<Declaration>
<Class abbreviatedIRI="atc:J"/>
</Declaration>
<AnnotationAssertion>
<AnnotationProperty abbreviatedIRI="rdfs:label"/>
<AbbreviatedIRI>atc:J</AbbreviatedIRI>
<Literal datatypeIRI="&xsd;string">ANTIINFECTIVES FOR SYSTEMIC USE</Literal>
</AnnotationAssertion>
</Ontology>
Here is the java code:
InputStream in = FileManager.get().open(filename);
if (in == null) {
throw new IllegalArgumentException("File: " + filename + " not found");
}
model = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
model.read(in, null);
try {
in.close();
} catch (IOException e) {
System.err.println("Couldn't close the inputStream");
}
Does this help? I really don't have any idea anymore...
That's an OWL2 XML formatted file. Jena does't support that format, but it does support OWL in RDF/XML.
In other words it's expecting the wrong flavour of XML and getting confused.
Try saving it in another format.

Cutting a XML with JAXB

I have the following xml:
<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic id="1">
<Size>
<value>1.68</value>
<scale>Meters</scale>
<comment>Size can vary, depending on temperature</comment>
</Size>
<Weight>
<value>9</value>
<scale>M*Tons</scale>
<comment>His mama is so fat, we had to use another scale</comment>
</Weight>
<rating>
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:value companyDepartment="Finance">A</rating:value>
<rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate>
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic id="2">
<Size/>
<Weight/>
<rating/>
</Characteristic>
...
<Characteristic id="n"/>
</PackageBody>
</Package>
And the following Java code:
public class XMLTest {
public static void main(String[] args) throws Exception {
Package currentPackage = new Package();
Package sourcePackage = new Package();
int totalCharacteristics;
PackageBody currentPackageBody = new PackageBody();
Characteristic currentCharacteristic = new Characteristic();
rating currentRating = new rating();
FileInputStream fis = new FileInputStream("sourceFile.xml");
JAXBContext myCurrentContext = JAXBContext.newInstance(Package.class);
Marshaller m = myCurrentContext.createMarshaller();
Unmarshaller um = myCurrentContext.createUnmarshaller();
sourcePackage = (Package)um.unmarshal(fis);
currentPackage.setPackageHeader(sourcePackage.getPackageHeader());
totalCharacteristics = sourcePackage.getPackageBody().getCharacteristics().size();
for (int i = 0; i < totalCharacteristics; i++)
{
currentRating = sourcePackage.getPackageBody().getCharacteristics().get(i).getrating();
}
currentCharacteristic.setrating(currentRating);
currentPackageBody.getCharacteristics().add(currentCharacteristic);
currentPackage.setPackageBody(currentPackageBody);
m.marshal(currentPackage, new File("targetFile.xml"));
fis.close();
}
}
Which gives me the next XML:
<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic id="1">
<rating>
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:value companyDepartment="Finance">A</rating:value>
<rating:expirationDate update="1/12/2010">1/1/2014</rating:expirationDate>
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic id="2">
<rating/>
</Characteristic>
...
<Characteristic id="n"/>
</PackageBody>
</Package>
And this is what I need:
<Package>
<PackageHeader>
<name>External Vendor File</name>
<description>External vendor file for some purpose</description>
<version>3.141694baR3</version>
</PackageHeader>
<PackageBody>
<Characteristic>
<rating id="1">
<ratingCompany>ISO</ratingCompany>
<rating:details xmlns:rating="http://www.w3schools.com/ratingDetails">
<rating:comment userID="z94234">You're not Silvia.</rating:comment>
<rating:comment userID="r24942">You're one of the Kung-Fu Creatures On The Rampage</rating:comment>
<rating:comment userID="i77880">TWO!</rating:comment>
<rating:priority>3</rating:priority>
</rating:details>
</rating>
</Characteristic>
<Characteristic>
<rating id="2"/>
</Characteristic>
...
<Characteristic/>
</PackageBody>
</Package>
But I have a few questions:
How could I implement a way to read a 4GBs file? (for example, reading it with StAX).
If I want to filter some tags from source to target(as in the last xml), would I have to assign them one by one to the targetFile? Is there any iterator that might allow me to go through all subnodes and assign them?
If the sourceFile changes, would I need to rerun the xjc and recompile the whole project?
Thanks.
For reading huge XML files, you definitely need a streaming parser like StAX. In addition, you can use a combination of JAXB to selectively map a given piece of xml to java object if you wish work with it. You need to regenerate your JAXB classes only if your schema changes. No need to regenerate if you application code changes.

import kml with java

I'm trying to import a mkl file with jak but i get the following error:
javax.xml.bind.UnmarshalException: unexpected element (uri:"http://earth.google.com/kml/2.2", local:"kml"). Expected elements are
...
and then a big list
Does anyone else run into this problem?
This is the code:
final Kml kml = Kml.unmarshal(new File("../data/Eemskanaal.kml"));
final Placemark placemark = (Placemark) kml.getFeature();
Point point = (Point) placemark.getGeometry();
List<Coordinate> coordinates = point.getCoordinates();
for (Coordinate coordinate : coordinates) {
System.out.println(coordinate.getLatitude());
System.out.println(coordinate.getLongitude());
System.out.println(coordinate.getAltitude());
}
And this is the kml file:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<name>BU00100107 Verspreide huizen Eemskanaal (ten zuiden)</name>
<description><![CDATA[description]]></description>
<Placemark>
<name>BLA!</name>
<description><![CDATA[]]></description>
<styleUrl>#style1</styleUrl>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
6.941796,53.314914,0.000000
6.942705,53.310923,0.000000
6.952713,53.305394,0.000000
6.954853,53.300262,0.000000
6.954239,53.296317,0.000000
6.962271,53.295483,0.000000
6.995900,53.287338,0.000000
6.995013,53.285264,0.000000
6.996842,53.281429,0.000000
6.991748,53.278255,0.000000
6.990729,53.275234,0.000000
6.988361,53.274477,0.000000
6.990120,53.271780,0.000000
6.984540,53.272709,0.000000
6.984543,53.274393,0.000000
6.980317,53.274404,0.000000
6.975829,53.272503,0.000000
6.974816,53.271125,0.000000
6.963342,53.271937,0.000000
6.955082,53.265909,0.000000
6.945183,53.269634,0.000000
6.940684,53.273351,0.000000
6.935942,53.273875,0.000000
6.934392,53.276351,0.000000
6.929104,53.272181,0.000000
6.909544,53.265952,0.000000
6.908803,53.269015,0.000000
6.909151,53.278897,0.000000
6.888166,53.279161,0.000000
6.887788,53.279639,0.000000
6.886750,53.280950,0.000000
6.886729,53.280977,0.000000
6.888260,53.281856,0.000000
6.895912,53.286254,0.000000
6.892976,53.288089,0.000000
6.891571,53.290803,0.000000
6.887323,53.298046,0.000000
6.887729,53.309725,0.000000
6.887583,53.309816,0.000000
6.888683,53.311891,0.000000
6.893966,53.313119,0.000000
6.924732,53.311548,0.000000
6.929655,53.312392,0.000000
6.934810,53.315353,0.000000
6.941796,53.314914,0.000000
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
<Polygon>
<outerBoundaryIs>
<LinearRing>
<tessellate>1</tessellate>
<coordinates>
6.905549,53.283453,0.000000
6.908790,53.282516,0.000000
6.912146,53.283305,0.000000
6.916480,53.287575,0.000000
6.916764,53.288072,0.000000
6.915251,53.288369,0.000000
6.915097,53.290097,0.000000
6.912526,53.292361,0.000000
6.908052,53.290971,0.000000
6.905569,53.288875,0.000000
6.905549,53.283453,0.000000
</coordinates>
</LinearRing>
</outerBoundaryIs>
</Polygon>
</Placemark>
</Document>
</kml>
Any other solutions are also welcome
Here is my quick and dirty way :)
public static Kml getKml(InputStream is) throws Exception {
String str = IOUtils.toString( is );
IOUtils.closeQuietly( is );
str = StringUtils.replace( str, "xmlns=\"http://earth.google.com/kml/2.2\"", "xmlns=\"http://www.opengis.net/kml/2.2\" xmlns:gx=\"http://www.google.com/kml/ext/2.2\"" );
ByteArrayInputStream bais = new ByteArrayInputStream( str.getBytes( "UTF-8" ) );
return Kml.unmarshal(bais);
}
I am unfamiliar with Jak, but if you're using the OGC Schema, the namespace is different. You have
http://earth.google.com/kml/2.2
The OGC namespace is
http://www.opengis.net/kml/2.2
The Google extension schema uses
http://www.google.com/kml/ext/2.2
as well. The namespace you're using was used by Google before KML was given to the OGC as an open standard.
The namespace is incorrect, but if you have 1700 files and this is the only problem, you might consider simply using the two-argument form of Kml.unmarshal(File file, boolean validate).
final Kml kml = loadXMLFile("../data/Eemskanaal.kml");
private static Kml loadXMLFile(String path) {
Kml kml = null;
try {
kml = Kml.unmarshal(path);
} catch (RuntimeException ex) {
kml = Kml.unmarshal(new File(path), false);
}
return kml;
}
I've also used the following cheezy perl script to correct my files.
$ cat ~/bin/fixxmlns
#!/usr/bin/perl
for (#ARGV) {
open (FH,"<$_");
open (NFH,">$_.x");
$look = 1;
while ($r = <FH>) {
if ($look && $r =~ /earth.google.com/) {
$r = qq{<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:gx="http://www.google.com/kml/ext/2.2">\n};
$look = 0;
}
print NFH $r;
}
close (NFH);
close (FH);
rename("$_", "$_.orig");
rename("$_.x", "$_");
}
$ fixxmlns *.kml
$ find parentdir -name "*.kml" -print0 | xargs -0 fixxmlns

Categories

Resources