php – 用XML创建子元素

前端之家收集整理的这篇文章主要介绍了php – 用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>

我无法工作的部分是两者之间的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();
?>
将节点添加到DOM需要3个步骤

>使用createElement()或createTextNode()等Document方法创建节点
>配置节点并附加子节点
>将节点附加到其父节点

第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();
原文链接:https://www.f2er.com/php/139105.html

猜你在找的PHP相关文章