Michal
Michal

Reputation: 15669

Handling bad XML from HttpRequest

I have a problem with XML parser - I receive a XML via:

public String executeHttp() {
    try {
        post.setEntity(new UrlEncodedFormEntity(nvp));
        resp = client.execute(post);
        HttpEntity ent = resp.getEntity();

        return EntityUtils.toString(ent, "UTF-8");
    } catch (ParseException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;
}

and then give it to the parser:

Xml.parse(this.executeHttp(), this);

which works just fine, it parses what I need and gosh, life is great...however. I have just found out that I get some serious exceptions on real devices, where the internet connection isn't that great thanks to the fact that my parser isn't handling situations such as: "I didn't get a proper XML.", "That XML didn't make it really through." or "Uhm..this XML doesn't make any sense, really."

I believe it has to be somewhere in the parser. Any good advices where this "XML verification" should be placed?

Thanks a lot!

Upvotes: 1

Views: 121

Answers (2)

Michal
Michal

Reputation: 15669

Problem solved! It was right in front of me the whole time!

try {
        Xml.parse(this.executeHttp(), this);

    } catch (Exception e) {
        this.execute(XMLchoice, objId, selectedTime);
    }

Instead of taking down the whole app, I'm catching an exception and repeating downloading the XML file until it's complete.

Upvotes: 1

Marvin Pinto
Marvin Pinto

Reputation: 31008

What you're looking for is to validate the XML data you received from your HTTP request against an XML Schema file (xsd document). Seeing as you've never done this before, I would start here and first create a valid schema, and then move on to the Java validation portion.

Upvotes: 0

Related Questions