-和/>;在xml文档中(php试图创建与示例中相同的文档)


- and /> in xml document (with php trying to create the same document as in example)

我有一个xml文档的例子,php试图创建相同的xml。

这是xml文档示例的一部分(当使用web浏览器打开时显示)

-<DeclarationFile>
  -<Declaration Id="DEC">
    -<DokPVNv4>

需要创建相同的。我的php代码

$DOM = new DOMDocument('1.0','UTF-8');
$root = $DOM->createElement('DeclarationFile');
$DOM->appendChild($root);
$child_declaration_id = $DOM->createElement('Declaration');
$root->appendChild($child_declaration_id);
$child_declaration_id_att = $DOM->createAttribute('Id');
$child_declaration_id->appendChild($child_declaration_id_att);
$att_child_declaration_id_text = $DOM->createTextNode('DEC');
$child_declaration_id_att->appendChild($att_child_declaration_id_text);
$DokPVNv4 = $DOM->createElement('DokPVNv4');
$root->appendChild($DokPVNv4);

在输出(web浏览器)中获取此

-<DeclarationFile>
  <Declaration Id="DEC"/>
  <DokPVNv4/>
 </DeclarationFile>
  1. 在xml的例子中,在<Declaration Id="DEC"><DokPVNv4>之前有-。用我的php代码没有。这些-意味着什么?如何使用php创建它们?

  2. <Declaration Id="DEC"><DokPVNv4>中的xml示例中,没有/。用我的代码我得到了/。如何删除它们(以及这些/的含义)?

我想所有的差异都和根元素有关吧?

更新。从答案中可以理解:如果某个元素/标签没有结束标签,那么元素以/结束。和-+,是的,浏览器创建了它们。

-可能是+/-按钮中的浏览器绘图,该按钮允许您打开/关闭这些元素。根据XML规范,/>REQUIRED。任一标签都是容器,例如

<foo>
   ...
</foo>

或者它们是singleton标签,在这种情况下,它们必须是自关闭的,例如

<bar />

您的代码没有显式地添加它们,但由于它们在xml中是必需的,DOM系统正在为您添加它们。

您的代码创建了嵌套错误的元素。尝试更改此

$root->appendChild($DokPVNv4);

到这个

 $child_declaration_id->appendChild($DokPVNv4);