Question

I'm write a Air application that consumes a beta webservice API. Sometimes this APIreturns me a malformed XML node and actionscript will raise an error when I try creating the XML object out of it.

What I've been doing is just try/catching the result and ignore the whole response if the XML is bad, but I could just ignore the malformed node.

For example:

<result>
  <Song>
    <location>http://www.anyurl.com/audio/loftparty092108_pt4.mp3</location>
    <title>Phonte party @ The Loft, 9/21/08 (pt. 4)</title>
    <artist>Statik, Jahsonic &amp; Stylus        <artist>Statik, Jahsonic &amp; Stylusre />
  </Song>
</result>

See that tag? If I fould something like that, I'd like to skip the whole Song and get the next one.

Is there any way I could accomplish this?

Was it helpful?

Solution

  1. Write to the webservice provider. Tell them about the malformed XML. They might just fix it.
  2. When a malformed XML is passed back Flex's default XML decoder is at a loss and throws.
  3. If you want to try processing roll your own decoder. See the xmlDecode member of HTTPService.

OTHER TIPS

Beautiful Soup does this, but it is written in Python. You could of course always go digging through the source, and see how they implemented it.

If the number of class of errors is small, pre-process the XML to remove the error, then hand it to your parser. This will enable you to remove the workaround easily when the issue is fixed. In the above case, you would load the file as text, look for nodes with duplicate tags, and just remove the entire node from the XML text.

Or just notify the web service provider and wait - if they're returning malformed XML just about every parser will choke on it and the should have an incentive to fix it soon.

No, you cannot ignore part of a malformed XML document with an normal XML parser. It's like asking the Flex compiler to ignore syntax errors and figure out what the coder really meant to do. You will have to write your own parser that tries to infer what the malformation is and what it can ignore.

Honestly, any Web Service, even a beta, that sends bad XML is not dependable. It implies that they are composing the XML "by hand" instead of using a programmatic class. Anyone doing that is likely to make lots of other errors, especially regressions. Your time is too valuable to depend on them.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top