我有代码将如下所示的项目添加到xml文件中:
<newWord>
<Heb>rer</Heb>
<Eng>twew</Eng>
</newWord>
问题是当我添加元素时,它将不在正确的位置,它来自XML标签,如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<newWord>
<Heb>hebword</Heb>
<Eng>banna</Eng>
</newWord>
</xml>
<newWord>
<Heb>rer</Heb>
<Eng>twew</Eng>
</newWord>
如何将新元素添加到正确的位置?
我的代码:
<?php
$wordH=$_GET['varHeb'];
$wordE=$_GET['varEng'];
$domtree='';
if(!$domtree)
{
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
$domtree->load('Dictionary_user.xml');
}
$Dictionary_user = $domtree->documentElement;
$currentitem = $domtree->createElement("newWord");
$currentitem = $domtree->appendChild($currentitem);
$currentitem->appendChild($domtree->createElement('Heb', $wordH));
$currentitem->appendChild($domtree->createElement('Eng',$wordE));
$Dictionary_user->childNodes->item(0)->parentNode->insertBefore($currentitem,$Dictionary_user->childNodes->item(0));
header("Content-type: text/xml");
$domtree->save("Dictionary_user.xml");
/* get the xml printed */
echo $domtree->saveXML();
?>
也许你想要这样的东西:
<?php
if(!empty($_GET['varHeb']) && !empty($_GET['varEng'])){
//Dont forget utf-8 if using Hebrew letters
header('Content-type: text/xml; charset=utf-8');
//Load It
$xml = simplexml_load_file('./Dictionary_user.xml');
//Add a new node & add the children
$node = $xml->addChild('newWord');
$node->addChild('Heb', trim($_GET['varHeb']));
$node->addChild('Eng', trim($_GET['varEng']));
//DOMDocument to format code output
$dom = new DOMDocument('1.0', 'UTF-8');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($xml->asXML());
echo $dom->saveXML();
}else{
//params not set for $_GET
}
/**
* Result
<?xml version="1.0" encoding="UTF-8"?>
<xml>
<newWord>
<Heb>hebword</Heb>
<Eng>banna</Eng>
</newWord>
<newWord>
<Heb>אנחנו אוהבים גלישת מחסנית</Heb>
<Eng>We Love Stack overflow</Eng>
</newWord>
</xml>
*/
问题是你在做什么:
$currentitem = $domtree->createElement("newWord");
$currentitem = $domtree->appendChild($currentitem);
将其更改为
$currentitem = $domtree->createElement("newWord");
$currentitem = $domtree->documentElement->appendChild($currentitem);
这会将它们追加到根节点。
代码中还有一些其他需要改进的地方:
$wordH=$_GET['varHeb'];
$wordE=$_GET['varEng'];
确保对此内容进行消毒/过滤。DOM 非常擅长确保不会插入恶意内容,但任何来自外部世界的内容都应该进行健全性检查。
$domtree='';
if(!$domtree)
{
这样做毫无意义。您$domtree
为空字符串,因此将始终触发if
块。删除该部分,然后只需加载 XML。
$domtree = new DOMDocument('1.0', 'UTF-8');
$domtree->formatOutput = true;
$domtree->load('Dictionary_user.xml');
}
当您使用 DOM load
字符串或文件时,它将丢弃您放入构造函数中的任何参数,改用在字符串或文件的 xml prolog 中找到的参数。因此,在更新 DOMDocument 时,您不需要提供它们。
$Dictionary_user = $domtree->documentElement;
这是要追加到的根节点 ( <xml>
(。
$currentitem = $domtree->createElement("newWord");
$currentitem = $domtree->appendChild($currentitem);
这是发生错误的地方。创建一个新元素,然后将其附加到 $domtree
,即整个文档。在 XPath 查询中,当您要追加到/xml
时,$domtree
/
,例如根元素。
$currentitem->appendChild($domtree->createElement('Heb', $wordH));
$currentitem->appendChild($domtree->createElement('Eng',$wordE));
这按预期工作。您正在添加到<newWord>
$Dictionary_user
->childNodes
->item(0)
->parentNode
->insertBefore($currentitem, $Dictionary_user->childNodes->item(0));
您正在从根元素(<xml>
(遍历到第一个子元素,然后返回到父元素,这显然又是根元素。所以整个遍历是不必要的。事实上,除非您要将追加的<newWord>
移动到根元素的第一个子元素上方,否则根本不需要整个块。