domattr / exml
一个简单的XML读取器,将标记转换为对象
v0.2.0
2022-05-11 14:41 UTC
Requires
- php: ^8.0
Requires (Dev)
- pestphp/pest: ^1.8
- vimeo/psalm: ^4.8
This package is auto-updated.
Last update: 2024-09-11 19:51:23 UTC
README
一个简单的XML解析器,无论命名空间如何,都能将XML转换为对象。
安装
此软件包可以通过composer/packagist进行安装
composer require domattr/exml
使用方法
要读取和解析XML,我们只需使用read()方法
$xml = '<?xml version="1.0" encoding="utf-8?><Customer><Name>Matt</Name></Customer'; $obj = Domattr\Exml\Exml::read($xml);
使用对象
返回的对象将包含所有数据、属性和子元素。下面是一个简单的原始XML和结果对象的示例
<?xml version="1.0" encoding="utf-8"?> <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <github:Repo> <RepoName>Exml</RepoName> <UserInformation> <Name>Matt Lake</Name> <Role>Developer</Role> </UserInformation> <Url>https://github.com/mattlake/exml</Url> <Status>Public</Status> </github:Repo> </soap:Body> </soap:Envelope>
object(Domattr\Exml\Container)#4 (7) { ["version":"Domattr\Exml\Container":private]=> string(3) "1.0" ["encoding":"Domattr\Exml\Container":private]=> string(5) "utf-8" ["namespace":"Domattr\Exml\Element":private]=> string(4) "soap" ["tag":"Domattr\Exml\Element":private]=> string(8) "Envelope" ["attributes":"Domattr\Exml\Element":private]=> array(3) { [0]=> object(Domattr\Exml\Attribute)#5 (2) { ["key":"Domattr\Exml\Attribute":private]=> string(9) "xmlns:xsi" ["value":"Domattr\Exml\Attribute":private]=> string(41) "http://www.w3.org/2001/XMLSchema-instance" } [1]=> object(Domattr\Exml\Attribute)#6 (2) { ["key":"Domattr\Exml\Attribute":private]=> string(9) "xmlns:xsd" ["value":"Domattr\Exml\Attribute":private]=> string(32) "http://www.w3.org/2001/XMLSchema" } [2]=> object(Domattr\Exml\Attribute)#7 (2) { ["key":"Domattr\Exml\Attribute":private]=> string(10) "xmlns:soap" ["value":"Domattr\Exml\Attribute":private]=> string(41) "http://schemas.xmlsoap.org/soap/envelope/" } } ["children":"Domattr\Exml\Element":private]=> array(1) { ["Body"]=> object(Domattr\Exml\Element)#9 (5) { ["namespace":"Domattr\Exml\Element":private]=> string(4) "soap" ["tag":"Domattr\Exml\Element":private]=> string(4) "Body" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(1) { ["Repo"]=> object(Domattr\Exml\Element)#11 (5) { ["namespace":"Domattr\Exml\Element":private]=> string(6) "github" ["tag":"Domattr\Exml\Element":private]=> string(4) "Repo" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(4) { ["RepoName"]=> object(Domattr\Exml\Element)#16 (5) { ["namespace":"Domattr\Exml\Element":private]=> NULL ["tag":"Domattr\Exml\Element":private]=> string(8) "RepoName" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(0) { } ["value":"Domattr\Exml\Element":private]=> string(4) "Exml" } ["UserInformation"]=> object(Domattr\Exml\Element)#17 (5) { ["namespace":"Domattr\Exml\Element":private]=> NULL ["tag":"Domattr\Exml\Element":private]=> string(15) "UserInformation" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(2) { ["Name"]=> object(Domattr\Exml\Element)#20 (5) { ["namespace":"Domattr\Exml\Element":private]=> NULL ["tag":"Domattr\Exml\Element":private]=> string(4) "Name" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(0) { } ["value":"Domattr\Exml\Element":private]=> string(9) "Matt Lake" } ["Role"]=> object(Domattr\Exml\Element)#21 (5) { ["namespace":"Domattr\Exml\Element":private]=> NULL ["tag":"Domattr\Exml\Element":private]=> string(4) "Role" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(0) { } ["value":"Domattr\Exml\Element":private]=> string(9) "Developer" } } ["value":"Domattr\Exml\Element":private]=> NULL } ["Url"]=> object(Domattr\Exml\Element)#19 (5) { ["namespace":"Domattr\Exml\Element":private]=> NULL ["tag":"Domattr\Exml\Element":private]=> string(3) "Url" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(0) { } ["value":"Domattr\Exml\Element":private]=> string(32) "https://github.com/mattlake/exml" } ["Status"]=> object(Domattr\Exml\Element)#18 (5) { ["namespace":"Domattr\Exml\Element":private]=> NULL ["tag":"Domattr\Exml\Element":private]=> string(6) "Status" ["attributes":"Domattr\Exml\Element":private]=> array(0) { } ["children":"Domattr\Exml\Element":private]=> array(0) { } ["value":"Domattr\Exml\Element":private]=> string(6) "Public" } } ["value":"Domattr\Exml\Element":private]=> NULL } } ["value":"Domattr\Exml\Element":private]=> NULL } } ["value":"Domattr\Exml\Element":private]=> NULL }
使用对象
对象中的数据可以像这样访问
$obj = Exml::read($xml); // Get the username $username = $obj->Body->Repo->UserInformation->Name->value();