简体   繁体   中英

How do i validate XML against a schema (php or perl or possible C++)

I need to take in XML and validate it against a schema file. Afterward i must call a function based on the command (example updateContactList). What is the best way to do this? I am worried about validating the XML (and report errors) and i have no idea what is the best way to put the data into a function to run

-edit- NOTE: By validating the schema i need to validate the (regex) pattern. It would be great if i can call a function with the XML and schema and have it return false + error msg or true

I'm not sure what you mean by regex pattern? The most common way to validate an XML document is trough an XSD . You can use DomDocument->schemaValidate for this:

$doc = new DOMDocument();
$doc->load($tempFile);
$doc->schemaValidate('schema.xsd');

There is also the corresponding DomDocument->relaxNGValidate for validating against the lesser used RelaxNG schema.

You may also want to use the error-handler functions for libxml , if you plan on capturing the errors and doing something about them, rather than just validate true or false. In essence, call libxml_use_internal_errors(true); before loading and validating the document, and use libxml_get_errors and display_xml_error to fetch the errors.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM