如何通过dom和php显示数据

Posted

技术标签:

【中文标题】如何通过dom和php显示数据【英文标题】:How to display data through dom and php 【发布时间】:2017-04-24 16:29:24 【问题描述】:

这是我的php代码:执行这个php后,数据显示在这个文件的底部。我希望它只添加一个人员元素。

$name = $_POST['sName'];
$ic = $_POST['sIC'];
$email = $_POST['sEmail'];
$address = $_POST['sAddress'];
$nophone = $_POST['sPhone'];

$dom = new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;


//loading the existing XML file
$dom->load('phone_XML.xml');

在同一文件 php 中写入新的 xml 节点:

$phone = $dom->documentElement;
$staff = $dom->documentElement;
// appendChild() can be combined with the create*()
//$staff = $phone->appendChild($dom->createElement("staff"));

// you missed the `s` element node
$s = $staff->appendChild($dom->createElement('s'));
$s->setAttribute('id', 'S002Doe');

$s
->appendChild($dom->createElement('sName'))
->appendChild($dom->createTextNode($name));
$s
->appendChild($dom->createElement('sIC'))
->appendChild($dom->createTextNode($ic));
$s
->appendChild($dom->createElement('sEmail'))
->appendChild($dom->createTextNode($email));
$s
->appendChild($dom->createElement('sAddress'))
->appendChild($dom->createTextNode($address));
$s
->appendChild($dom->createElement('sPhone'))
->appendChild($dom->createTextNode($nophone));

保存在同一个文件中:

$dom->save('phone_XML.xml');
echo "<script language='javascript'>alert('Add item succesful');</script>";
echo "<script language='JavaScript'>window.location = 'index.php';</script>";
?>

输出应该是这样的:

<phone>
<staff>
<s id="S001Akmal">
  <sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
  <sIC>940228-10-6101</sIC>
  <sEmail>muhdnurakmal@velocity.net.my</sEmail>
  <sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
  <sPhone>012-3456789</sPhone>
</s>
<s id="S002Atikah">
  <sName> Nur Atikah Binti Rohizad </sName>
  <sIC>940421-14-6236</sIC>
  <sEmail>nuratikah@gmail.com</sEmail>
  <sAddress>No 24, Taman Saujana Impian, 43000 Kajang</sAddress>
  <sPhone>013-6752800 </sPhone>
</s>
</staff>
</phone>

【问题讨论】:

【参考方案1】:

这里的问题可能不同但含义相同

PHP XML how to output nice format

祝你好运

【讨论】:

【参考方案2】:

您正在加载现有的 XML。这将包括一些带有空格的文本节点(换行符、缩进空格)。 DOM 在序列化过程中识别它们,并尝试在重新格式化时保留原始 XML(包括空白节点)。 DOMDocument::$preserveWhiteSpace 属性允许它在解析时删除空白节点。

其他要点:

您错过了s 元素节点。 phone 元素节点是它在 DOMDocument 对象的属性中可用的文档元素。 appendChild() 可以与 create*() 调用结合使用。它返回附加的节点。 添加文本节点。不要使用 createElement() 的第二个参数,它有一个损坏的转义。

放在一起:

$xml = <<<'XML'
<phone>
<staff>
    <s id="S001Akmal">
        <sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
        <sIC>940228-10-6101</sIC>
        <sEmail>muhdnurakmal@velocity.net.my</sEmail>
        <sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
        <sPhone>012-3456789</sPhone>
    </s>
</staff>
</phone>
XML;

$document = new DOMDocument();
$document->formatOutput = TRUE;
$document->preserveWhiteSpace = FALSE;
$document->loadXml($xml);

$phone = $document->documentElement;
// appendChild() can be combined with the create*()
$staff = $phone->appendChild($document->createElement("staff"));

// you missed the `s` element node
$s = $staff->appendChild($document->createElement('s'));
$s->setAttribute('id', 'S002Doe');

$s
  ->appendChild($document->createElement('sName'))
  ->appendChild($document->createTextNode("John Doe"));
$s
  ->appendChild($document->createElement('sIC'))
  ->appendChild($document->createTextNode("123"));
$s
  ->appendChild($document->createElement('sEmail'))
  ->appendChild($document->createTextNode("john@example.tld"));
$s
  ->appendChild($document->createElement('sAddress'))
  ->appendChild($document->createTextNode("somewhere"));
$s
  ->appendChild($document->createElement('sPhone'))
  ->appendChild($document->createTextNode("456"));

echo $document->saveXml();

输出:

<?xml version="1.0"?>
<phone>
  <staff>
    <s id="S001Akmal">
      <sName>Muhammad Nur Akmal Bin Mohd Halim </sName>
      <sIC>940228-10-6101</sIC>
      <sEmail>muhdnurakmal@velocity.net.my</sEmail>
      <sAddress>Lot 2863 Jalan Limau, Meru 42200 Klang</sAddress>
      <sPhone>012-3456789</sPhone>
    </s>
  </staff>
  <staff>
    <s id="S002Doe">
      <sName>John Doe</sName>
      <sIC>123</sIC>
      <sEmail>john@example.tld</sEmail>
      <sAddress>somewhere</sAddress>
      <sPhone>456</sPhone>
    </s>
  </staff>
</phone>

提示:如果发布问题,请尝试构建可以在没有外部依赖项的情况下运行的完整示例。

【讨论】:

感谢先生的关心。我只是编辑我的帖子先生。先生,你能帮我吗?

以上是关于如何通过dom和php显示数据的主要内容,如果未能解决你的问题,请参考以下文章

访问数据库后更改 DOM

如何在客户端存储一个php数组?

如何将出生日期从数据库显示到表单并在 php 和 PDO 中更新?

如何将来自API的数据注入DOM [重复]

如何使用 PHP Simple HTML DOM Parser 提取标题和元描述?

android如何显示数据库内容