Vizzard
Vizzard

Reputation: 55

Cannot figure out reason for JDOMException SAXBuilder

I am not able to figure out the reason for JDOMException being caught. I am passing an XML format string to the SAXBulider's build function for getting a Document, an exception is thrown here.

XML String which is stored in String results:

<?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>

And the part of code causing exception is:

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

Output generated using PrintWriter is as follows:

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

Thus, there is an exception thrown at doc=builder.build(new StringReader(results));

Kindly guide me through this problem.

Upvotes: 0

Views: 385

Answers (1)

forty-two
forty-two

Reputation: 12817

And after having printed the stack trace (or at least the exception message) you'll see that the input XML is not well formed: There's an attribute containing the string "R&B". Ampersand happens to be one of the few characters that has to be escaped, in this case it should read: "R&amp;B".

Upvotes: 2

Related Questions