题
我试图验证对它所引用的架构的XML文件。 (使用Delphi和MSXML2_TLB。)代码(的相关部分)看起来像这样:
procedure TfrmMain.ValidateXMLFile;
var
xml: IXMLDOMDocument2;
err: IXMLDOMParseError;
schemas: IXMLDOMSchemaCollection;
begin
xml := ComsDOMDocument.Create;
if xml.load('Data/file.xml') then
begin
schemas := xml.namespaces;
if schemas.length > 0 then
begin
xml.schemas := schemas;
err := xml.validate;
end;
end;
end;
这具有该高速缓存被加载的结果(schemas.length > 0
),但随后的下一个任务引发一个例外:“仅XMLSchemaCache-schemacollections可以用”
我应该如何去吗?
解决方案
我已经想出了,似乎工作的方法。我第一次加载架构的明确,然后themn添加到schemacollection。接下来,我加载XML文件并分配schemacollection其架构属性。该解决方案现在看起来像这样:
uses MSXML2_TLB
That is:
// Type Lib: C:\Windows\system32\msxml4.dll
// LIBID: {F5078F18-C551-11D3-89B9-0000F81FE221}
function TfrmMain.ValidXML(
const xmlFile: String;
out err: IXMLDOMParseError): Boolean;
var
xml, xsd: IXMLDOMDocument2;
cache: IXMLDOMSchemaCollection;
begin
xsd := CoDOMDocument40.Create;
xsd.Async := False;
xsd.load('http://the.uri.com/schemalocation/schema.xsd');
cache := CoXMLSchemaCache40.Create;
cache.add('http://the.uri.com/schemalocation', xsd);
xml := CoDOMDocument40.Create;
xml.async := False;
xml.schemas := cache;
Result := xml.load(xmlFile);
if not Result then
err := xml.parseError
else
err := nil;
end;
有使用XMLSchemaCache40或更高重要。早期版本不遵循W3C XML Schema标准,但只有验证对XDR架构,微软规范。
该解决方案的缺点是,我需要显式加载架构的。在我看来,它应该是可以自动检索。
其他提示
虽然BennyBechDk可能是在正确的轨道上,我与他的代码,我要去下面来纠正一些问题:
uses Classes, XMLIntf, xmlDoc, SysUtils;
function IsValidXMLDoc(aXmlDoc: IXMLDocument): boolean;
var
validateDoc: IXMLDocument;
begin
result := false; // eliminate any sense of doubt, it starts false period.
validateDoc := TXMLDocument.Create(nil);
try
validateDoc.ParseOptions := [poResolveExternals, poValidateOnParse];
validateDoc.XML := aXmlDoc.XML;
validateDoc.Active := true;
Result := True;
except
// for this example, I am going to eat the exception, normally this
// exception should be handled and the message saved to display to
// the user.
end;
end;
如果您希望系统只引发异常,那么就没有理由让它摆在首位的功能。
uses Classes, XMLIntf, XMLDoc, SysUtils;
procedure ValidateXMLDoc(aXmlDoc: IXMLDocument);
var
validateDoc: IXMLDocument;
begin
validateDoc := TXMLDocument.Create(nil);
validateDoc.ParseOptions := [poResolveExternals, poValidateOnParse];
validateDoc.XML := aXmlDoc.XML;
validateDoc.Active := true;
end;
由于validateDoc是一个接口,它会以适当方式处理作为函数/过程退出,就没有必要自己执行处置。如果你打电话ValidateXmlDoc并没有得到一个例外,那么它是有效的。我个人比较喜欢第一个呼叫,IsValidXMLDoc返回true,如果有效,或者如果不是假的(和自身之外不会引发例外)。
予制作的Miel's方案来解决disadventage。我打开XML两次,第一次得到的命名空间,和其他的,之后创建架构集合,以验证该文件。这个对我有用。 这似乎是IXMLDOMDocument2,一旦打开,鸵鸟政策接受设置架构属性。
function TForm1.ValidXML2(const xmlFile: String;
out err: IXMLDOMParseError): Boolean;
var
xml, xml2, xsd: IXMLDOMDocument2;
schemas, cache: IXMLDOMSchemaCollection;
begin
xml := CoDOMDocument.Create;
if xml.load(xmlFile) then
begin
schemas := xml.namespaces;
if schemas.length > 0 then
begin
xsd := CoDOMDocument40.Create;
xsd.Async := False;
xsd.load(schemas.namespaceURI[0]);
cache := CoXMLSchemaCache40.Create;
cache.add(schemas.namespaceURI[1], xsd);
xml2 := CoDOMDocument40.Create;
xml2.async := False;
xml2.schemas := cache;
Result := xml2.load(xmlFile);
//err := xml.validate;
if not Result then
err := xml2.parseError
else
err := nil;
end;
end;
我先前验证使用以下代码的XML文档:
Uses
Classes,
XMLIntf,
SysUtils;
Function ValidateXMLDoc(aXmlDoc: IXMLDocument): boolean;
var
validateDoc: IXMLDocument;
begin
validateDoc := TXMLDocument.Create(nil);
validateDoc.ParseOptions := [poResolveExternals, poValidateOnParse];
validateDoc.XML := aXmlDoc.XML;
validateDoc.Active := true;
Result := True;
end;
不隶属于 StackOverflow