Java XML解析错误| OSM脚本
执行下面的代码后,我收到一个错误,说明“非法字符是在索引95:overpass api.de/api/解释器处查询的?debug=way(大约:40,54.950922111781,-7.721371648195827)[“高速公路”][“maxspeed”];(.>;);out;” 有人能看看这个并提供一些建议吗 谢谢 我试图解析的XML文件:Java XML解析错误| OSM脚本,java,android,debugging,error-handling,openstreetmap,Java,Android,Debugging,Error Handling,Openstreetmap,执行下面的代码后,我收到一个错误,说明“非法字符是在索引95:overpass api.de/api/解释器处查询的?debug=way(大约:40,54.950922111781,-7.721371648195827)[“高速公路”][“maxspeed”];(.>;);out;” 有人能看看这个并提供一些建议吗 谢谢 我试图解析的XML文件: <way id="64273241"> <nd ref="768053039"/> <nd ref=
<way id="64273241">
<nd ref="768053039"/>
<nd ref="1922602861"/>
<nd ref="1922622063"/>
<nd ref="795319854"/>
<nd ref="795320324"/>
<tag k="highway" v="secondary"/>
<tag k="maxspeed" v="60"/>
<tag k="name" v="Port Road"/>
<tag k="oneway" v="no"/>
<tag k="ref" v="R229"/>
</way>
主要活动:
public class MainActivity extends ActionBarActivity {
TextView txtLat;
TextView txtLong;
TextView txtSpeed;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat = (TextView) findViewById(R.id.latTxt);
txtLong = (TextView) findViewById(R.id.longTxt);
txtSpeed = (TextView) findViewById(R.id.carSpeedTxt);
LocationManager ls = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
LocationListener li = new mylocationListener();
ls.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, li);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
//Location listener
class mylocationListener implements LocationListener
{
@Override
public void onLocationChanged(Location location) {
if(location != null)
{
//Get position
double lon = location.getLongitude();
double lat = location.getLatitude();
txtLong.setText("Longitude: " +lon);
txtLat.setText("Latitude: " + lat);
//Get speed
double speed = location.getSpeed();
txtSpeed.setText(speed + " km/h");
//Send request to recieve information regarding Speed zones everytime there is a change in location
//sendRequest(lat+"", lon+"");
//sendGetRequest(lat+"", lon+"");
readXML2(lat+"", lon+"");
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
@Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
@Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
}
/**
* Getting XML from URL making HTTP request
* @param url string
* */
public String getXmlFromUrl(String url) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
public void readXML2(String lat, String lon)
{
String query = "http://overpass-api.de/api/interpreter?data=way(around:40,"
+lon+","+lat+")" + " [\"highway\"] [\"maxspeed\"]; ( ._; >; ); out;";
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document document;
document = documentBuilder.parse(MainActivity.class.getResourceAsStream(getXmlFromUrl(query)));
NodeList nodeNodeList = document.getElementsByTagName("way");
for (int i = 0; i < nodeNodeList.getLength(); i++) {
Node nNode = nodeNodeList.item(i);
String limit = nNode.getAttributes().getNamedItem("maxspeed").getNodeValue();
TextView curLimitText = (TextView)findViewById(R.id.curLimitTxt);
curLimitText.setText(limit +" km/h");
System.out.println(nNode.getAttributes().getNamedItem("maxspeed").getNodeValue());
System.out.println(nNode.getAttributes().getNamedItem("lon").getNodeValue());
}
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
公共类MainActivity扩展了ActionBarActivity{
TextView-txtLat;
TextView-txtLong;
TextView-txtSpeed;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtLat=(TextView)findViewById(R.id.latTxt);
txtLong=(TextView)findViewById(R.id.longTxt);
txtSpeed=(TextView)findViewById(R.id.carSpeedTxt);
LocationManager ls=(LocationManager)getSystemService(Context.LOCATION\u服务);
LocationListener li=新建mylocationListener();
ls.requestLocationUpdates(LocationManager.GPS_提供程序,0,0,li);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
//位置侦听器
类mylocationListener实现LocationListener
{
@凌驾
已更改位置上的公共无效(位置){
如果(位置!=null)
{
//获得位置
double lon=location.getLongitude();
双纬度=location.getLatitude();
setText(“经度:+lon”);
设置纬度(纬度:+lat);
//加速
double speed=location.getSpeed();
txtSpeed.setText(速度+“km/h”);
//每次位置发生变化时发送请求以接收有关速度区的信息
//发送请求(lat+“”,lon+“”);
//sendGetRequest(lat+“”,lon+“”);
readXML2(lat+“”,lon+“”);
}
}
@凌驾
public void onStatusChanged(字符串提供程序、int状态、Bundle extra){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderEnabled(字符串提供程序){
//TODO自动生成的方法存根
}
@凌驾
公共无效onProviderDisabled(字符串提供程序){
//TODO自动生成的方法存根
}
}
/**
*从发出HTTP请求的URL获取XML
*@param url字符串
* */
公共字符串getXmlFromUrl(字符串url){
字符串xml=null;
试一试{
//defaultHttpClient
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
xml=EntityUtils.toString(httpEntity);
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
//返回XML
返回xml;
}
public void readXML2(字符串lat、字符串lon)
{
字符串查询=”http://overpass-api.de/api/interpreter?data=way(约:40)
+lon+“,“+lat+””+“[\“高速公路\]”][\“最大速度\]”;(.\u;>);out;“;
试一试{
DocumentBuilderFactory DocumentBuilderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder DocumentBuilder=documentBuilderFactory.newDocumentBuilder();
org.w3c.dom.Document;
document=documentBuilder.parse(MainActivity.class.getResourceAsStream(getXmlFromUrl(查询));
NodeList nodeNodeList=document.getElementsByTagName(“方式”);
for(int i=0;i
那么第/char 95行是什么?您是否正确设置了UTF-8 unicode编码?日志上刚刚显示了索引95。它必须是char。不,我还没有设置。我需要设置吗?