Since there is no XML validation currently in the Flex/Flash framework, I've decided to start a class that parses a string in an effort to validate the markup, returning whether the XML is valid. I have a need for it in my non-server based application, and have read in numerous places where there is desire for it as well. I have built the class far enough to get the ball rollin', but figure it should be :
A) available to the community
B) able to be improved upon by the community
If you improve upon the code, please post your work here so everyone can benefit.
Here is the source code :
/*
////////////////////////////////////////////////////////////////////// //////////
XML Validator : v0.2 - last edit by Justin Myers | J2 CREATIVE MEDIA DESIGN
NOTES:
Parser is a bit weak and needs work.
Still need to :
- Make sure tags are ended properly
- Make sure there is space between tag name and attribute
Parser currently handles :
- Making sure there is no space between tags (with exception to white space)
- Making sure that attributes open and close properly
- Making sure there is proper space after an attribute or that it is immediately followed with the tag closing
////////////////////////////////////////////////////////////////////// //////////
*/
package community.classes.parsers
{
publicclass XMLValidator
{
publicfunction XMLValidator()
{
}
publicstaticfunction validate(str:String , ignoreWhiteSpace:Boolean = true):Boolean
{
// validation defaults to true (innocent til proven guilty)
varvalid:Boolean = true;
// minimum char length to be valid XML
if (str.length < 4)
{
valid = false;
}
var withinTag:Boolean;
var withinAttribute:Boolean;
var tags:Array = [];
var tag:String;
for (var i:uint = 0 ; i < str.length ; i++)
{
var char:String = str.charAt(i);
// if we are closing a tag
if (char == ">")
{
// invalid if we never opened a tag, or if we never closed the last attribute
if (!withinTag || withinAttribute)
{
valid = false;
break;
}
else
{
withinTag = false;
tags.push(tag);
}
}
// invalid if last character is not a closing tag
elseif (i == str.length - 1)
{
valid = false;
break;
}
// if we are entering a tag
elseif (char == "<")
{
// invalid if we haven't closed the last tag
if (withinTag)
{
valid = false;
break;
}
else
{
withinTag = true;
tag = "";
}
}
// all other characters
else
{
if ((char != " " || char != "\n") || ((char == " " || char == "\n") && !ignoreWhiteSpace) )
{
// invalid if there are any characters between tags
if (!withinTag)
{
valid = false;
break;
}
else
{
if (char == "\"")
{
// entering attribute
if (!withinAttribute)
{
// invalid if = does not preclude ", or there is space before =
if (str.charAt(i-1) != "=" || str.charAt(i-2) == " ")
{
valid = false;
break;
}
else
{
withinAttribute = true;
}
}
// exiting attribute
else
{
withinAttribute = false;
// invalid if no space after attribute closing, or the tag is not closing
if (str.charAt(i+1) != " "&& str.charAt(i+1) != "/"&& str.charAt(i+1) != ">")
{
valid = false;
break;
}
}
}
tag += char;
}
}
}
}
returnvalid;
}
}
}