maxnz/

xml-builder

轻松简洁地构建XML

v1.0.1 2022-05-05 18:48 UTC

This package is auto-updated.

Last update: 2024-09-05 23:53:07 UTC


README

在PHP中轻松简洁地构建XML。

用法

单个XML元素

$xml = new XMLElement(
    qualifiedName: "elementName",
    value: "element value",
);

print($xml->buildXML());
<elementName>element value</elementName>

添加属性

$xml = new XMLElement(
    qualifiedName: "elementName",
    value: "element value",
    attributes: [
        "attribute1" => "attribute 1 value",
        "attribute2" => "attribute 2 value",
    ],
);

print($xml->buildXML());
<elementName attribute1="attribute 1 value" attribute2="attribute 2 value">element value</elementName>

子元素

$xml = new XMLElement(
    "elementName",
    "element value",
    [
        "attribute1" => "attribute 1 value",
        "attribute2" => "attribute 2 value",
    ],
    [
        new XMLElement(
            "child1",
            children: [
                new XMLElement(
                    "childOfChild",
                    "valueOfChild",
                )
            ]
        ),
        new XMLElement(
            "AnotherChild",
            "child2 Value",
        ),
    ],
);

print($xml->buildXML());
<elementName attribute1="attribute 1 value" attribute2="attribute 2 value">element value<child1><childOfChild>valueOfChild</childOfChild></child1><AnotherChild>child2 Value</AnotherChild></elementName>