2

I have this xml node. I want to split the coordinate to latitude and longitude using java.

<MAP>1234:3.12345,119.12345</MAP>

I want to split the coordinate or at least can get the coordinate with this format (lat,long). Thanks all.

1 Answers1

3

Have you tried with regex ?

final Pattern regex = Pattern.compile("<MAP>((.*):(.*),(.*))</MAP>", Pattern.DOTALL); 
final Matcher matcher = regex.matcher(whatyouwanttoparselatlongwiththeaboveformat); 
if (matcher.find()) { 
     System.out.print(matcher.group(2) + " : (lat,lon) = ");
     float latitude = Float.valueOf(matcher.group(3));
     float longitude = Float.valueOf(matcher.group(4));
     System.out.println(latitude + "," + longitude);
} 

Then you can deal with latitude and longitude as you wish.

Jerome
  • 4,472
  • 4
  • 18
  • 18
  • Note that you probably want to get the contents of the node with an XML parser and then apply the regex to that. XML, [like HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags), generally cannot be parsed with regex. But it might be fine, depending on the details of your use case. – Taymon May 09 '12 at 03:18
  • thanks for the replies. i just have the answer. im using this code String st = getCharacterDataFromElement(line); String str[] = st.split(":", 2); String latlong; latlong = str[1]; out.println(latlong); – aishul.aman May 09 '12 at 04:23