var*_*uog 18 php xml validation xsd xml-parsing
如何针对xsd验证xml文件?有domdocument :: schemaValidate()但它没有告诉错误在哪里.那有什么课吗?它是否有任何值得从头开始的解析器?还是只是重新发明轮子,
Ed *_*eal 23
此代码执行业务:
$xml= new DOMDocument();
$xml->loadXML(<A string goes here containing the XML data>, LIBXML_NOBLANKS); // Or load if filename required
if (!$xml->schemaValidate(<file name for the XSD file>)) // Or schemaValidateSource if string used.
{
// You have an error in the XML file
}
Run Code Online (Sandbox Code Playgroud)
请参阅http://php.net/manual/en/domdocument.schemavalidate.php中的代码以检索错误.
即
justin at redwiredesign dot com 08-Nov-2006 03:32 post.
小智 8
来自http://php.net/manual/en/domdocument.schemavalidate.php的用户贡献
它就像一个魅力!
有关DOMDocument :: schemaValidate的更详细反馈,请禁用libxml错误并自行获取错误信息.有关详细信息,请参阅 http://php.net/manual/en/ref.libxml.php.
的example.xml
<?xml version="1.0"?>
<example>
<child_string>This is an example.</child_string>
<child_integer>Error condition.</child_integer>
</example>
Run Code Online (Sandbox Code Playgroud)
example.xsd
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="example">
<xs:complexType>
<xs:sequence>
<xs:element name="child_string" type="xs:string"/>
<xs:element name="child_integer" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Run Code Online (Sandbox Code Playgroud)
PHP
<?php
function libxml_display_error($error)
{
$return = "<br/>\n";
switch ($error->level) {
case LIBXML_ERR_WARNING:
$return .= "<b>Warning $error->code</b>: ";
break;
case LIBXML_ERR_ERROR:
$return .= "<b>Error $error->code</b>: ";
break;
case LIBXML_ERR_FATAL:
$return .= "<b>Fatal Error $error->code</b>: ";
break;
}
$return .= trim($error->message);
if ($error->file) {
$return .= " in <b>$error->file</b>";
}
$return .= " on line <b>$error->line</b>\n";
return $return;
}
function libxml_display_errors() {
$errors = libxml_get_errors();
foreach ($errors as $error) {
print libxml_display_error($error);
}
libxml_clear_errors();
}
// Enable user error handling
libxml_use_internal_errors(true);
$xml = new DOMDocument();
$xml->load('example.xml');
if (!$xml->schemaValidate('example.xsd')) {
print '<b>DOMDocument::schemaValidate() Generated Errors!</b>';
libxml_display_errors();
}
?>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
20875 次 |
最近记录: |