承接 aaronddm/xml-builder 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

aaronddm/xml-builder

最新稳定版本:v2.0.3

Composer 安装命令:

composer require aaronddm/xml-builder

包简介

A simple PHP based XML builder

README 文档

README

This is a simple PHP 7.2+ based XML Builder library. Use it to easily generate XML output with just PHP.

Table of Contents

Installation

composer require aaronddm/xml-builder

Prerequisites

  • PHP >=7.2.0
  • php-xml if using XMLWriter

Basic Usage

The following is an example of the most basic usage of this library.

Example: Using XMLWriter

<?php

require_once 'vendor/autoload.php';

use AaronDDM\XMLBuilder\XMLBuilder;
use AaronDDM\XMLBuilder\Writer\XMLWriterService;
use AaronDDM\XMLBuilder\Exception\XMLArrayException;

$xmlWriterService = new XMLWriterService();
$xmlBuilder = new XMLBuilder($xmlWriterService);

try {
    $xmlBuilder
        ->createXMLArray()
            ->start('Root')
                ->addCData('1 First Child First Element', 'This is a test')
                ->add('First Child Second Element', false)
                ->start('Second Parent')
                    ->add('Second child 1', null, ['myAttr' => 'Attr Value'])
                    ->add('Second child 2', false)
                    ->start('Third Parent')
                        ->add('Child')
                    ->end()
                ->end()
                ->add('First Child Third Element')
            ->end();

    var_dump($xmlBuilder->getXML());
} catch (XMLArrayException $e) {
    var_dump('An exception occurred: ' . $e->getMessage());
}

Output

string(414) "<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <FirstChildFirstElement><![CDATA[This is a test]]></FirstChildFirstElement>
    <FirstChildSecondElement>False</FirstChildSecondElement>
    <SecondParent>
        <Secondchild myAttr="Attr Value"/>
        <Secondchild>False</Secondchild>
        <ThirdParent>
            <Child/>
        </ThirdParent>
    </SecondParent>
    <FirstChildThirdElement/>
</Root>
"

Example: Custom XMLWriter instance

<?php

require_once 'vendor/autoload.php';

use AaronDDM\XMLBuilder\XMLBuilder;
use AaronDDM\XMLBuilder\Writer\XMLWriterService;

$xmlWriter = new \XMLWriter();
$xmlWriter->openMemory();
$xmlWriter->setIndent(true);
$xmlWriter->setIndentString('    ');
$xmlWriter->startDocument('1.0', 'UTF-8');
$xmlWriter->writeDtd('html', '-//W3C//DTD XHTML 1.0 Transitional//EN', 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd');

$xmlWriterService = new XMLWriterService($xmlWriter);
$xmlBuilder = new XMLBuilder($xmlWriterService);

try {
    $xmlBuilder
        ->createXMLArray()
            ->start('Root')
                ->addCData('1 First Child First Element', 'This is a test')
                ->add('First Child Second Element', false)
                ->start('Second Parent')
                    ->add('Second child 1', null, ['myAttr' => 'Attr Value'])
                    ->add('Second child 2', false)
                    ->start('Third Parent')
                        ->add('Child')
                    ->end()
                ->end()
                ->add('First Child Third Element')
            ->end();

    var_dump($xmlBuilder->getXML());
} catch (XMLArrayException $e) {
    var_dump('An exception occurred: ' . $e->getMessage());
}

Output

string(414) "<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Root>
    <FirstChildFirstElement><![CDATA[This is a test]]></FirstChildFirstElement>
    <FirstChildSecondElement>False</FirstChildSecondElement>
    <SecondParent>
        <Secondchild myAttr="Attr Value"/>
        <Secondchild>False</Secondchild>
        <ThirdParent>
            <Child/>
        </ThirdParent>
    </SecondParent>
    <FirstChildThirdElement/>
</Root>
"

Looping through data

You easily added sets of data using the startLoop method provided.

Example: XML output of a list of users

<?php
require_once 'vendor/autoload.php';

use AaronDDM\XMLBuilder\XMLArray;
use AaronDDM\XMLBuilder\XMLBuilder;
use AaronDDM\XMLBuilder\Writer\XMLWriterService;

$users = [
    [
        'name' => 'John Doe',
        'age' => 32
    ],
    [
        'name' => 'Jane Doe',
        'age' => 98
    ]
];


$xmlWriterService = new XMLWriterService();
$xmlBuilder = new XMLBuilder($xmlWriterService);

$xmlBuilder
    ->createXMLArray()
        ->start('Root')
            ->startLoop('Users', [], function (XMLArray $XMLArray) use ($users) {
                foreach ($users as $user) {
                    $XMLArray->start('User')
                        ->add('name', $user['name'])
                        ->add('age', $user['age']);
                }
            })
            ->end()
        ->end();

var_dump($xmlBuilder->getXML());

Output

string(261) "<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <Users>
        <User>
            <name>John Doe</name>
            <age>32</age>
        </User>
        <User>
            <name>Jane Doe</name>
            <age>98</age>
        </User>
    </Users>
</Root>
"

Looping without a parent

You easily loop through a list of of data using the loop method provided, without having a parent element for the looped data.

Example: XML output of a list of users

<?php
require_once 'vendor/autoload.php';

use AaronDDM\XMLBuilder\XMLArray;
use AaronDDM\XMLBuilder\XMLBuilder;
use AaronDDM\XMLBuilder\Writer\XMLWriterService;

$users = [
    [
        'name' => 'John Doe',
        'age' => 32
    ],
    [
        'name' => 'Jane Doe',
        'age' => 98
    ]
];


$xmlWriterService = new XMLWriterService();
$xmlBuilder = new XMLBuilder($xmlWriterService);

$xmlBuilder
    ->createXMLArray()
    ->start('Root')
        ->loop(function (XMLArray $XMLArray) use ($users) {
            foreach ($users as $user) {
                $XMLArray->start('User')
                    ->add('name', $user['name'])
                    ->add('age', $user['age']);
            }
        })
    ->end();
    
var_dump($xmlBuilder->getXML());
?>

Output

<Root>
    <User>
        <name>John Doe</name>
        <age>32</age>
    </User>
    <User>
        <name>Jane Doe</name>
        <age>98</age>
    </User>
</Root>

Using a custom "XMLElementData" class

You can override the XMLElementData element class to implement transformations to the value of your data based on the type passed. To do this, you simply extend the XMLElementData class and override any of the methods to your liking.

Example: Customized MyXMLElementData class

<?php

require_once 'vendor/autoload.php';

use AaronDDM\XMLBuilder\XMLElementData;
use AaronDDM\XMLBuilder\XMLBuilder;
use AaronDDM\XMLBuilder\Writer\XMLWriterService;
use AaronDDM\XMLBuilder\Exception\XMLArrayException;

/**
 * Class MyXMLElementData
 */
class MyXMLElementData extends XMLElementData
{
    /**
     * @return mixed
     */
    public function getValue()
    {
        $type = $this->getType();
        $value = $this->value;

        if(is_bool($value)) {
            $type = 'boolean';
        }

        switch($type) {
            case 'specialType':
                $value = 'Special Type Value';
                break;
            case 'boolean':
                $value = ($value) ? 'True' : 'False';
                break;
        }

        return $value;
    }
}

$xmlWriterService = new XMLWriterService();
$xmlBuilder = new XMLBuilder($xmlWriterService);
$xmlBuilder->setElementDataClass(MyXMLElementData::class);

try {
    $xmlBuilder
        ->createXMLArray()
            ->start('Root')
                ->addCData('1 First Child First Element', 'This is a test')
                ->add('First Child Second Element', false)
                ->start('Second Parent')
                    ->add('Second child 1', null, ['myAttr' => 'Attr Value'])
                    ->add('Second child 2', false)
                    ->start('Third Parent')
                        ->add('Child')
                        ->add('Special Type Child', "1", [], 'specialType')
                    ->end()
                ->end()
                ->add('First Child Third Element')
            ->end();

    var_dump($xmlBuilder->getXML());
} catch (XMLArrayException $e) {
    var_dump('An exception occurred: ' . $e->getMessage());
}

Output

string(482) "<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <FirstChildFirstElement><![CDATA[This is a test]]></FirstChildFirstElement>
    <FirstChildSecondElement>False</FirstChildSecondElement>
    <SecondParent>
        <Secondchild myAttr="Attr Value"/>
        <Secondchild>False</Secondchild>
        <ThirdParent>
            <Child/>
            <SpecialTypeChild>Special Type Value</SpecialTypeChild>
        </ThirdParent>
    </SecondParent>
    <FirstChildThirdElement/>
</Root>
"

Running tests

cd /root/of/project/
vendor/bin/phpunit

OR

docker build -t xmlbuilder .
docker run -u appuser -it --rm xmlbuilder vendor/bin/phpunit

License

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

统计信息

  • 总下载量: 121.85k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 29
  • 点击次数: 2
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 28
  • Watchers: 2
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 未知

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固