2012-02-12 26 views
0
  1. タグのXML行を正しい場所に書き込むにはどうすればよいですか? <newWord><Heb>bull</Heb><Eng>bu</Eng></newWord>
  2. 私はエラーを取得するコードを実行する「コラム1の1行目でエラー:ドキュメントが空である」。何が間違っ
  3. タグに追加する方法<newWoed> id = 1、id = 2 <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

+0

いいえ、欠落していないタグ 'Favorite_Word'はありません。 –

+0

それはちょうどコピーの過去の問題であるようです! – tlenss

答えて

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