我尝试将以下内容显示为xml文件:
<NetAmount currency="GBP">200</NetAmount>
这是我的PHP代码:
<?php
$str = '<?xml version="1.0" encoding="UTF-8" standalone="no"?><CarrierFreightInvoice></CarrierFreightInvoice>';
$xml = simplexml_load_string($str);
$RID = $_POST['rid'];
$SNDID = $_POST['sndid'];
$RID = htmlentities($RID, ENT_COMPAT, 'UTF-8', false);
$SNDID = htmlentities($SNDID, ENT_COMPAT, 'UTF-8', false);
$xml->Header = "";
$xml->Header->addChild('RID', $RID);
$xml->Header->addChild('SNDID', $SNDID);
$FreightInvoiceNumber = $_POST['fin'];
$FreightInvoiceDate = $_POST['fid'];
$CarrierReferenceNumber = $_POST['crn'];
$ShipperReferenceNumber = $_POST['srn'];
$NetAmount = $_POST['net'];
$Currency = $_POST['gbp'];
$FreightInvoiceNumber = htmlentities($FreightInvoiceNumber, ENT_COMPAT, 'UTF-8', false);
$FreightInvoiceDate = htmlentities($FreightInvoiceDate, ENT_COMPAT, 'UTF-8', false);
$CarrierReferenceNumber = htmlentities($CarrierReferenceNumber, ENT_COMPAT, 'UTF-8', false);
$ShipperReferenceNumber = htmlentities($ShipperReferenceNumber, ENT_COMPAT, 'UTF-8', false);
$NetAmount = htmlentities($NetAmount, ENT_COMPAT, 'UTF-8', false);
$Currency = htmlentities($Currency, ENT_COMPAT, 'UTF-8', false);
$xml->addChild('FreightInvoiceNumber', $FreightInvoiceNumber);
$xml->addChild('FreightInvoiceDate', $FreightInvoiceDate);
$xml->addChild('CarrierReferenceNumber', $CarrierReferenceNumber);
$xml->addChild('ShipperReferenceNumber', $ShipperReferenceNumber);
$xml->addChild('NetAmount', $NetAmount, $Currency);
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;
$doc->preserveWhiteSpace = true;
$doc->loadXML($xml->asXML(), LIBXML_NOBLANKS);
$doc->save('test.xml');
?>
结果如下:
<NetAmount xmlns="GBP">200</NetAmount>
我尝试过各种各样的addchild组合,但我无法使用它,而且我不熟悉这个php! 它看起来像html中的php包括将它排序。我会很感激一些指示......
答案 0 :(得分:0)
使用addChild()
时,第三个参数是您要将项目添加到的命名空间(请参阅https://timbrado1.dcigroupmexico.com.mx/ServicioSeguridad.php?wsdl)。这就是为什么
$xml->addChild('NetAmount', $NetAmount, $Currency);
将货币添加为命名空间。
如果要添加属性,则需要执行以下操作。创建元素,然后将属性添加到新元素。
$amount = $xml->addChild('NetAmount', $NetAmount);
$amount->addAttribute('currency', $Currency);