I always thought that newlines and whitespace in XML documents were ignored ? Is that a misconception, or is there something lacking in my code?
What happens is the following: I’m parsing an XML document which I retrieve from my server. This works fine when all content is on 1 line, but fails when the XML document is indented.
My code :
public class JsonTestActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
Log.d("xxx", "on create started");
try {
URL url = new URL("http://www.morefast.be/gethistory.php");
HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.setDoInput(true);
urlConnection.connect();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(urlConnection.getInputStream());
String path = doc.getElementsByTagName("GeometryCollection").item(0).getFirstChild().getFirstChild().getFirstChild().getNodeValue() ;
Log.d("xxx", "path is "+path);
}
catch (Exception e) {
Log.d("xxx", e.getMessage());
}
}
}
Works fine when the XML file is
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0"><Document>
<GeometryCollection><LineString><coordinates>These are the coordinates</coordinates></LineString></GeometryCollection>
</Document></kml>
But fails when the file is (e.g.)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.0"><Document>
<GeometryCollection>
<LineString><coordinates>These are the coordinates</coordinates></LineString></GeometryCollection>
</Document></kml>
All I changed in those files is adding an extra newline after “GeometryCollection”
LogCat shows
07-30 20:44:48.364: ERROR/AndroidRuntime(545): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mydomain.jsontest/com.mydomain.jsontest.JsonTestActivity}: java.lang.NullPointerException: println needs a message
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.os.Looper.loop(Looper.java:123)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at dalvik.system.NativeStart.main(Native Method)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): Caused by: java.lang.NullPointerException: println needs a message
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.util.Log.println_native(Native Method)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.util.Log.d(Log.java:122)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at com.mydomain.jsontest.JsonTestActivity.onCreate(JsonTestActivity.java:43)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-30 20:44:48.364: ERROR/AndroidRuntime(545): ... 11 more
Is that expected? I’d thought that XML documents ignored whitespace?
Disclaimer: I don’t do Android (or even Java these days) programming, however…
White-space in XML is generally not ignored by the parser. Rather it is turned into text nodes (containing the white-space as content). The exact details depends on the XML parser and options
supplied.
Could this be changing what
getFirstChild()is doing? That is, is the first child ofGeometryCollectiona text node and notLineString? (And the first child of a text node is …? Kaboom! Although, I can’t fit this into the trace:java.lang.NullPointerException: println needs a message… which seems odd sitting there.)I would suggest starting with a better selector and avoiding the manual stepping, if this is indeed the case.
Happy coding.