horat1us / simple-xml
SimpleXML 扩展
1.0.0
2019-03-08 19:38 UTC
Requires
- php: >=7.2
Requires (Dev)
- phpunit/phpunit: ^8.0
- squizlabs/php_codesniffer: ^3.4
This package is auto-updated.
Last update: 2024-09-09 13:58:43 UTC
README
本包为 simple xml 提供了一些扩展
安装
composer require wearesho-team/simple-xml
使用
解析
此类代表一个解析器,它将 simplexml_load_string
和 simplexml_load_file
包装在错误包装器中,因此您将能够捕获异常。
文件解析
<?php use Horat1us\SimpleXML; $path = 'valid.xml'; try { $element = SimpleXML\Parse::file($path/*, $className*/); } catch(SimpleXML\Parse\Exception\InvalidFilePath $e) { $path = $e->getPath(); } catch(SimpleXML\Parse\Exception\InvalidFormat\File $e) { $filePath = $e->getPath(); $libXmlErrors = $e->getErrors(); } catch(SimpleXML\Parse\Exception\InvalidClassName $e) { $className = $e->getClassName(); }
字符串解析
<?php use Horat1us\SimpleXML; $path = 'valid.xml'; try { $element = SimpleXML\Parse::file($path/*, $className*/); } catch(SimpleXML\Parse\Exception\InvalidFormat\Data $e) { $contents = $e->getContents(); $libXmlErrors = $e->getErrors(); } catch(SimpleXML\Parse\Exception\InvalidClassName $e) { $className = $e->getClassName(); }
错误
- Parse\Exception - 所有解析异常的基础异常接口。
- Parse\Exception\InvalidFormat - libxml 错误的基础异常。
- Parse\Exception\InvalidFormat\Data - 字符串解析错误。
- Parse\Exception\InvalidFormat\File - 文件解析错误。
- Parse\Exception\InvalidClassName - 表示传入的类名不存在的情况。
- Parse\Exception\InvaldiFilePath - 表示传入的文件路径不存在或不是文件路径的情况。
- Parse\Exception\InvalidFormat - libxml 错误的基础异常。