0

PHP のクラスを使用DOMDocumentして xml ファイルを作成しています。コードは次のとおりです。

    $xmlObject = new DOMDocument('1.0', 'utf-8');
    //root node -- books
    $books = $xmlObject->createElement('books');
    //book node
    $book = $xmlObject->createElement('book');
    //book node's attribute -- index
    $index = new DOMAttr('index', '1');
    $book->appendChild($index);
    //name node
    $name = $xmlObject->createElement('name', 'Maozedong');
    //name node's attribute -- year
    $year = new DOMAttr('year', '1920');
    $name->appendChild($year);
    $book->appendChild($name);
    //story node
    $story = $xmlObject->createElement('story');
    $title = $xmlObject->createElement('title', 'Redrevolution');
    $quote = $xmlObject->createElement('quote', 'LeaveoffHunan');
    $story->appendChild($title);
    $story->appendChild($quote);
    $book->appendChild($story);
    $books->appendChild($book);
    if ($xmlObject->save('xml/books.xml') != false){
        echo 'success';
    }else{
        echo 'error';
    }

books.xml の内容は、次の 1 行だけです。

<?xml version="1.0" encoding="utf-8"?>

他のノードは存在しません。コードにエラーはありますか?

4

1 に答える 1

-2

$xmlObject に books ノードを追加するのを忘れました。追加: $xmlObject->appendChild($books);

于 2012-10-25T16:44:17.917 に答える