dim. 22 janvier 2012, 19h38
Gregory
atoutfox.public.association
Re: Validateur XML
Correction en rouge
Il faut mettre xmlObj.Schemas = m.schemaObj apres le load de l’xml
=m.xmlObj.Load(m.xmlFile)
xmlObj.Schemas = m.schemaObj
Gregory
"Gregory" <NoOne@NoWhere.com> wrote in message news:jfhkc6$2u8j$1@news.niouzes.net...
Mike,
Ne pas le faire avec xmlAdapter qui utilise msxml4 et qui repond parfois: Schema is too complex
Essaye ceci
(1) Le fichier HeadCount.xsd contient
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<xs:schema xmlns="xsdHeadCount" targetNamespace="xsdHeadCount" xmlns:xs="
http://www.w3.org/2001/XMLSchema">
<xs:element name='HeadCount' type="HEADCOUNT"/>
<xs:complexType name="HEADCOUNT">
<xs:sequence>
<xs:element name='Name' type='xs:string' maxOccurs='unbounded'/>
</xs:sequence>
<xs:attribute name='division' type='xs:int' use='optional' default='8'/>
</xs:complexType>
</xs:schema>
(2) HeadCount.xml contient
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<hc:HeadCount xmlns:hc='xsdHeadCount' xmlns:xsi='
http://www.w3.org/2001/XMLSchema-instance'>
<Name>Waldo Pepper</Name>
<Name>Red Pepper</Name>
</hc:HeadCount>
(3) validation
local xmlFile, xsdFile
xmlFile = 'd:\tmp\HeadCount.xml'
xsdFile = 'd:\tmp\HeadCount.xsd'
local xsdObj, schemaObj, xmlObj
&& read schema
xsdObj = createObject('Msxml2.DOMDocument.6.0')
xsdObj.Async = .f.
xsdObj.ValidateOnParse = .t.
=m.xsdObj.Load( m.xsdFile)
schemaObj = createObject('Msxml2.XmlSchemaCache.6.0')
local nameSpace
nameSpace = 'xsdHeadCount'
=m.schemaObj.Add(m.nameSpace, m.xsdObj)
xmlObj = createObject('Msxml2.DOMDocument.6.0')
xmlObj.Async = .f.
xmlObj.ValidateOnParse = .t.
=m.xmlObj.Load(m.xmlFile)
xmlObj.Schemas = m.schemaObj
local parseError
parseError = m.xmlObj.Validate()
#if .f.
.errorCode Contains the error code of the last parse error.
.filepos Contains the absolute file position where the error occurred
.line Specifies the line number that contains the error.
.linepos Contains the character position within the line where the error occurred.
.reason Describes the reason for the error
.srcText Returns the full text of the line containing the error.
.url Contains the URL of the XML document containing the last error.
#endif
if( parseError.errorCode == 0 )
?'OK'
else
? 'error:' , parseError.errorCode
? 'reason:' , parseError.reason
?'source', parseError.srcText
cliptext = parseError.reason
endif
A ajouter:mettre les Load() dans un try/catch
Gregory
"Mike Gagnon" wrote in message news:jfh740$2ibg$1@news.niouzes.net...
Il y a t-il quelqu'un qui connait un methode pour valider un fichier XML contre un ficher XSD?
Je crois que la classe XMLAdaptor le fait. Mais je ne trouve pas d'exemple.
Merci
Permalink : http://www.atoutfox.org/nntp.asp?ID=0000008023