This content has been marked as final. Show 7 replies
Ok, so I am a bit slow on the uptake. I figured out those are line numbers and positions.
So here's another related question. Does anyone know how I can look for that line number and pull the string on that line out of the xml file? I'd like to show where/what the error is and perhaps translate the error code into english while I'm at it.
Did you ever get an answer to this? I'm looking to do the same thing.
Doing a Google search on "cvc-length-valid", it seems that this is probably the sort of exception Xerces raises when an XSD validation fails.
You might want to read the Xerces docs for more info.
Or you might want to settle for outputting an error message along the lines of "that XML you gave me was bung", and leave it at that.
As for working out line numbers, the lines are delimited by chr(10) and/or chr(13), so treat the XML as a list delimited by those characters at pull out the nth line. Then the character position is easy enough to extract from the line itself.
In my case, I need to tell the user exactly where the error occurred. I'm converting a CSV file with thousands of rows into a Coldfusion XML object. Unfortunately, the lines are not delimited using this method. You get two lines in your XML document: the XML declaration on line 1 and the rest on line 2.
> In my case, I need to tell the user exactly where the error occurred. I'm
> converting a CSV file with thousands of rows into a Coldfusion XML object.
I'd approach it differently (given the error you're getting is no help to
you). Validate the data before you put it into the XML string. If you've
got an XSD that says "this attribute needs to be this sort of value", then
you can do the same validation as you populate the XML.
If all your XML is in one long line without CR/LF characters in it, then
the error message reporting line:col is not a reference to that data. It
might be a reference to the XSD file?
I got it... thanks Adam for both of your posts. They got my wheels turning. Each item in my XML object (element name "item") contains several children; I'm interested in returning all of the children for that item.
After doing my conversion, I get an XML object called myXmlObj. I immediately take that XML object and do:
<cfset myXmlObj = Replace(ToString(myXmlObj),"<item>",Chr(10) & "<item>", "all") />
which gives me a delimited list that matches the line numbers not only in the original CSV but also in the XmlValidate error results.
When I get a validation error (XmlValidate results stored in "myResults"), I loop through the returned errors and parse the error message as a list (using : as a delimeter) to get the line number using ListGetAt().
<cfset errorLine = ListGetAt(myResults.errors ,2,":") />
I now have a reference to the XML item number and return it and it's children from myXmlObj using:
<cfset xmlSnip = XmlParse(ListGetAt(myXmlObj,errorLine,Chr(10))) />
Now I can loop through the xmlSnip XML object, put the results in a table, and show the user exactly where the error occurred in the context of the entire record.
I will document this better and post to a blog entry. When I do, I'll post a link here with a better example of how it works.
Thanks again for the assist!!