2014-02-13 41 views
1

子元素我试图完成以下任务:在XML中创建

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

我不能获得工作的一部分是在德之间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(); 
?> 

回答

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文本,但这可能不是在某类情况下是足够的价值没有逃脱(参考:php.net - 我只是在这里使用它来快速)。

示例输出:

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

感谢您的帮助,但现在,你怎么把author元素book元素 – user3306814

+0

内它的完成同样的方式多。我更新了我的代码示例。你只是在改变你追加孩子的元素。 – stckrboy

+0

谢谢,这对我非常有帮助! – Roger

2

Addding节点到DOM需要3个步骤

  1. 使用文献方法,如createElement()createTextNode()
  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(); 
+0

感谢您的帮助,我明白了一切! – user3306814