jitesoft/xml2struct

简单库,用于将XML文件转换为节点树结构、数组结构或JSON字符串。

2.0.0 2017-09-14 10:27 UTC

This package is not auto-updated.

Last update: 2024-09-14 20:04:18 UTC


README

Build Status

Build Status

Dependency Status

XML2Struct

XML有时很麻烦... 有时候使用树结构或者JSON或数组会更方便!
创建此包是为了简化XML文件的测试,因此,而不是通过冗长的DOMElement测试用例,或者甚至直接字符串匹配XML,可以将其转换为更易处理的结构。
因此诞生了XML2Struct。

使用方法

通过Composer安装包

composer require jitesoft/xml2struct

解析器非常简单易用,所需做的就是创建一个新的解析器对象,并调用其parse方法,提供要解析的XML字符串

$parser = new Jitesoft\XML\Parser();
// Node tree!
$out    = $parser->parse(file_get_contents('myxmlfile.xml'));
// Json string!
$json   = json_encode($out);
// Array!
$array  = $out->toArray();

有关更深入的示例,请参阅examples文件夹。

返回的对象是Jitesoft\XML\Node结构。
该结构定义了以下字段

name:       string                  default: ""
content:    string                  default: ""
attributes: array ([key => value])  default: []
children:   array ([Node])          default: []

当将节点转换为数组或JSON字符串时,属性或键将使用与节点字段相同的名称。键始终存在,即使XML中没有值,如果没有解析的值,它将是上述默认值。

变更

2.0

  • 解析时移除了OutType。改为使用Node::toArrayjson_encode($node)
  • 移除了Node::toString重写。如果需要JSON,则作为JSON输出。

问题和PR

发现的任何问题都应在此存储库问题跟踪器中报告,当可能时,将修复这些问题。
接受PR,但请尽量遵循一般的代码风格!

许可证

MIT License

Copyright (c) 2017 Jitesoft

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.