php xml schemavalidate,如何根據模式(php或perl或可能的c++)驗證XML

本文讨论了如何验证XML文档并基于验证结果调用特定函数。建议使用PHP的DOMDocument类进行XML与XSD schema的验证,并通过libxml的错误处理函数捕获并处理错误。在验证成功后,可以依据预设命令调用相应的功能,如updateContactList。
摘要由CSDN通过智能技术生成

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

我需要接收XML並針對一個模式文件驗證它。之后,我必須根據命令調用一個函數(示例updateContactList)。最好的方法是什么?我擔心驗證XML(以及報告錯誤),我不知道將數據放入函數中運行的最佳方式是什么

-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

-編輯-注意:通過驗證模式,我需要驗證(regex)模式。如果我能用XML和模式調用一個函數,並讓它返回false + error msg或true,那就太好了

1 个解决方案

#1

7

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:

我不知道你說的regex模式是什么意思?驗證XML文檔的最常見方法是使用XSD。您可以使用DomDocument->schemaValidate進行以下操作:

$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.

還有相應的DomDocument->弛緩性驗證,用於針對使用較少的弛緩性模式進行驗證。

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.

如果您打算捕獲錯誤並對其進行處理,而不是僅僅驗證true或false,那么您可能還希望使用libxml的錯誤處理程序函數。從本質上講,叫libxml_use_internal_errors(真正的);在加載和驗證文檔之前,使用libxml_get_errors和display_xml_error獲取錯誤。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值