0

我将我的请求数据构建为数组结构,并希望使用 symfony XmlEncoder 将我的数组编码为 XML。

所以我想我的基本部分是正确的,例如:

    $request_object = [
      "acc-id" => $all_credentials,
      "req-id" => $request_guid,
      "tran-type" => "spec-url"
    ];

我正在寻找以下格式编码的语法,带有属性和值:

 <amount currency="EUR">1.99</amount>

我有可能在数组键​​上使用 @ 符号,但如何也适合该值?

    $request_object = [
      "acc-id" => $all_credentials,
      "req-id" => $request_guid,
      "tran-type" => "spec-url"
      "am" => ["@attr"=>"attrval"] 
    ];

这应该是

<am attr="attrval"/>

但是如何写它我也可以设置值?喜欢:

<am attr="attrval">VALUE</am>

非常感谢帮助

4

1 回答 1

2

'#'用作标量值的索引。
我通过查看编码器的测试找到了它。

#src:https://github.com/symfony/serializer/blob/master/Tests/Encoder/XmlEncoderTest.php  

#line: 196
public function testEncodeScalarRootAttributes()
{
    $array = [
        '#' => 'Paul',
        '@eye-color' => 'brown',
    ];
    $expected = '<?xml version="1.0"?>'."\n".
        '<response eye-color="brown">Paul</response>'."\n";
    $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
}
...
#line: 234
public function testEncodeScalarWithAttribute()
{
    $array = [
        'person' => ['@eye-color' => 'brown', '#' => 'Peter'],
    ];
    $expected = '<?xml version="1.0"?>'."\n".
        '<response><person eye-color="brown">Peter</person></response>'."\n";
    $this->assertEquals($expected, $this->encoder->encode($array, 'xml'));
}
于 2019-09-26T04:03:03.807 回答