0

I have the folowing XML SAX Handler:

private class GetXML_Handler extends DefaultHandler { int x = 0; public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { Log.i("DataHandler", "Start of XML element"); int y = 0; if (qName.equals("polygon")) { locations.add(x, new ArrayList<location>()); String coordinates = attributes.getValue("coordinates"); String[] parts = coordinates.split(","); System.out.println("Cyklus zacaty"); locations.get(x).add(y, new location(Double.parseDouble(parts[0]), Double.parseDouble(parts[1]))); for(int i = 2; i <= parts.length; i = i + 2){ y++; double Latitude = Double.parseDouble(parts[i].substring(2)); double Longitude = Double.parseDouble(parts[i+1]); locations.get(x).add(y, new location(Latitude, Longitude)); } System.out.println("cyklus skonceny"); x++; } } } 

However "cyklus zacaty" never gets printed. It prints okay if I move it before String[] parts = coordinates.split(",") . Those strings are rly big (like 350 GPS coordinates) so is it possible that java simply cant handle it and it stops (with no exception).

Also my "Start of XML element" gets printed only 5 times (till first coordinates) but if I remove this split method it prints 28 times (number of my XML elements). I am sure that my XML handler works correctly (its just something about those Strings).

Structure of XML is like this:

<?xml version="1.0" encoding="UTF-8"?> <oblasti> <oblast> <nazovOblasti>VT</nazovOblasti> <polygon> <coordinates> 132456,4658789,0 56487,4864684 </coordinates> </polygon> .... 

Any suggestions?

Thanks in forward

9
  • well, i can tell you that it is not about the length of the Strings and the split method. Commented Nov 4, 2014 at 9:45
  • yeah I realized that when I added only 6 coordinates and it stopped too ... but I cant see where is the problem Commented Nov 4, 2014 at 9:56
  • you should use your debugger to see where the programm stops/exits. Or, my favorite, use many System.out.println() to figure out which values are not as expected. Commented Nov 4, 2014 at 9:58
  • well I used System.out.println() and it worked correclty before initializing parts = coordinates.split(","). But when I put it just after this line it doesnt print anything Commented Nov 4, 2014 at 10:00
  • what about error logs? exceptions? first guess , coordinates is NULL! maybe ?! Commented Nov 4, 2014 at 10:53

2 Answers 2

1

The Attributes parameter does not contain the child elements of the node, only the attributes assigned to the node (if any). Hence, attributes.getValue("coordinates") is not doing what you expect.

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

1 Comment

yeah I edited it and now it is getting texts but still some of this split methods is causing problems
0

I think your problem is not with the split method, but this line:

locations.get(x).add(y, new location(Double.parseDouble(parts[0]), Double.parseDouble(parts[1]))); 

and the reason is because the string has whitespaces, and they stay after splitted, so it can't be parsed.

try this just after the split and before anything else:

for (i=0; i<parts.length(); i++){ parts[i].trim(); } 

when an exception occurs, sometimes it breaks the thread before last line is completely executed, especially if it's a console output. It has made me mad too many times. This may be the reason your printline doesn't show.

8 Comments

also, he got Strings like: "0 56487" (he posted in the XML file), which will rise an NumberFormatException.. But this still doesnt explain, why he is not getting any Errors. And as discussed before: He dont even reach the first System.out.println(); So the problem has to be somewhere else. (note, that the problem you described will also occure)
True. He has to control that too. But I think somehow the exception is not showing but still raises.
made an edit to my original post (edited xml structure)... I have no whitespaces before or after commas so no need to trim and I am controling this "0 56487" by using substring(2) on every second item
you have to tell us where the programm is exiting. put a breakpoint to line " if (qName.equals("polygon")) {" and go through it with the debugger line by line.
Have you printed on console the "coordinates" string? Maybe it has something you didn't control
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.