2012-02-12 60 views
0
  1. 我怎么能写在标签XML行改正的地方吗? <newWord><Heb>bull</Heb><Eng>bu</Eng></newWord>
  2. 当我运行的代码中,我得到的错误“1号线误差在列1:文件是空的。”有什么错
  3. 如何添加标签<newWoed> id = 1,id = 2 like <newWoed id=3>错误:当我写的Xml文件

    非常感谢帮助。

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

,XML是缺少'newWord'元素后'Favorite_Word'结束标记。 – daniel 2012-02-12 09:13:28

+0

没有不缺少标签“Favorite_Word”。 – 2012-02-12 09:30:35

+0

这似乎是一个复制过去的问题! – tlenss 2012-02-12 10:02:36

回答

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');