【问题标题】:Creating sub element in XML在 XML 中创建子元素
【发布时间】:2014-03-12 15:35:31
【问题描述】:

我正在努力完成以下工作:

<?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>

我无法开始工作的部分是介于两者之间的作者元素。 但是我不能再进一步了,我尝试了很多东西,但似乎只给了我空白页。 我现在拥有的:

<?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();
?>

【问题讨论】:

    标签: php xml domdocument appendchild createelement


    【解决方案1】:

    向 DOM 添加节点需要 3 个步骤

    1. 使用像createElement()createTextNode()这样的Document方法创建节点
    2. 配置节点并追加子节点
    3. 将节点追加到其父节点节点

    第 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();
    

    【讨论】:

    • 感谢您的帮助,我现在明白了一切!
    【解决方案2】:

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

    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 中添加文本来节省一些空间来创建文本节点,尽管在某些情况下这可能还不够,因为值没有被转义(参考:php.net - 我只是在这里使用它是为了快速) .

    样本输出:

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

    【讨论】:

    • 感谢您的帮助,但是现在;您如何将 author 元素放入 book 元素中
    • 以同样的方式完成。我已经更新了我的代码示例。您只是在更改将孩子附加到的元素。
    • 谢谢,这对我帮助很大!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多