在php中使用命名空间生成XML文档

时间:2015-09-18 06:52:38

标签: php xml web-services soap

我想使用PHP按照下面的结构创建一个XML文档(使用namesapace)。我的目标是创建将被推送到服务器的xml(使用curl)。

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<InsertData xmlns="http://tempuri.org/">
  <userName>string</userName>
  <password>string</password>
  <costCenterCode>string</costCenterCode>
  <consigneeName>string</consigneeName>
  <consigneeAddress>string</consigneeAddress>
  <consigneeMobNo>string</consigneeMobNo>
  <consigneeEmail>string</consigneeEmail>
  <originCityName>string</originCityName>
  <destinationCityName>string</destinationCityName>
  <pieces>string</pieces>
  <weight>string</weight>
  <codAmount>decimal</codAmount>
  <custRefNo>string</custRefNo>
  <productDetails>string</productDetails>
  <fragile>string</fragile>
  <services>string</services>
  <remarks>string</remarks>
  <insuranceValue>string</insuranceValue>
</InsertData>
</soap:Body>
</soap:Envelope>

这是我创建xml的代码,但问题是它没有添加带子级的命名空间,而Web服务服务器给出了错误,因为它没有按照xml引用进行结构化。

$xml = new SimpleXMLElement('<?xml version = "1.0" ?><Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope" />');

$body = $xml->addChild("Body");
$tcsAPI = $body->addChild('InsertData');

$tcsAPI->addChild('userName', "xxxxxx");

$tcsAPI->addChild('password', "xxxxxx");

$tcsAPI->addChild('costCenterCode', "CC001");
$tcsAPI->addChild('consigneeName', "MagDev");
$tcsAPI->addChild('consigneeAddress', "Address");
$tcsAPI->addChild('consigneeMobNo', "00000000");
$tcsAPI->addChild('consigneeEmail', "email@some.com");
$tcsAPI->addChild('originCityName', "ORG");
$tcsAPI->addChild('destinationCityName', "DEST");
$tcsAPI->addChild('pieces', "1");
$tcsAPI->addChild('weight', "1");
$tcsAPI->addChild('codAmount', "0");
$tcsAPI->addChild('custRefNo', "WEBT");
$tcsAPI->addChild('productDetails', "Test Product");
$tcsAPI->addChild('fragile', "0");
$tcsAPI->addChild('services', "0");
$tcsAPI->addChild('remarks', "Testing API services");
$tcsAPI->addChild('insuranceValue', "0");

由于

2 个答案:

答案 0 :(得分:0)

$tcsAPI->addAttribute('xmlns', 'http://tempuri.org/');

答案 1 :(得分:0)

这些方法有一个额外的参数,允许您指定命名空间。这将根据需要添加命名空间定义(xmlns属性)。

$xmlns = [
  'soap' => 'http://www.w3.org/2003/05/soap-envelope',     
  't' => 'http://tempuri.org/'
];

$xml = new SimpleXMLElement(
  '<?xml version = "1.0" ?><soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://www.w3.org/2003/05/soap-envelope" />'
);

$body = $xml->addChild("soap:Body", null, $xmlns['soap']);
$tcsAPI = $body->addChild('InsertData', null, $xmlns['t']);

//...

echo $xml->asXml();

您可以像属性一样手动添加命名空间定义。但这只需要避免后代的多个定义。如果祖先已经定义了命名空间,则后代不再需要这样做。

请注意,如果属性具有前缀,则属性仅在命名空间中,默认命名空间仅对元素有效。没有前缀的属性总是在“空”命名空间中。