drmad/semeele

极其简单且极简的PHP XML生成器。真的,非常简单。

1.0.5 2023-03-26 15:03 UTC

This package is auto-updated.

Last update: 2024-09-27 17:19:26 UTC


README

极其简单且极简的PHP XML生成器。真的,非常简单。

我之前使用的是 本地XML库 来创建... XML文档。但它们非常冗长。所以我最初用了大约四小时编写了这个。

它与PHP 5.6及更高版本兼容,并且启用了mbstring扩展。

示例

$xml = new Drmad\Semeele\Document('html');
$xml->child('head')
    ->add('title', 'An XHTML')
    ->add('meta', ['charset' => 'utf-8'])
    ->parent()
->child('body')
    ->add('h1', 'An XHTML')
    ->add('p', 'This is a XML-valid HTML. Yay!')
;

echo $xml->getXML();

就这些。这会打印出

<?xml version="1.0" encoding="utf-8"?><html><head><title>An XHTML</title><meta charset="utf-8"/></head><body><h1>An XHTML</h1><p>This is a XML-valid HTML. Yay!</p></body></html>

一个更复杂、现实生活中的示例,用于签名的UBL文档中

$xml = new Drmad\Semeele\Node('ext:UBLExtension');
$xml->child('ext:ExtensionContent')
    ->child('ds:Signature', ['Id' => 'Signature'])->save($s) // Save this node for later
        ->child('ds:SignedInfo')
            ->add('ds:CanonicalizationMethod',
                ['Algorithm'=>'http://www.w3.org/TR/2001/REC-xml-c14n-20010315'])
            ->add('ds:SignatureMethod',
                ['Algorithm'=>'http://www.w3.org/2000/09/xmldsig#rsa-sha1'])
            ->child('ds:Reference', ['URI'=>''])
                ->child('ds:Transforms')
                    ->add('ds:Transform', ['Algorithm'=>'http://www.w3.org/2000/09/xmldsig#enveloped-signature'])
                    ->parent()
                ->add('ds:DigestMethod', ['Algorithm'=>'http://www.w3.org/2000/09/xmldsig#sha1'])
                ->add('ds:DigestValue')
;
        $s->add('ds:SignatureValue')    // Using the saved node
        ->child('ds:KeyInfo')
            ->child('ds:X509Data')
                ->add('ds:X509SubjectName')
                ->add('ds:X509Certificate')
;
echo $xml;

输出

<ext:UBLExtension><ext:ExtensionContent><ds:Signature Id="Signature"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315"/><ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/><ds:Reference URI=""><ds:Transforms><ds:Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature"/></ds:Transforms><ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/><ds:DigestValue/></ds:Reference></ds:SignedInfo><ds:SignatureValue/><ds:KeyInfo><ds:X509Data><ds:X509SubjectName/><ds:X509Certificate/></ds:X509Data></ds:KeyInfo></ds:Signature></ext:ExtensionContent></ext:UBLExtension>

安装和基本用法

Semeele 通过 Composer 提供

composer require drmad/semeele

或者,您可以 git clone 或从 GitHub 下载ZIP文件,并使用这个简单的自动加载函数(根据需要更新常量 SEMEELE_PATH 为正确路径)

const SEMEELE_PATH = './semeele-master';

spl_autoload_register(function($class) {
    if (substr($class, 0, 13) == 'Drmad\\Semeele') {
        $base_name = substr($class, strrpos($class, '\\') + 1);
        $file_name = SEMEELE_PATH . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR . $base_name . '.php';
        if (file_exists($file_name)) {
            require $file_name;
        }
    }
});

Drmad\Semeele\Node

所有节点的基类。其构造函数有以下参数

  • $nodeName: 必需。
  • $content: 可选。这是节点内容。如果传递数组,则使用 $attribute 参数。
  • $attributes: 可选。具有 ['attribute name' => 'attribute value'] 结构的数组。
  • $encoding: 可选。默认为 'UTF-8'。用于重新编码 $content,并传递给使用 child()add() 方法创建的子节点。

该类有两个主要方法用于添加新的子节点:child()add()。两者都返回一个 Node 对象,但 child() 返回新创建的节点,而 add() 返回当前节点,因此您可以继续向同一节点添加新的子节点。

两种方法的所有参数都传递给 Node 构造函数。

其他方法如下(除非另有说明,所有方法都返回受影响的 Node 对象)

  • parent(): 返回父节点。用于'向上遍历',可能在执行完一系列 add() 方法之后(查看上面的示例)。
  • append(Node $node): 向此节点的子节点树添加已创建的节点及其子节点。
  • save(&$node): 将节点保存到 $node。对于从深层嵌套节点'返回'很有用。
  • attr($name, $value): 向此节点添加新属性。您可以指定两个参数,或者将具有多个属性和值的关联数组作为第一个参数传递。
  • comment($text): 添加一个新的 Drmad\Semeele\Comment 节点。

最终的XML使用 getXML() 方法生成,或者只需在 string 上下文中使用对象。

Drmad\Semeele\Document

该类扩展了 Node,并在节点内容之前添加了 XML声明(使用 Drmad\Semeele\ProcessingInstruction 类)。其构造函数有以下参数

  • $rootNodeName: 必需。它将是XML文档中主要 Node 的名称。
  • $version: 可选。XML版本,默认为 1.0
  • $encoding: 可选。XML编码,默认为 UTF-8

您可以使用 getDeclaration() 获取XML声明节点,可能用于添加新属性等。

Drmad\Semeele\Cdata

创建用于转义的CDATA值。仅当人类需要读取XML时才有用,因为所有字符串都始终在需要时转换为XML实体。例如。

$xml = new Drmad\Semeele\Node('test');
$xml->add('without_cdata', 'Love this song from <strong>KC & The Sunshine</strong>')
    ->add('with_cdata', new Drmad\Semeele\Cdata('Love that song from <strong>KC & The Sunshine</strong>'))
;
echo $xml;

输出

<test><without_cdata>Love this song from &lt;strong&gt;KC &amp; The Sunshine&lt;/strong&gt;</without_cdata><with_cdata><![CDATA[Love that song from <strong>KC & The Sunshine</strong>]]></with_cdata></test>