JSON转换为XML-SimpleXMLElement对象

时间:2018-09-20 11:53:20

标签: php arrays xml

我正在尝试将JSON转换为XML格式以通过API发送。我有多个有效的对象,但那些JSON数组不是多维数组,因此可以工作。但是,当我在更深的阵列上尝试此功能时,它将无法正常工作。

数组:

public function create_order_direct($plan_id) {
    return $this->__request(__FUNCTION__, [
        "media_id" => 'FACE',
        "client_id" => 'HELL',
        "agreement_id" => '*****',
        "client_reference" => ****,
        "client_contact" => "******",
        "plan_number" => ******,
        "plan_name" => "TEST",
        "cuid" => ******,
        "status" => 'P',
        "colour" => 0,
        "insertion" => [
            "insertion_date" => '2018-09-19',
            "end_date" => '2018-09-20',
            "client_reference" => 1234,
            "price_row" => [
                "price_code" => 000,
                "number_of_units" => 250000,
                "gross" => 1000,
                "discount" => [

                ]
            ],
        ],
        "comment" => "THIS IS A TEST DO NOT FAKTURER",

    ]);
}

代码:

$xml = new \SimpleXMLElement('<marathon/>');
//For each element in the array add it as a child node to the xml object.
foreach ($request as $k => $v) {
    if (is_array($v)) { //nested array
        $xml->addChild($k);
    } else {
        $xml->addChild($k, $v);
    }
}
echo"<pre>";
print_r($xml);
die;

$dom = dom_import_simplexml($xml)->ownerDocument;
$dom->encoding = "UTF-8";
$dom->formatOutput = true;
return $dom->saveXML();

输出:

SimpleXMLElement Object
(
    [media_id] => FACE
    [client_id] => HELL
    [agreement_id] => REDP
    [client_reference] => 123456
    [client_contact] => Asim Tariq
    [plan_number] => 408
    [plan_name] => TEST
    [cuid] => 123456
    [status] => P
    [colour] => 0
    [insertion] => SimpleXMLElement Object
        (
        )

    [comment] => THIS IS A TEST DO NOT FAKTURER
    [type] => create_order_direct
    [password] => *********
    [company_id] => REDP
)

我需要什么(预期输出):

<marathon>
   <media_id>***</media_id>
   <agreement_id>***</agreement_id>
   <client_reference>***</client_reference>
   <client_contact>***</client_contact>
   <plan_number>***</plan_number>
   <plan_name>***</plan_name>
   <cuid>***</cuid>
   <status>***</status>
   <colour>***</colour>
   <insertion>
      <insertion_date>2016-11-20</insertion_date>
      <end_date>201-11-21</end_date>
      <client_reference>123</client_reference>
      <price_row>
         <price_code>000</price_code>
         <number_of_units>2500000</number_of_units>
         <gross>1000</gross>
         <discount>
            <discount_1>100</discount_1>
         </discount>
         <comment>This is a comment!</comment>
      </price_row>
   </insertion>
</marathon>

insertion部分存在问题,看起来好像没有被迭代。

更新:(using: echo $xml->asXML();) (我发现“插入”->“ price_row”->“折扣”中缺少):
FACEREDP123456Asim Tariq408TEST123456P0这是一项测试,请不要做Rcreate_order_direct ********* REDP

1 个答案:

答案 0 :(得分:1)

您不添加它们。您仅添加嵌套数组的第一级。为了获得更深层次的知识,您需要将逻辑重构为一个函数。这允许递归调用。使用DOM稍微容易一些:

$data = [
    "media_id" => 'FACE',
    "colour" => 0,
    "insertion" => [
        "client_reference" => 1234,
        "price_row" => [
            "price_code" => '000',
            "discount" => [

            ]
        ],
    ],
    "comment" => "THIS IS A TEST DO NOT FAKTURER"
];

function appendDataToNode(\DOMElement $parent, $data) {
    $document = $parent->ownerDocument;
    if (\is_array($data)) {
        foreach ($data as $name => $value) {
            // append an element node for the array element
            $node = $parent->appendChild($document->createElement($name));
            // call itself to append data to the new element node
            appendDataToNode($node, $value);
        }
    } else {
        // append value as a text node
        $node = $parent->appendChild($document->createTextNode($data));
    }
}

$document = new \DOMDocument('1.0', 'UTF-8');
// create + append a document element
$document->appendChild($document->createElement('marathon'));
// append data to document element
appendDataToNode($documnet->documentElement, $data);

$document->formatOutput = TRUE;
echo $document->saveXML();

输出:

<?xml version="1.0" encoding="UTF-8"?> 
<marathon> 
  <media_id>FACE</media_id> 
  <colour>0</colour> 
  <insertion> 
    <client_reference>1234</client_reference> 
    <price_row> 
      <price_code>000</price_code> 
      <discount/> 
    </price_row> 
  </insertion> 
  <comment>THIS IS A TEST DO NOT FAKTURER</comment>
</marathon>