xp-framework / xml
XP Framework 的 XML API
v12.0.0
2024-03-29 09:22 UTC
Requires
- php: >=7.4.0
- xp-framework/collections: ^10.0 | ^9.0 | ^8.0
- xp-framework/core: ^12.0 | ^11.0 | ^10.0
- xp-framework/reflection: ^3.1
Requires (Dev)
- xp-framework/test: ^2.0 | ^1.0
README
xml 包提供了处理 XML 的 API。
XML 数据
大多数时候,XML 用于存储数据。在这种情况下,一个完整的 DOM API 对于处理数据来说开销太大。这就是 xml.Tree 类发挥作用的地方。
此示例将打印出一个格式良好的 XML 文档
use xml\{Tree, Node}; $t= new Tree('customer'); $t->root()->setAttribute('id', '6100'); $t->addChild(new Node('name', 'Timm Übercoder')); $t->addChild(new Node('email', 'uebercoder@example.com')); echo $t->getSource(INDENT_DEFAULT);
XSL 转换
DomXSLProcessor 类使用 LibXSLT,因此支持 EXSLT 功能,如用户函数、回调、字符串函数、动态评估等。
一个简单的示例
use xml\DomXSLProcessor; use xml\TransformerException; use util\cmd\Console; $proc= new DomXSLProcessor(); $proc->setXSLFile('test.xsl'); $proc->setXMLFile('test.xml'); try { $proc->run(); } catch (TransformerException $e) { // Handle } Console::writeLine($proc->output());
XPath 查询
use xml\XPath; use util\cmd\Console; $xml= '<dialog id="file.open"> <caption>Open a file</caption> <buttons> <button name="ok"/> <button name="cancel"/> </buttons> </dialog>'; Console::writeLine((new XPath($xml))->query('/dialog/buttons/button/@name')));