2012-05-09 62 views
2

我有這個XML節點。我想用java分割座標到緯度和經度。如何拆分XML字符串?

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

我想分割座標或至少可以得到與這種格式(拉特,長)的座標。謝謝大家。

回答

3

你用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); 
} 

然後,您可以根據需要處理緯度和經度。

+0

請注意,您可能想要使用XML解析器獲取節點的內容,然後將正則表達式應用於該解析器。 XML,[像HTML](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags),通常不能用正則表達式解析。但它可能沒問題,這取決於你的用例的細節。 – Taymon

+0

感謝您的答覆。我只是有答案。即時通訊使用此代碼String st = getCharacterDataFromElement(line); String str [] = st.split(「:」,2); 字符串latlong; latlong = str [1]; out.println(latlong); –