2013-04-07 90 views
0

我無法弄清JDOMException被抓到的原因。我將一個XML格式的字符串傳遞給SAXBulider的構建函數以獲得Document,這裏引發了一個異常。找不到JDOMException的原因SAXBuilder

其被存儲在字符串結果

XML字符串:

<?xml version='1.0' encoding='ISO-8859-1'?><results><result cover="http://cps-static.rovicorp.com/3/JPG_170/MI0002/213/MI0002213251.jpg?partner=allrovi.com" title="Hey-Ya" artist="A.D.D." year="N/A" genre="Pop/Rock" details="http://www.allmusic.com/album/hey-ya-mw0001029555"/><result cover="http://cps-static.rovicorp.com/3/JPG_170/MI0003/152/MI0003152820.jpg?partner=allrovi.com" title="Heyma" artist="Abir Nasraoui" year="N/A" genre="Latin, Pop/Rock" details="http://www.allmusic.com/album/heyma-mw0002115440"/><result cover="http://cs-server.usc.edu:14186/album.jpg" title="Heyla" artist="Candy" year="2003" genre="R&B" details="http://www.allmusic.com/album/heyla-mw0000698853"/><result cover="http://cps-static.rovicorp.com/3/JPG_170/MI0002/172/MI0002172691.jpg?partner=allrovi.com" title="Heya" artist="Jimmy Stallings" year="2003" genre="International" details="http://www.allmusic.com/album/heya-mw0000336392"/><result cover="http://cps-static.rovicorp.com/3/JPG_170/MI0003/361/MI0003361503.jpg?partner=allrovi.com" title="Heya" artist="David Jones" year="N/A" genre="Electronic" details="http://www.allmusic.com/album/heya-mw0002388044"/></results> 

和代碼引起異常的部分是:使用的PrintWriter是如下生成的

SAXBuilder builder = new SAXBuilder(); 

    String temp=""; 

    out.println("Inside Servlet :::: "); 

    try 
    { 
     out.println("1. HERE"); 
     Document doc = builder.build(new StringReader(results)); 
     out.println("2. HERE"); 
     Element root=doc.getRootElement(); 
     out.println(root); 
     List resultChildren=root.getChildren(); 
     out.println("3. HERE"); 
     if(resultChildren.size()==0) 
     { 
      out.println("{\"results\":[]}"); 
      return; 
     }   
     temp="{\"results\":{\"result\":["; 
     for(int i=0;i<resultChildren.size();i++) 
     { 
      Element tempElem = (Element)(resultChildren.get(i)); 

      if(i>0) 
      temp+=","; 

      if((request.getParameter("type")).equals("Artists"))      
       temp+=parseArtists(tempElem);     
      else if((request.getParameter("type")).equals("Albums")) 
       temp+=parseAlbums(tempElem); 
      else      
       temp+=parseSongs(tempElem);               
     } 
     temp+="]}}"; 
    } 
    catch(JDOMException ex) 
    { 
     errors+="1.Could not parse xml file";   
    } 
    catch(IOException ex) 
    { 
     errors+="2.Could not parse xml file";   
    } 

輸出:

Inside Servlet :::: 
1. HERE 
{"errors": {"1.Could not parse xml file"}} 

因此,在doc = builder.build(new StringReader(results))中引發了一個異常;

請指導我解決這個問題。

+0

打印異常的堆棧跟蹤。異常信息中解釋了異常的原因。拋出異常的位置在異常堆棧跟蹤中。忽略這些信息是阻止你理解你爲什麼得到它的原因。 – 2013-04-07 09:26:20

回答

2

打印堆棧跟蹤(或至少是異常消息)後,您會看到輸入XML格式不正確:存在包含字符串"R&B"的屬性。 &符恰好是必須逃脫的少數幾個字符之一,在這種情況下,它應該是:"R&amp;B"