connectedpixel.com

actionscript, web development

Detecting unwell xml

Submitted by joelmay on 4 January, 2007 - 9:07am.

Note: This blog entry has been superseded by the comment from Chris below. Use the xml status property instead. Posting one's ignorance is one way to learn. I guess the corrected blog should simply say: "check the status code in addition to the onLoad success argument."

If an XML file is created manually or edited manually, it is easy to misspell a closing tag, misplace a greater than sign or forget a quote mark – it's easy to create non-well-formed xml. Flash will not be able to read it and your application will fail. Furthermore, it might load some of the data and operate in a mysterious, crippled fashion, and the cause of the problem will not be obvious.

The easiest way to prevent this is to use a xml editor that will tell you if your xml is well-formed. I use a free version of XMLSpy, but I can't find the free version on their website anymore. Visual Studio also works. I'm sure there are others.

But sometimes your client will edit the file, or a co-worker who does not use such an editor. Maybe the xml files is a configuration file that the client will want to change periodically, and maybe your client likes to use notepad. Maybe the xml is edited remotely with an ftp-based editor. If they do make a mistake, your Flash app will not work. You will get blamed.

It would be nice if there were an easy way to check whether the xml is well-formed in the Flash app itself.

Fortunately, AS3 throws an exception when it reads an xml file that is not well formed. No problem there.

Flash 8, however, does not complain at all. If it reads an invalid xml file, it merely stops parsing when it encounters a problem. Your XML onLoad function is called with success set to true. Your application will execute with partial data and who knows what will happen.

Here’s my simple solution: I add a final node to the xml named “well-formed”. It must be there. If, after parsing the xml, that node is not present, the xml is not well formed and an error message is displayed.

Here's what the xml looks like.

And here's the ActionScript:

Yes, this technique is so simple as to be trivial, but it has saved me much grief.

Spender (not verified) Says:

xml well formed?

4 January, 2007 - 9:52am

Joel,

I think the droid you are looking for is the status property of the XML. A non-zero value indicates parsing failure.

/chris

joelmay Says:

D'oh

4 January, 2007 - 9:58am

That's a far better way to handle this. Don't know how I overlooked that. Live and learn.

spender (not verified) Says:

often overlooked

4 January, 2007 - 10:13am

It's not something that is commonly checked for, and intuitively one assumes that the parameter passed to the onLoad handler represents the parsing status of the xml being loaded, when in fact, all it represents is whether or not the data was successfully retrieved. The flash docs don't highlight the importance of checking the status parameter and it is not immediately apparent that is is probably more important to check the status of the XML than the "success" argument handed to onLoad. In the best case, it would probably be better to implement one's own onData handler that incorporates this check and explicitly calls onLoad with a more representative "success" argument when these tighter constraints are met.

joelmay Says:

Thanks

4 January, 2007 - 10:28am

Thanks for making me feel less stupid.

I agree. It is a sneeky thing. The docs for XML.onLoad should mention the status property.

JabbyPanda (not verified) Says:

To check the validity of of

3 March, 2007 - 3:58am

To check the validity of of manually edited XML files I just open thme with IE browser, it has built-in decent XML viewer and highlights ill-formed XML pretty well.

jim bachalo (not verified) Says:

IE doesn't like it though if

14 March, 2007 - 6:47am

IE doesn't like it though if your node names are numbers, even if the XML is well formed, as I found out recently when converting objects to XML