Here's the XML validation function for ColdFusion MX that I promised
yesterday. It requires JDOM (http://www.jdom.org/) which MX uses internally,
so it should run on all MX installations.
Hope this helps - let me know if you have any problems.
Tom
<!--- XML validation function --->
<cffunction name = "XMLValidate" returntype="boolean">
<cfargument name="XMLSource" required="true">
<cfobject
class = "org.jdom.input.SAXBuilder"
name = "SAXBuilder"
action="create"
type = "java">
<cfset setVal = SAXBuilder.setValidation(true)>
<cfset XMLValidateError = "">
<cfset XMLValid = 1>
<cftry>
<cfset trybuild = SAXBuilder.build(XMLSource)>
<cfcatch>
<cfset XMLValidateError = cfcatch.message>
<cfset XMLValid = 0>
</cfcatch>
</cftry>
<cfreturn XMLValid>
</cffunction>
<!--- example usage --->
<cfset checkDoc = XMLValidate('http://torchbox.com/xml/test.xml')>
<cfif CheckDoc>
The XML document is valid
<cfelse>
There is an error in the XML document: <br />
<cfoutput>#XMLValidateError#</cfoutput>
</cfif>
> I'll at least throw a response out... nope, not doing validation here.
>
> And I can't find any docs or ANY information on how to do so straight from
> MX. I'm not saying it can't be done, just that I haven't been able to find
> out how.
>
> I would say this, though... since with MX you are in a Java enviroment you
> could conceivable use java to accomplish this task.
>
> My $.02.
>
> Jason
> -----Original Message-----
>
> Is there any way to actually validate an XML doc against a DTD from within
> MX?
>
> The idea being to provide a friendly error message if it fails.
>
> What are some ways people are validating XML? Even when I try to define the
> DTD within the XML file itself (<!DOCTYPE rootelement SYSTEM
> "somedocdef.dtd">), no matter what path I try to define, MX's parser
> complains that it "can not resolve the relative URI without a base URI".
>
> I'm surprised there isn't a <XMLValidate> tag.
>
> Thanks,
> -Carl
-----------------+
tom dyson
t: +44 (0)1608 811870
m: +44 (0)7958 752657
http://torchbox.com
-----------------------+
cf-xml mailing list
http://torchbox.com/xml/list.cfm