php – 用XML创建子元素

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了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();

脚本宝典总结

以上是脚本宝典为你收集整理的php – 用XML创建子元素全部内容,希望文章能够帮你解决php – 用XML创建子元素所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。