用 Symfony/XmlEncoder 转换此数组的正确 key/value 语法是什么?
Whats the correct key/value Syntax to convert this Array with Symfony/XmlEncoder?
我正在将我的请求数据构建为数组结构,并希望使用 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>
非常感谢帮助
使用 '#'
作为标量值的索引。
我通过查看编码器的测试找到了它。
#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'));
}
我正在将我的请求数据构建为数组结构,并希望使用 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>
非常感谢帮助
使用 '#'
作为标量值的索引。
我通过查看编码器的测试找到了它。
#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'));
}