无法弄清楚JDOMException SAXBuilder的原因

时间:2013-04-07 09:05:48

标签: json servlets saxparser jdom

我无法弄清楚JDOMException被捕获的原因。我将XML格式字符串传递给SAXBulider的构建函数以获取Document,这里抛出异常。

存储在String结果中的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>

导致异常的代码部分是:

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";           
    }

使用PrintWriter生成的输出如下:

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

因此,在doc = builder.build(new StringReader(results))中抛出异常;

请指导我完成这个问题。

1 个答案:

答案 0 :(得分:2)

在打印堆栈跟踪(或至少是异常消息)后,您将看到输入XML格式不正确:有一个包含字符串"R&B"的属性。 Ampersand恰好是必须转义的少数几个字符之一,在这种情况下它应该是:"R&amp;B"