I have:
1 - A XML file and XSD schema.
2 - I want validate it in a Delphi Project. (Delphi 6 update 2 -
Win2000Pro).
If I create a Form with e TXMLDocument component and write:
XMLDocument1.ParseOptions:= [poValidateOnPArse];
XMLDocument1.Active:= true;
XMLDocument1.LoadFromFile('MyXMLFile.xml');
the validation works bad. In fact if I have a NON-Validated document XML
(checked with XML-Spy) there isn't any Error Message !
I've also read the MS documentation of msxml 4.0 SDK and found example with
the activeX: DOMDocument.4.0.
So coping the example in JScript it works good:
var x = new ActiveXObject("MSXML2.DOMDocument.4.0");
x.async = false;
x.validateOnParse = true;
x.load("MyXMLFile.xml");
if (x.parseError.errorCode != 0)
{
WScript.Echo("errorReason=" + x.parseError.reason);
}
else
WScript.Echo("===NO PARSE ERROR===");
The question is...
I have to use DomDocument40 in my Delphi project (that means including the
Type Library msxml4) or I can use better the TXMLDocument ?
Any Idea?
Thanks a lot.
James
You have to use MSXML directly (either through the type-library or through
OLE-automation). The Schema has to be added to the Schema Cache in MSXML I
think. But currently in TXMLDocument it is not possible.
Hope this helps,
--
Jeff Rafter
Defined Systems
http://www.defined.net
XML Development and Developer Web Hosting
Hi Jeff.
I've chose this way for validating my xml docs...
Xmldoc := CoDOMDocument40.Create;
Xmldoc.async := False;
Xmldoc.load(OpenDlg.FileName);
Error := xmldoc.validate;
if error.errorCode <> S_OK then
begin
lblReason.Caption := Error.reason;
end
else
lblReason.Caption := 'Valid XML';
Now there is another problem: using this little project i find NON-VALID XML
documents, but using for example XMLspy they are valid !!
I think there are some problems with the msxml4.dll, or the xml-spy
validation isn't really correct ?
So on.... some xml documents are NO valid for both validator but the reason
is different in each one..
I'm driving crazy !!!
Thanks a lot !
James
Try it with this instead:
XMLDocument1.ParseOptions:= [poValidateOnPArse, poResolveExternals];
And let me me know if it then works correctly. It works for me when I do that.
Mark
Hi Mark!!
I've tried your idea:
XmlDoc1: TXMLDocument;
XmlDoc1.ParseOptions:= [poValidateOnParse, poResolveExternals];
try
XmlDoc1.LoadFromFile(OpenDlg.filename);
except
On E: EDOMParseError do
begin
Validated:= False;
lblValid.Caption:= 'NOT VALID';
Memo1.Lines.Add(IntToStr(E.ErrorCode));
Memo1.Lines.Add(E.Reason);
Memo1.Lines.Add(IntToStr(E.Line));
end;
end;
I used TRY.. EXCEPT otherwise this doesn't work (and deselected
Integrated Debuger in Debugger Options).
Now I can validate a document using TXMLDocument component but the
other problem is present yet: some XML docs are valid for XMLSpy but
not for MS.
I'll search the solution on the web...
Thanks a lot !!!
James
Great. Thanks for reporting back.
> I used TRY.. EXCEPT otherwise this doesn't work (and deselected
> Integrated Debuger in Debugger Options).
Yes, that's what you would need to do if you don't want the default exception
handling (which is to display the exception information in a dialog).
> I'll search the solution on the web...
Make sure you have the latest XML Spy. I haven't played much with the schema
validation in MSXML4, but everything I've tried in XML Spy gave me correct
results. If you find anything useful, please report back here if you can.
Thanks again,
Mark