<newWord><Heb>bull</Heb><Eng>bu</Eng></newWord>
如何添加标记<newWoed>
id = 1,id = 2,如<newWoed id=3>
?
非常感谢您的帮助。
xml文件:
<?xml version="1.0" encoding="windows-1255"?>
<Favorite_Word xml:lang="EN"><newWord><Heb>bull</Heb><Eng>bu</Eng></newWord>
<newWord>
<Heb>Cow</Heb>
<Eng>Co</Eng>
</newWord>
<newWord>
<Heb>Camel</Heb>
<Eng>Ca</Eng>
</newWord>
<newWord>
<Heb>Bull</Heb>
<Eng>BUl</Eng>
</newWord>
</Favorite_Word>
php文件:
function addElementToXml($doc='')
{
if(!$doc)
{
$doc = new DOMDocument();
// we want a nice output
$doc->formatOutput = true;
$doc->load('Dictionary_user.xml');
}
$Dictionary_user = $doc->documentElement;
#var_dump($Dictionary_user->childNodes->item(0)->parentNode->nodeName);
$newWord = $doc->createElement('newWord');
#$newWord->setAttribute('isbn','http-equiv="Content-Type" content="text/html; CHARSET=windows-1255"');
$prop = $doc->createElement('Heb','bull');
$newWord->appendChild($prop);
$prop = $doc->createElement('Eng','bu');
$newWord->appendChild($prop);
$Dictionary_user->childNodes->item(0)->parentNode->insertBefore($newWord,$Dictionary_user->childNodes->item(0));
header("Content-type: text/xml");
echo $doc->saveXML();
$doc->save("Dictionary_user.xml");
}
addElementToXml();
答案 0 :(得分:0)
如果收到错误消息:
DOMDocument::load(): Document is empty in Dictionary_user.xml, line: 1 in foo.php on line 10
这意味着它要么读错了文件。或者文件只是空的。
要添加ID,您只需在要创建的节点上调用setAttribute。
$newWord = $doc->createElement('newWord');
$newWord->setAttribute('id','1');