php - PHP如何创建特殊元素
问题描述
我正在尝试在 php 中创建以下元素,以创建我们的一位客户所需的 xml。
一切正常,但我无法找出我需要如何创建以下元素
我尝试了几种方法,但仍然无法正确生成上述示例
有人可以让我上路吗?
这就是我的编码
$eanucc_countryISOCode = $dom->createElement('eanucc','BE');
$eanucc_countryISOCode->setAttribute('xmlns', 'urn:ean.ucc:2');
解决方案
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>
推荐阅读
- excel - 如何将工作表从一本书复制到另一本书
- php - 无法从局域网上的 apache 访问 wp-admin - nginx rev 代理
- r - R:列表的元素到相应的数据框行
- arduino - FastLED 闪烁 - 使用 millis() 无延迟地改变颜色
- multithreading - 线程什么时候死?
- b-tree - B-tree minimum internal children count 解释
- mysql - MYSQL LOAD DATA IN FILE 并将 LAST_INSERT_ID 和 INSERT DATA 放入另一个表
- django - 无法从与一对多关系关联的模型中获取模板中的数据
- git - 远程分支在 sourcetree 中可以为空 ( '' ) 还是错误?
- python - 从工作目录中的文件夹导入另一个 py 文件