2013-10-23 59 views
0

我在使用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"); 
    } 
+1

您可以添加異常消息嗎? – Craig

+0

10-22 17:22:12.313:D/HTTP_CALL(16025):expected:/ leg read:start_location(position:END_TAG @ [email protected]中的@ 1:121) – user1874435

+0

這是什麼錯誤意思? – user1874435

回答

1

我猜,因爲你正在讀入其逃避一些你所讀回作爲字符流字符的Java字符串。

這似乎爲我工作:

InputStream inputStream = connection.getInputStream(); 
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
DocumentBuilder db = dbf.newDocumentBuilder(); 
Document doc = db.parse(inputStream); 
+0

謝謝,我確實嘗試過這種方式,它說「文檔意外結束」。你有什麼想法如何解決這個問題? – user1874435

+0

你調用了多少次'connection.getInputStream()'?確保你只調用它一次 – Craig

+0

Ty,現在它沒有任何exception.ty – user1874435

0

我可以從錯誤信息瞭解,它在找</leg>因爲它說expected: /leg,它有一個start_location

您可能錯過了關閉<leg>標記

+0

這似乎並沒有問題,它是一個有效的XML文檔與結束標籤 – Craig