import kml with java - 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
Related
VTD-XML reading gives no results
I am trying to read a RSS content using VTD-XML. Below is a sample of RSS. <?xml version="1.0" encoding="UTF-8"?> <rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"> <?xml-stylesheet type="text/xsl" href="rss.xsl"?> <channel> <title>MyRSS</title> <atom:link href="http://www.example.com/rss.php" rel="self" type="application/rss+xml" /> <link>http://www.example.com/rss.php</link> <description>MyRSS</description> <language>en-us</language> <pubDate>Tue, 22 May 2018 13:15:15 +0530</pubDate> <item> <title>Title 1</title> <pubDate>Tue, 22 May 2018 13:14:40 +0530</pubDate> <link>http://www.example.com/news.php?nid=47610</link> <guid>http://www.example.com/news.php?nid=47610</guid> <description>bla bla bla</description> </item> </channel> </rss> Anyway as you know, some RSS feeds can contain more styling info etc. However in every RSS, the <channel> and <item> will be common, at least for the ones I need to use. I tried VTD XML to read this as quickly as possible. Below is the code. VTDGen vg = new VTDGen(); if (vg.parseHttpUrl(appDataBean.getUrl(), true)) { VTDNav vn = vg.getNav(); AutoPilot ap = new AutoPilot(vn); ap.selectXPath("/channel/item"); int result = -1; while ((result = ap.evalXPath()) != -1) { if (vn.matchElement("item")) { do { //do something with the contnets in the item node Log.d("VTD", vn.toString(vn.getText())); } while (vn.toElement(VTDNav.NEXT_SIBLING)); } } } Unfortunately this did not print anything. What am I doing wrong here? Also non of the RSS feeds are very big, so I need to read them in couple of miliseconds. This code is on Android.
How to train Chunker in Opennlp?
I need to train the Chunker in Opennlp to classify the training data as a noun phrase. How do I proceed? The documentation online does not have an explanation how to do it without the command line, incorporated in a program. It says to use en-chunker.train, but how do you make that file? EDIT: #Alaye After running the code you gave in your answer, I get the following error that I cannot fix: Indexing events using cutoff of 5 Computing event counts... done. 3 events Dropped event B-NP:[w_2=bos, w_1=bos, w0=He, w1=reckons, w2=., w_1=bosw0=He, w0=Hew1=reckons, t_2=bos, t_1=bos, t0=PRP, t1=VBZ, t2=., t_2=bost_1=bos, t_1=bost0=PRP, t0=PRPt1=VBZ, t1=VBZt2=., t_2=bost_1=bost0=PRP, t_1=bost0=PRPt1=VBZ, t0=PRPt1=VBZt2=., p_2=bos, p_1=bos, p_2=bosp_1=bos, p_1=bost_2=bos, p_1=bost_1=bos, p_1=bost0=PRP, p_1=bost1=VBZ, p_1=bost2=., p_1=bost_2=bost_1=bos, p_1=bost_1=bost0=PRP, p_1=bost0=PRPt1=VBZ, p_1=bost1=VBZt2=., p_1=bost_2=bost_1=bost0=PRP, p_1=bost_1=bost0=PRPt1=VBZ, p_1=bost0=PRPt1=VBZt2=., p_1=bosw_2=bos, p_1=bosw_1=bos, p_1=bosw0=He, p_1=bosw1=reckons, p_1=bosw2=., p_1=bosw_1=bosw0=He, p_1=bosw0=Hew1=reckons] Dropped event B-VP:[w_2=bos, w_1=He, w0=reckons, w1=., w2=eos, w_1=Hew0=reckons, w0=reckonsw1=., t_2=bos, t_1=PRP, t0=VBZ, t1=., t2=eos, t_2=bost_1=PRP, t_1=PRPt0=VBZ, t0=VBZt1=., t1=.t2=eos, t_2=bost_1=PRPt0=VBZ, t_1=PRPt0=VBZt1=., t0=VBZt1=.t2=eos, p_2=bos, p_1=B-NP, p_2=bosp_1=B-NP, p_1=B-NPt_2=bos, p_1=B-NPt_1=PRP, p_1=B-NPt0=VBZ, p_1=B-NPt1=., p_1=B-NPt2=eos, p_1=B-NPt_2=bost_1=PRP, p_1=B-NPt_1=PRPt0=VBZ, p_1=B-NPt0=VBZt1=., p_1=B-NPt1=.t2=eos, p_1=B-NPt_2=bost_1=PRPt0=VBZ, p_1=B-NPt_1=PRPt0=VBZt1=., p_1=B-NPt0=VBZt1=.t2=eos, p_1=B-NPw_2=bos, p_1=B-NPw_1=He, p_1=B-NPw0=reckons, p_1=B-NPw1=., p_1=B-NPw2=eos, p_1=B-NPw_1=Hew0=reckons, p_1=B-NPw0=reckonsw1=.] Dropped event O:[w_2=He, w_1=reckons, w0=., w1=eos, w2=eos, w_1=reckonsw0=., w0=.w1=eos, t_2=PRP, t_1=VBZ, t0=., t1=eos, t2=eos, t_2=PRPt_1=VBZ, t_1=VBZt0=., t0=.t1=eos, t1=eost2=eos, t_2=PRPt_1=VBZt0=., t_1=VBZt0=.t1=eos, t0=.t1=eost2=eos, p_2B-NP, p_1=B-VP, p_2B-NPp_1=B-VP, p_1=B-VPt_2=PRP, p_1=B-VPt_1=VBZ, p_1=B-VPt0=., p_1=B-VPt1=eos, p_1=B-VPt2=eos, p_1=B-VPt_2=PRPt_1=VBZ, p_1=B-VPt_1=VBZt0=., p_1=B-VPt0=.t1=eos, p_1=B-VPt1=eost2=eos, p_1=B-VPt_2=PRPt_1=VBZt0=., p_1=B-VPt_1=VBZt0=.t1=eos, p_1=B-VPt0=.t1=eost2=eos, p_1=B-VPw_2=He, p_1=B-VPw_1=reckons, p_1=B-VPw0=., p_1=B-VPw1=eos, p_1=B-VPw2=eos, p_1=B-VPw_1=reckonsw0=., p_1=B-VPw0=.w1=eos] Indexing... done. Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at opennlp.tools.ml.model.AbstractDataIndexer.sortAndMerge(AbstractDataIndexer.java:89) at opennlp.tools.ml.model.TwoPassDataIndexer.<init>(TwoPassDataIndexer.java:105) at opennlp.tools.ml.AbstractEventTrainer.getDataIndexer(AbstractEventTrainer.java:74) at opennlp.tools.ml.AbstractEventTrainer.train(AbstractEventTrainer.java:91) at opennlp.tools.ml.model.TrainUtil.train(TrainUtil.java:53) at opennlp.tools.chunker.ChunkerME.train(ChunkerME.java:253) at com.oracle.crm.nlp.CustomChunker2.main(CustomChunker2.java:91) Sorting and merging events... Process exited with exit code 1. (My en-chunker.train had only the first 2 and last line of your sample data set.) Could you please tell me why this is happening and how to fix it? EDIT2: I got the Chunker to work, however it gives an error when I change the sentence in the training set to any sentence other than the one you've given in your answer. Can you tell me why that could be happening?
As said in Opennlp Documentation Sample sentence of the training data: He PRP B-NP reckons VBZ B-VP the DT B-NP current JJ I-NP account NN I-NP deficit NN I-NP will MD B-VP narrow VB I-VP to TO B-PP only RB B-NP # # I-NP 1.8 CD I-NP billion CD I-NP in IN B-PP September NNP B-NP . . O This is how you make your en-chunk.train file and you can create the corresponding .bin file using CLI: $ opennlp ChunkerTrainerME -model en-chunker.bin -lang en -data en-chunker.train -encoding or using API public class SentenceTrainer { public static void trainModel(String inputFile, String modelFile) throws IOException { Objects.nonNull(inputFile); Objects.nonNull(modelFile); MarkableFileInputStreamFactory factory = new MarkableFileInputStreamFactory( new File(inputFile)); Charset charset = Charset.forName("UTF-8"); ObjectStream<String> lineStream = new PlainTextByLineStream(new FileInputStream("en-chunker.train"),charset); ObjectStream<ChunkSample> sampleStream = new ChunkSampleStream(lineStream); ChunkerModel model; try { model = ChunkerME.train("en", sampleStream, new DefaultChunkerContextGenerator(), TrainingParameters.defaultParams()); } finally { sampleStream.close(); } OutputStream modelOut = null; try { modelOut = new BufferedOutputStream(new FileOutputStream(modelFile)); model.serialize(modelOut); } finally { if (modelOut != null) modelOut.close(); } } } and the main method will be: public class Main { public static void main(String args[]) throws IOException { String inputFile = "//path//to//data.train"; String modelFile = "//path//to//.bin"; SentenceTrainer.trainModel(inputFile, modelFile); } } reference: this blog hope this helps! PS: collect/write the data as above in a .txt file and rename it with .train extension or even the trainingdata.txt will work. that is how you make a .train file.
Reading RDF data using Jena failing
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="⪚colin"> <mum rdf:resource="⪚rosy" /> <age>13</age> </Teenager> The file rdfsDemoSchema.rdf #prefix eg: <urn:x-hp:eg/> . <rdf:Description rdf:about="⪚mum"> <rdfs:subPropertyOf rdf:resource="⪚parent"/> </rdf:Description> <rdf:Description rdf:about="⪚parent"> <rdfs:range rdf:resource="⪚Person"/> <rdfs:domain rdf:resource="⪚Person"/> </rdf:Description> <rdf:Description rdf:about="⪚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/" > ]> ...
XPath Java parsing xml document under more conditions
XPath Java parsing xml under more conditions I need to show elements from books.xml which satisfy next two conditions: price > 10 and publish_date > "2006-12-31" . books.xml is: <?xml version='1.0'?> <catalog> <book id='bk110'> <author>O'Brien, Tim</author> <title>Microsoft .NET: The Programming Bible</title> <genre>Computer</genre> <price>36.95</price> <publish_date>2006-12-09</publish_date> <description>Microsoft's .NET initiative is explored in detail in this deep programmer's reference.</description> </book> <book id='bk111'> <author>O'Brien, Tim</author> <title>MSXML3: A Comprehensive Guide</title> <genre>Computer</genre> <price>36.95</price> <publish_date>2007-12-01</publish_date> <description>The Microsoft MSXML3 parser is covered in detail, with attention to XML DOM interfaces, XSLT processing, SAX and more.</description> </book> <book id='bk112'> <author>Galos, Mike</author> <title>Visual Studio 7: A Comprehensive Guide</title> <genre>Computer</genre> <price>49.95</price> <publish_date>2008-04-16</publish_date> <description>Microsoft Visual Studio 7 is explored in depth, looking at how Visual Basic, Visual C++, C#, and ASP+ are integrated into a comprehensive development environment.</description> </book> </catalog> When I try this code: package web.services; import java.io.File; import java.io.*; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.xpath.*; import org.xml.sax.*; import org.w3c.dom.*; public class WebServices { private static void showElements() { InputSource inputSource = null; Object result; NodeList nodeList = null; String file; String workingDir = System.getProperty("user.dir"); file="data"+File.separator+"books.xml"; try { XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xPathExpression = xPath.compile("//book[price > 10][xs:date(publish_date) > xs:date('2005-12-31')]/*/text()"); File xmlDocument = new File(file); try { inputSource = new InputSource(new FileInputStream(xmlDocument)); } catch (FileNotFoundException ex) { Logger.getLogger(WebServices.class.getName()).log(Level.SEVERE, null, ex); } result = xPathExpression.evaluate(inputSource, XPathConstants.NODESET); nodeList = (NodeList) result; } catch (XPathExpressionException ex) { Logger.getLogger(WebServices.class.getName()).log(Level.SEVERE, null, ex); } for (int i = 0; i < nodeList.getLength(); i++) { System.out.print("Node name: " + nodeList.item(i).getNodeName()); System.out.print(" | "); System.out.println("Node value: " + nodeList.item(i).getNodeValue()); System.out.println("------------------------------------------------"); } } /** * #param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here showElements(); } } I'm getting this error: maj 27, 2015 10:01:19 AM web.services.WebServices showElements SEVERE: null javax.xml.transform.TransformerException: Unknown error in XPath. at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:368) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:305) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:135) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:109) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:303) at web.services.WebServices.showElements(WebServices.java:39) at web.services.WebServices.main(WebServices.java:58) Caused by: java.lang.NullPointerException at com.sun.org.apache.xpath.internal.functions.FuncExtFunction.execute(FuncExtFunction.java:210) at com.sun.org.apache.xpath.internal.Expression.execute(Expression.java:157) at com.sun.org.apache.xpath.internal.operations.Operation.execute(Operation.java:111) at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.executePredicates(PredicatedNodeTest.java:344) at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:481) at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(AxesWalker.java:374) at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(WalkingIterator.java:197) at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(NodeSequence.java:344) at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(NodeSequence.java:503) at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:279) at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:214) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:339) ... 6 more --------- java.lang.NullPointerException at com.sun.org.apache.xpath.internal.functions.FuncExtFunction.execute(FuncExtFunction.java:210) at com.sun.org.apache.xpath.internal.Expression.execute(Expression.java:157) at com.sun.org.apache.xpath.internal.operations.Operation.execute(Operation.java:111) at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.executePredicates(PredicatedNodeTest.java:344) at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:481) at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(AxesWalker.java:374) at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(WalkingIterator.java:197) at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(NodeSequence.java:344) at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(NodeSequence.java:503) at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:279) at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:214) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:339) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:305) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:135) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:109) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:303) at web.services.WebServices.showElements(WebServices.java:39) at web.services.WebServices.main(WebServices.java:58) --------------- linked to ------------------ javax.xml.xpath.XPathExpressionException: javax.xml.transform.TransformerException: Unknown error in XPath. at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:305) at web.services.WebServices.showElements(WebServices.java:39) at web.services.WebServices.main(WebServices.java:58) Caused by: javax.xml.transform.TransformerException: Unknown error in XPath. at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:368) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:305) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:135) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.eval(XPathExpressionImpl.java:109) at com.sun.org.apache.xpath.internal.jaxp.XPathExpressionImpl.evaluate(XPathExpressionImpl.java:303) ... 2 more Caused by: java.lang.NullPointerException at com.sun.org.apache.xpath.internal.functions.FuncExtFunction.execute(FuncExtFunction.java:210) at com.sun.org.apache.xpath.internal.Expression.execute(Expression.java:157) at com.sun.org.apache.xpath.internal.operations.Operation.execute(Operation.java:111) at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.executePredicates(PredicatedNodeTest.java:344) at com.sun.org.apache.xpath.internal.axes.PredicatedNodeTest.acceptNode(PredicatedNodeTest.java:481) at com.sun.org.apache.xpath.internal.axes.AxesWalker.nextNode(AxesWalker.java:374) at com.sun.org.apache.xpath.internal.axes.WalkingIterator.nextNode(WalkingIterator.java:197) at com.sun.org.apache.xpath.internal.axes.NodeSequence.nextNode(NodeSequence.java:344) at com.sun.org.apache.xpath.internal.axes.NodeSequence.runTo(NodeSequence.java:503) at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(NodeSequence.java:279) at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(LocPathIterator.java:214) at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:339) ... 6 more Exception in thread "main" java.lang.NullPointerException at web.services.WebServices.showElements(WebServices.java:45) at web.services.WebServices.main(WebServices.java:58) Java Result: 1 BUILD SUCCESSFUL (total time: 2 seconds) What is wrong? Thank you!
You are trying to use XPath 2.0 data types like xs:date while the XPath implementation in the Oracle JRE only supports XPath 1.0 which does not know any such data types. For that particular path expression it should be possible to use XPath 1.0 and simple number comparison with a path like //book[price > 10][number(translate(publish_date, '-', '')) > 20051231]. If you want to use XPath 2.0 you need to look into third party libraries like Saxon 9 or into XQuery implementations (as XPath 2.0 is a subset of XQuery 1.0).
NullPointerException when importing XML-file into Processing, Java
I'm trying to get data from an XML-file and use this data in processing. When doing so I get a NPE, and I can't quite figure out where I'm wrong. The XML got several layers and I have to get data from this "child": http://i62.tinypic.com/2mb90g.png My code looks like this: XML xml; void setup(){ xml = loadXML("parker.xml"); XML[] children = xml.getChildren("kml"); XML[] Folder=children[0].getChildren("Folder"); XML[] Placemark=Folder[1].getChildren("Placemark"); XML[] Polygon=Placemark[2].getChildren("Polygon"); XML[] outerBoundaryIs=Polygon[3].getChildren("outerBoundaryIs"); XML[] LinearRing=outerBoundaryIs[4].getChildren("LinearRing"); for (int i = 0; i < LinearRing.length; i++) { float coordinates = children[i].getFloat("coordinates"); println(coordinates); } } Best Chris Stack trace: [Fatal Error] :1:1: Content is not allowed in prolog. org.xml.sax.SAXParseException; lineNumber: 1; columnNumber: 1; Content is not allowed in prolog. at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257) at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:347) at processing.data.XML.(XML.java:187) at processing.core.PApplet.loadXML(PApplet.java:6310) at processing.core.PApplet.loadXML(PApplet.java:6300) at XMLtryout.setup(XMLtryout.java:21) at processing.core.PApplet.handleDraw(PApplet.java:2359) at processing.core.PGraphicsJava2D.requestDraw(PGraphicsJava2D.java:240) at processing.core.PApplet.run(PApplet.java:2254) at java.lang.Thread.run(Thread.java:744) XML file: https://www.dropbox.com/s/xn3thjskhlf2wai/parker.xml
This error maybe caused because missing this at the top of your xml file <?xml version="1.0" encoding="utf-8"?> or there's some non-printable garbage at the start of your file.
The 'Content is not allowed in prolog' error indicates that you have some content between the XML declaration and the appearance of the document element, for example <?xml version="1.0" encoding="utf-8"?> content here is not allowed <kml xmlns="http://earth.google.com/kml/2.1"> ... </kml> The XML file you linked is ok though, so it seems you're reading the XML binary incorrectly before passing it to the XML parser, or (more likely) you're not reading the XML at all (can happen when you read from a web URL and getting an error response). I assume you get a HTTP 40x error which you don't recognize, and read the response (usually HTML) as XML, which causes the error. Remember that applets usually can only read resources from the same server (that's what might cause the error). To verify this, attempt to read the URL content and output it as text, and check if it looks ok.
Make it more easy try like this public static void setUp(){ try { File file = new File("parker.xml"); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(file); doc.getDocumentElement().normalize(); System.out.println("Root element " + doc.getDocumentElement().getNodeName()); NodeList nodeLst = doc.getElementsByTagName("LinearRing"); System.out.println("Information"); for (int s = 0; s < nodeLst.getLength(); s++) { Node fstNode = nodeLst.item(s); if (fstNode.getNodeType() == Node.ELEMENT_NODE) { Element fstElmnt = (Element) fstNode; NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("coordinates"); Element fstNmElmnt = (Element) fstNmElmntLst.item(0); NodeList fstNm = fstNmElmnt.getChildNodes(); System.out.println("coordinates : " + ((Node) fstNm.item(0)).getNodeValue()); // // } } } catch (Exception e) { e.printStackTrace(); } } that should display : Root element kml Information coordinates : 10.088512,56.154109,0 10.088512,56.154109,0 10.088512,56.154109,0 10.088511,56.15411,0 10.088511,56.15411,0 10.08851,56.15411,0 10.08851,56.154111,0 10.088509,56.154111,0 10.088508,56.154111,0 10.088508,56.154111,0 10.088507,56.154111,0 10.088506,56.154111,0 10.088506,56.154111,0 10.088505,56.154111,0 10.088504,56.154111,0 10.088504,56.154111,0 10.088503,56.15411,0 10.088503,56.15411,0 10.088502,56.15411,0 10.088502,56.154109,0 10.088502,56.154109,0 10.088501,56.154109,0 10.088501,56.154108,0 10.088501,56.154108,0 10.088501,56.154108,0 10.088501,56.154107,0 10.088285,56.154094,0 10.088104,56.154372,0 10.088061,56.154401,0 10.087988,56.15445,0 10.087915,56.154611,0 10.08791,56.154613,0 10.087912,56.154613,0 10.087877,56.1548,0 10.08772,56.15482,0 10.087558,56.154911,0 10.087421,56.155111,0 10.087316,56.155308,0 10.087328,56.15538,0 10.087372,56.155413,0 10.087446,56.155453,0 10.087484,56.155487,0 10.08747,56.155601,0 10.08772,56.155616,0 10.087719,56.155618,0 10.088618,56.155671,0 10.089096,56.1557,0 10.089096,56.155699,0 10.089138,56.155701,0 10.089127,56.155706,0 10.089004,56.155787,0 10.08888,56.155853,0 10.088799,56.155806,0 10.088571,56.155914,0 10.088455,56.155946,0 10.088112,56.156081,0 10.088184,56.156138,0 10.087733,56.156353,0 10.087489,56.156421,0 10.087288,56.156341,0 10.087268,56.156333,0 10.086893,56.156182,0 10.08684,56.156271,0 10.087049,56.156373,0 10.086893,56.156455,0 10.086664,56.156575,0 10.086443,56.156698,0 10.086425,56.156708,0 10.085983,56.156955,0 10.085655,56.157139,0 10.085462,56.157276,0 10.085272,56.157233,0 10.085176,56.157328,0 10.084917,56.157393,0 10.084883,56.157458,0 10.08495,56.157513,0 10.084947,56.157524,0 10.084943,56.157539,0 10.084855,56.15787,0 10.084855,56.15787,0 10.084321,56.157317,0 10.085553,56.156195,0 10.085555,56.156194,0 10.085553,56.156194,0 10.085734,56.156035,0 10.085821,56.155977,0 10.085937,56.155932,0 10.085993,56.155942,0 10.086031,56.155959,0 10.086171,56.15592,0 10.086227,56.155901,0 10.086392,56.155841,0 10.086513,56.155786,0 10.08664,56.155699,0 10.086686,56.155657,0 10.086727,56.155605,0 10.086777,56.155486,0 10.086861,56.155289,0 10.086916,56.155134,0 10.087006,56.154899,0 10.087075,56.154706,0 10.087094,56.154649,0 10.0871,56.154574,0 10.087112,56.154464,0 10.087111,56.154362,0 10.087112,56.154279,0 10.087112,56.154279,0 10.087113,56.15427,0 10.087114,56.154198,0 10.087108,56.15413,0 10.087091,56.154054,0 10.086992,56.153698,0 10.087,56.153678,0 10.087031,56.153647,0 10.087036,56.153648,0 10.087046,56.153652,0 10.087035,56.153647,0 10.087039,56.153645,0 10.087072,56.153612,0 10.087367,56.153308,0 10.087371,56.153303,0 10.08742,56.15323,0 10.087568,56.152963,0 10.087568,56.152962,0 10.087569,56.152962,0 10.08757,56.152961,0 10.087571,56.15296,0 10.087573,56.152959,0 10.087574,56.152959,0 10.087575,56.152958,0 10.087577,56.152958,0 10.087579,56.152958,0 10.087581,56.152957,0 10.087582,56.152957,0 10.087584,56.152957,0 10.087586,56.152958,0 10.087588,56.152958,0 10.087589,56.152958,0 10.087591,56.152959,0 10.087592,56.152959,0 10.087593,56.15296,0 10.087594,56.152961,0 10.087595,56.152962,0 10.087596,56.152963,0 10.087596,56.152964,0 10.087596,56.152965,0 10.087596,56.152965,0 10.087614,56.152967,0 10.087921,56.152988,0 10.088134,56.153019,0 10.088311,56.15304,0 10.088454,56.153052,0 10.088469,56.153378,0 10.08847,56.153445,0 10.088473,56.153597,0 10.088473,56.153597,0 10.088473,56.153597,0 10.088703,56.153614,0 10.088703,56.153614,0 10.088703,56.153614,0 10.088704,56.153614,0 10.088705,56.153614,0 10.088705,56.153615,0 10.088706,56.153615,0 10.088706,56.153615,0 10.088707,56.153616,0 10.088707,56.153616,0 10.088707,56.153616,0 10.088707,56.153617,0 10.088707,56.153617,0 10.088707,56.153617,0 10.088707,56.153618,0 10.088512,56.154108,0 10.088512,56.154109,0 coordinates : 10.086779,56.155487,0 10.086778,56.155488,0 10.086779,56.155487,0 10.086779,56.155487,0 coordinates : 10.08847,56.153602,0 10.088469,56.153602,0 10.088469,56.153602,0 10.08847,56.153602,0 PS : kml is the root element