zacksitt/xml-helper

???Laravel XML Helper 包简化了在 Laravel 应用程序中处理 XML 数据的过程。它提供了直观的方法来轻松解析、生成和转换 XML,这些方法与 Laravel 生态系统无缝集成。

v1.0.0 2024-06-07 04:32 UTC

This package is auto-updated.

Last update: 2024-09-09 04:01:53 UTC


README

Laravel XML Helper 包为在 Laravel 应用程序中处理 XML 数据提供了一种简单高效的方式。它包括将数组转换为 XML、将 XML 解析为数组以及使用 XPath 查询提取值的方法。

安装

要安装此包,您可以使用 Composer

composer require zacksitt/xml-helper

Usage
Converting Arrays to XML
Use the toXML() method to convert a PHP array to an XML string.

use Zacksitt\XmlHelper\XmlHelper;

$array = [
    'root' => [
        'element' => 'value',
        'anotherElement' => 'anotherValue'
    ]
];

$xmlHelper = new XmlHelper();
$xmlString = $xmlHelper->toXml($array);

echo $xmlString;

Parsing XML to Arrays

The toArray() method parses an XML string into a PHP array.

$xmlString = '<root><element>value</element><anotherElement>anotherValue</anotherElement></root>';
$xmlHelper = new XmlHelper();
$array = xmlHelper->toArray($xmlString);

print_r($array);

Extracting Values with element.
Use the getValue() method to extract values from an XML string using an element name.

$xmlString = '<root><element>value</element><anotherElement>anotherValue</anotherElement></root>';
$xpath = 'anotherElement';

$xmlHelper = new XmlHelper();
$value = $xmlHelper->getValue($xmlString, $xpath);

echo $value; // Output: anotherValue

Methods
toXML(array $data): string
Converts a PHP array to an XML string.

toArray(string $xml): array
Parses an XML string into a PHP array.

getValue(string $xml, string $element): mixed
Extracts a value from an XML string using an XPath query.

License
This package is open-sourced software licensed under the MIT license.