1

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

9
  • 1
    post your stack trace Commented Apr 28, 2014 at 11:25
  • added to the post now Commented Apr 28, 2014 at 11:28
  • I think your xml is malformed.. Can you post it ? Commented Apr 28, 2014 at 11:29
  • Which XML API are you using? (not W3C DOM/SAX obviously) Commented Apr 28, 2014 at 11:31
  • XML-file added to the post. Commented Apr 28, 2014 at 11:39

3 Answers 3

0

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm. <?xml version="1.0" encoding="utf-8"?> is written at the top of the xml file. So I guess thats not the problem. Could the "garbage" maybe be the HTML format in the start of the xml file?
0

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.

7 Comments

I'm not quite sure what to do now, then? Should I delete the "earth.google.com/kml/2.1"-URL in the XML file? Chris
No, the XML-namespace is ok. As you have an Applet (according to your stack trace), how do you read the XML file? Can you post the code in your 'PApplet.loadXML'?
I have posted everything I have (in the code and in the stack trace). I was told by my teacher that Processing would be able to parce the XML file the way I did it already. So I think I need a library or something that could help me read the XML file? Chris
I don't know if it is the problem, but originally the XML file is a KML that I converted to XML. Could that be the problem?
As far as I can read it should be possible to call loadXML("filename.xml") from inside PApplet to read XML data in Processing.
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.