views:

565

answers:

4

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&lt;/location&gt;
    <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?

+1  A: 

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.

grieve
+1  A: 
  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.
dirkgently
1. I did already. I just wanted to do a quick fix on my code since I don't know when they will fix it.2. I noticed that. :)3. I'll look into that, but I'm not using HTTPService, I'm using my own implementation of URLLoader.
leolobato
In that case you'll need a custom handler for the complete event.
dirkgently
+1  A: 

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.

Michael
+1  A: 

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

Richard Haven
It's not at all the same as asking a compiler to guess code semantics. For example, simply cutting off the malformed node from the parse tree and discarding it is perfectly reasonable when you know you don't care about the data in that branch and nothing better is available. Real-world programmers don't always have the luxury of choosing their data sources and dealing with perfectly spec-compliant data. (Perhaps your bosses have taken the decision to work with company X, and there's nothing you can do about it.) Sometimes the data you need to work with in order to do your job isn't pretty.
Paul Legato