用XML创建子元素

时间:2014-02-13 16:17:18

标签: php xml domdocument appendchild createelement

我正在努力完成以下任务:

<?xml version="1.0"?>
<books>
<book>
<name>Harry potter</name>
<category>Adventure | Family | Fantasy</category>
<pages>850</pages>
<author>
<author_name>Jhon Doe</author_name>
<author_wiki>http://wikipedia....</author_wiki>
</author>
<description>lorem ipsum blabla</description>
</book>
</books>

我无法上班的部分是其中的de author元素。 但我不能再进一步了,我已经尝试了很多东西,但它似乎只给我blanco页面。 我现在拥有的:

<?xml version="1.0"?>
<books>
<book>
<name>Harry potter</name>
<category>Adventure | Family | Fantasy</category>
<pages>850</pages>
<description>lorem ipsum blabla</description>
</book>
</books>

<?php header('Content-Type: text/xml;'); 
// Start XML file, create parent node
$doc = new DOMDocument('1.0');
$root = $doc->createElement('books');
$root = $doc->appendChild($root);
// we want a nice output
$doc->formatOutput = true;
$user = $doc->createElement('book');
$user = $doc->appendChild($user);
$title = $doc->createElement('name');
$title = $user->appendChild($title);
$text = $doc->createTextNode('Harry potter');
$text = $title->appendChild($text);
$title = $doc->createElement('category');
$title = $user->appendChild($title);
$text = $doc->createTextNode('Adventure | Family | Fantasy');
$text = $title->appendChild($text);
$title = $doc->createElement('pages');
$title = $user->appendChild($title);
$text = $doc->createTextNode('850');
$text = $title->appendChild($text);
$title = $doc->createElement('description');
$title = $user->appendChild($title);
$text = $doc->createTextNode('lorem ipsum blabla');
$text = $title->appendChild($text);
$user = $root->appendChild($user);
echo $doc->saveXML();
?>

2 个答案:

答案 0 :(得分:3)

将节点添加到DOM需要3个步骤

  1. 使用createElement()createTextNode()
  2. 等文档方法创建节点
  3. 配置节点并附加子节点
  4. 将节点附加到其节点
  5. 步骤2和3是可交换的。您可以在添加节点之后或之前配置节点。 appendChild()返回追加节点。

    我根据结果xml中的级别缩进调用:

    $doc = new DOMDocument('1.0');
    $doc->formatOutput = true;
    
    $books = $doc->appendChild($doc->createElement('books'));
      $book = $books->appendChild($doc->createElement('book'));
        $name = $book->appendChild($doc->createElement('name'));
          $name->appendChild($doc->createTextNode('Harry potter'));
        $category = $book->appendChild($doc->createElement('category'));
          $category->appendChild($doc->createTextNode('Adventure | Family | Fantasy'));
        $pages = $book->appendChild($doc->createElement('pages'));
          $pages->appendChild($doc->createTextNode('850'));
    
        $author = $book->appendChild($doc->createElement('author'));
          $authorName = $author->appendChild($doc->createElement('author_name'));
            $authorName->appendChild($doc->createTextNode('John Doe'));
          $authorWiki = $author->appendChild($doc->createElement('author_wiki'));
            $authorWiki->appendChild($doc->createTextNode('http://wikipedia....'));
    
        $description = $book->appendChild($doc->createElement('description'));
          $description->appendChild($doc->createTextNode('lorem ipsum blabla'));
    
    echo $doc->saveXML();
    

答案 1 :(得分:1)

您需要在此处执行的操作是将作者详细信息附加到author元素而不是根元素。所以这样的事情会起作用:

header('Content-Type: text/xml;'); 
$doc = new DOMDocument('1.0');
$doc->formatOutput = true;

$book = $doc->createElement("book");
$doc->appendChild($book);

$author = $doc->createElement("author");
$book->appendChild($author); // add author as child of book

// you can add content at the same time as creating the element
$author_name = $doc->createElement("author_name", "John Doe");
// append author name to author element
$author->appendChild($author_name); 

echo $doc->saveXML();

另请注意,您可以通过在createElement中添加文本来节省一些创建文本节点的空间,但这在cetain情况下可能还不够,因为值没有被转义(参考:php.net - 我只是在这里使用它速度)。

示例输出:

<book>
  <author>
    <author_name>John Doe</author_name>
  </author>
</book>