我在使用DocumentBuilderFactory解析xml字符串時遇到問題。當我查看log.d(「文件」,文件)時,文件包含我想要的xml字符串。但是,當我使用Document doc = db.parse(inputSource)來獲取文檔時,它會返回一個SAXException。有人能告訴我我的編碼有什麼問題嗎?謝謝。如何解決SAXException從builder.parse(xmlString)返回?
String query="https://maps.googleapis.com/maps/api/directions/xml?origin=33.78917,-118.107626&destination=33.77319,-118.125221&sensor=true";
url = new URL(query);
URLConnection connection = url.openConnection();
HttpURLConnection httpConnection = (HttpURLConnection)connection;
int responseCode = httpConnection.getResponseCode();
InputStreamReader in = new InputStreamReader(httpConnection.getInputStream());
InputStream inputStream = httpConnection.getInputStream();
BufferedReader buf = new BufferedReader(in);
String file="";
while(buf.readLine()!=null){
file+=buf.readLine();
Log.d("File", file);
}
if(responseCode==HttpURLConnection.HTTP_OK){
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db= dbf.newDocumentBuilder();
//parse xml file
//Document doc = db.parse(file);
InputSource inputSource = new InputSource();
inputSource.setCharacterStream(new StringReader(file));
Log.d("COde",httpConnection.getResponseCode()+"");
try{
Document doc = db.parse(inputSource);
Element el = doc.getDocumentElement();
NodeList nl = el.getElementsByTagName("step");
Log.d("COde",httpConnection.getResponseCode()+"");
Path p;
if(nl!=null && nl.getLength()>0){
for(int i=0; i<nl.getLength();i++){
Node entry =nl.item(i);
NodeList child= entry.getChildNodes();
Log.d("List Size",list.size()+"size");
Element elements = (Element)entry.getChildNodes();
Element sPoint = (Element) elements.getElementsByTagName("start_location");
Element ePoint = (Element)elements.getElementsByTagName("end_location");
Point sP = new Point(sPoint.getFirstChild().getNodeValue(),sPoint.getLastChild().getTextContent());
Point eP = new Point(ePoint.getFirstChild().getTextContent(),ePoint.getLastChild().getTextContent());
String durationValue = elements.getElementsByTagName("duration").item(0).getNodeValue();
Log.d("Duration",durationValue);
String durationText = elements.getElementsByTagName("duration").item(1).getNodeValue();
String instr= elements.getElementsByTagName("html_instructions").item(0).getNodeValue();
String distanceValue =elements.getElementsByTagName("distance").item(0).getNodeValue();
String distanceText = elements.getElementsByTagName("duration").item(1).getNodeValue();
p = new Path(sP,eP,durationText,distanceText,instr);
list.add(p);
Log.d("List Size",list.size()+"size");
}
}
}
finally{}
}
}
catch(MalformedURLException e1){
Log.d("HTTP_CALL","MalformedURLException");
}
catch(ParserConfigurationException e1){
Log.d("HTTP_CALL","ParserConfigurationException");
}
catch(SAXException e1){
Log.d("HTTP_CALL","SAXException");
}catch(IOException e1){
Log.d("HTTP_CALL","IOException");
}
您可以添加異常消息嗎? – Craig
10-22 17:22:12.313:D/HTTP_CALL(16025):expected:/ leg read:start_location(position:END_TAG start_location> @ [email protected]中的@ 1:121) – user1874435
這是什麼錯誤意思? – user1874435