,php,xml"/>

首页 > 解决方案 > PHP如何创建特殊元素

问题描述

我正在尝试在 php 中创建以下元素,以创建我们的一位客户所需的 xml。

一切正常,但我无法找出我需要如何创建以下元素

在此处输入图像描述

我尝试了几种方法,但仍然无法正确生成上述示例

有人可以让我上路吗?

这就是我的编码

$eanucc_countryISOCode = $dom->createElement('eanucc','BE');
$eanucc_countryISOCode->setAttribute('xmlns', 'urn:ean.ucc:2');

标签: phpxml

解决方案


xmlns:eanucc是命名空间的命名空间定义urn:ean.ucc:2。使用命名空间感知 DOM 方法(带有后缀NS)。

$document = new DOMDocument();
$document
  ->appendChild(
    $document->createElementNS('urn:ean.ucc:2', 'eanucc:countryISOCode')    
  )
  ->appendChild(
    $document->createTextnode('BE')    
  );

echo $document->saveXML();

输出:

<?xml version="1.0"?>
<eanucc:countryISOCode xmlns:eanucc="urn:ean.ucc:2">BE</eanucc:countryISOCode>

推荐阅读