繁体   English   中英

在php中将数组转换为xml

[英]Convert an array to xml in php

我需要一些帮助。 我想将数组 php 转换为用于 Web 服务的 xml tu,但我不能,我将举个例子:

$parameters = [   'outputFormat' => 
    [ 
      'x' => 0,
      'y' => 0
    ]   'letter' => 
    [
      'service' => 
        [ 
          'productCode' => 'code',
          'depositDate' => '2019-11-26',
        ]
    ]
    'customsDeclarations' => 
      [
        'includeCustomsDeclarations' => 1,
        'contents' => 
          [
            'article' => 
              [ 
                  0 => 
                    [ 
                      'description' => 'desc 1',
                      'quantity' =>  1,
                      'weight' =>  1,
                      'value' => '4',
                    ],
                  1 => 
                    [ 
                      'description' => 'desc 2',
                      'quantity' => 1,
                      'weight' => 1,
                      'value' => '10',
                     ]
              ],
              'category' => 
                [ 
                  'value' => int 6,
                  ],
          ]
      ] ];  

我需要像这样将此数组转换为 xml 我的问题出在“文章”节点中:

<includeCustomsDeclarations>1</includeCustomsDeclarations>
       <contents>
        <article>
          <description>desc1</description>
          <quantity>1</quantity>
          <weight>1</weight>
          <value>1999</value>
        </article>
        <article>
          <description>desc2</description>
          <quantity>1</quantity>
          <weight>1</weight>
          <value>1200</value>
        </article> 

我用这个函数来转换

public function convertArrayToXml(array $soapRequest, $soapRequestXml)
{
    foreach ($soapRequest as $key => $value) {
        if (is_array($value)) {
            if (!is_numeric($key)) {
                $subNode = $soapRequestXml->addChild("$key");
                $this->convertArrayToXml($value, $subNode);
            } else {
                $subNode = $soapRequestXml->addChild("item$key");
                $this->convertArrayToXml($value, $subNode);
            }
        } else {
            $soapRequestXml->addChild("$key", htmlspecialchars("$value"));
        }
    }
}

尝试在键控元素下的数组中添加多个元素是一个常见问题。 而不是使用简化的 - 让我们添加另一个级别,这段代码直接将它添加到父元素中。 在第二次迭代中,它查找父节点以获取元素的名称并使用该名称创建一个新的子节点并将新的数组元素添加到新节点。

public function convertArrayToXml(array $soapRequest, $soapRequestXml)
{
    foreach ($soapRequest as $key => $value) {
        if (is_array($value)) {
            if (!is_numeric($key)) {
                $subNode = $soapRequestXml->addChild("$key");
                $this->convertArrayToXml($value, $subNode);
            } else {
                // After first element
                if ( $key > 0 ) {
                    // Fetch parent node
                    $parentNode = $soapRequestXml->xpath("..")[0];
                    // Add in a new node with same name and set as node to add data to
                    $subNode = $parentNode->addChild($soapRequestXml->getName());
                }
                else    {
                    // First time, add it directly to existing node
                    $subNode = $soapRequestXml;
                }
                $this->convertArrayToXml($value, $subNode);
            }
        } else {
            $soapRequestXml->addChild("$key", htmlspecialchars("$value"));
        }
    }
}

对于该测试数据,输出是...

<outputFormat>
    <x>0</x>
    <y>0</y>
</outputFormat>
<letter>
    <service>
        <productCode>code</productCode>
        <depositDate>2019-11-26</depositDate>
    </service>
</letter>
<customsDeclarations>
    <includeCustomsDeclarations>1</includeCustomsDeclarations>
    <contents>
        <article>
            <description>desc 1</description>
            <quantity>1</quantity>
            <weight>1</weight>
            <value>4</value>
        </article>
        <article>
            <description>desc 2</description>
            <quantity>1</quantity>
            <weight>1</weight>
            <value>10</value>
        </article>
        <category>
            <value>6</value>
        </category>
    </contents>
</customsDeclarations>

https://github.com/SrDebiasi/php-xml-array-parser

我做了这个。 该函数位于 github 上的 function.php 您可以使用多个键和属性,数组需要遵循以下结构:

$array = [
    [
        'key' => 'car',
        'fields' =>
            [
                ['key' => 'wheel', 'value' => 1],
                ['key' => 'wheel', 'value' => 2],
                ['key' => 'wheel', 'value' => 3],
                ['key' => 'tire', 'value' => 3],
                ['key' => 'wire', 'fields' => [
                    ['key' => 'central', 'value' => 1],
                    ['key' => 'pack', 'attributes' =>
                        ['key' => 'HX', 'value' => 'Right'],
                        ['key' => 'HX', 'value' => 'Right'],
                        ['key' => 'HZ', 'value' => 'Left'],
                    ],
                ]],
            ]
    ]
];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM