<;中的PHP空白字段/属性>;

<;中的PHP空白字段/属性>;,php,xml,Php,Xml,我想知道如果最终用户没有填写名为“name”的表单字段并将其留空,如何将empy值返回到我的“attribute”中 我的实际代码: $domElement = $domDocument->createElement('attribute', $posted_data['name']); $domAttribute = $domDocument->createAttribute('domainname'); $domAttribute->value = 'Name'; $dom

我想知道如果最终用户没有填写名为“name”的表单字段并将其留空,如何将empy值返回到我的“attribute”中

我的实际代码:

$domElement = $domDocument->createElement('attribute', $posted_data['name']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';
$domElement->appendChild($domAttribute);
<?php
$domDocument = new DOMDocument;
$domElement = $domDocument->createElement('attribute');

$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';

$domElement->appendChild($domAttribute);

echo $domDocument->saveXML($domElement, LIBXML_NOEMPTYTAG), PHP_EOL;
表单字段“名称”中输入为空时所需的输出:

<attribute domainname="Name"></attribute>
<attribute domainname="Name"/>

基于实际代码的电流输出:

<attribute domainname="Name"></attribute>
<attribute domainname="Name"/>

正如您所见,它不是由
完成的,而是由
/>


有什么线索吗?

您可以使用constLIBXML\u NOEMPTYTAG

$domElement = $domDocument->createElement('attribute', $posted_data['name']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';
$domElement->appendChild($domAttribute);
<?php
$domDocument = new DOMDocument;
$domElement = $domDocument->createElement('attribute');

$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';

$domElement->appendChild($domAttribute);

echo $domDocument->saveXML($domElement, LIBXML_NOEMPTYTAG), PHP_EOL;

您可以使用constLIBXML\u NOEMPTYTAG

$domElement = $domDocument->createElement('attribute', $posted_data['name']);
$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';
$domElement->appendChild($domAttribute);
<?php
$domDocument = new DOMDocument;
$domElement = $domDocument->createElement('attribute');

$domAttribute = $domDocument->createAttribute('domainname');
$domAttribute->value = 'Name';

$domElement->appendChild($domAttribute);

echo $domDocument->saveXML($domElement, LIBXML_NOEMPTYTAG), PHP_EOL;

虽然在视觉上不完全相同,但这两个示例在技术上是完全相同的。@WebCode.ie因此,从技术上讲,导入.xml文件是否可以正常工作?@Wager-Yes应该可以。@WebCode.ie谢谢。通常被称为自动关闭标记,以防您怀疑……虽然视觉上不完全相同,这两个示例在技术上是相同的。@WebCode.ie因此,从技术上讲,导入.xml文件时它能正常工作吗?@Wager-Yes应该可以。@WebCode.ie现在感谢您。通常被称为自动关闭标记,以防您怀疑…我确实使用了方法
$content=chunk\u split(base64\u encode($domDocument->saveXML($domElement,LIBXML_NOEMPTYTAG));
但它仍然是一样的,
echo$domDocument->saveXML($domElement,LIBXML_NOEMPTYTAG);
不起作用。我用完整的例子更新了我的答案。正如你所看到的,它对我来说仍然不起作用,因为我依赖于用户的输入
('attribute',$posted\u data['name'))
。我不能像你的例子那样仅仅删除$posted_数据。我确实使用了方法
$content=chunk_split(base64_encode($domDocument->saveXML($domElement,LIBXML_NOEMPTYTAG));
但是它仍然是一样的,
echo$domDocument->saveXML($domElement,LIBXML_NOEMPTYTAG)
不起作用。我用完整的示例更新了我的答案。正如您所看到的,它对我来说仍然不起作用,因为我依赖于用户的输入
('attribute',$posted_data['name'))
。我不能按照您的示例删除$posted_数据。