Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Xml Laravel Package

thenorthmemory/xml

Lightweight XML transformer for PHP: parse XML into arrays and build XML back from arrays. Supports repeated elements/lists, optional pretty printing, custom root nodes, and wrapping arrays to control tag output. Extracted from wechatpay-php for general use.

View on GitHub
Deep Wiki
Context7

A wrapper of the XML parser and builder

Split from the wechatpay-php project for general usages.

GitHub actions Packagist Stars Packagist Downloads Packagist Version Packagist PHP Version Support Packagist License

Install

composer require thenorthmemory/xml

Usage

use TheNorthMemory\Xml\Transformer;
$array = Transformer::toArray('<xml><hello>world</hello></xml>');
// print_r($array);
// Array
// (
//     [hello] => world
// )
$xml = Transformer::toXml($array);
// print_r($xml);
// <xml><hello>world</hello></xml>
$xml = <<<TencentCOSRequest
<Request>
<Operation>
 <WatermarkTemplateId>t146d70eb241c44c63b6efc1cc93ccfc5d</WatermarkTemplateId>
 <WatermarkTemplateId>t12a74d11687d444deba8a6cc52051ac27</WatermarkTemplateId>
</Operation>
</Request>
TencentCOSRequest;
$array = Transformer::toArray($xml);
// Array
// (
//     [Operation] => Array
//         (
//             [WatermarkTemplateId] => Array
//                 (
//                     [0] => t146d70eb241c44c63b6efc1cc93ccfc5d
//                     [1] => t12a74d11687d444deba8a6cc52051ac27
//                 )

//         )

// )
$xml1 = Transformer::toXml($array, true, true, 'Request');
// print_r($xml1);
// <Request>
//  <Operation>
//   <WatermarkTemplateId>
//    <item>t146d70eb241c44c63b6efc1cc93ccfc5d</item>
//    <item>t12a74d11687d444deba8a6cc52051ac27</item>
//   </WatermarkTemplateId>
//  </Operation>
// </Request>
$array['Operation']['WatermarkTemplateId'] = Transformer::wrap($array['Operation']['WatermarkTemplateId'], true, 'WatermarkTemplateId');
$xml2 = Transformer::toXml($array, true, true, 'Request');
// print_r($xml2);
// <Request>
// <Operation>
//  <WatermarkTemplateId>t146d70eb241c44c63b6efc1cc93ccfc5d</WatermarkTemplateId>
//  <WatermarkTemplateId>t12a74d11687d444deba8a6cc52051ac27</WatermarkTemplateId>
// </Operation>
// </Request>

API

Transformer::toArray(string $xml = '<xml/>')

Parse the XML string to array.

Transformer::sanitize(string $xml = '<xml/>')

Sanitize the XML string in the XML1.0 20081126 Character Range.

Transformer::toXml(array $data, bool $headless = true, bool $indent = false, string $root = 'xml', string $item = 'item'): string

Build the data array to XML string.

Transformer::wrap(array $data, bool $wrapped = false, string $label = 'item'): LabeledArrayIterator

Wrap the array data with a label and wrapped flag.

License

Apache-2.0 License

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support
se7enxweb/flysystem
lampwick/laravel
jeslage/statamic-emojis
micahdshackelford/rmap-laravel
draftsmaninc/draftsman
square-bit/laravel-volition
himito/laravel-mailman
htinlynn/genius-helper
maco-studios/hephaestus