JSON to XML - SimpleXMLElement Object



我正在尝试将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这是一个测试不要FAKTURERcreate_order_direct*********REDP

你不添加它们。您只添加嵌套数组的第一级。若要获得更深层次,需要将逻辑重构为函数。这允许递归调用。使用 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>

相关内容

  • 没有找到相关文章

最新更新