在XML中创建子元素 [英] Creating sub element in XML
本文介绍了在XML中创建子元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试完成以下任务:
I am trying to accomplish the following:
<?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>
我无法工作的部分是介于两者之间的作者元素. 但是我不能再这样下去了,香港专业教育学院尝试了很多事情,但似乎只给了我空白页面. 我现在所拥有的:
The part i cant get to work is de author element in between. But i cant go futher then is, ive tried a lot of things but it seems to only give me blanco pages. What i have now:
<?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个步骤
Addding nodes to the DOM requires 3 Steps
- 使用诸如
createElement()
或createTextNode()
之类的Document方法创建节点
- 配置节点并追加子节点
- 将节点附加到其父节点
- Create the node using Document methods like
createElement()
orcreateTextNode()
- Configure the node and append child nodes
- Append the node to its parent node
第2步和第3步是可交换的.您可以在附加节点之后或之前配置节点. appendChild()
返回附加节点.
Step 2 and 3 are exchangeable. You can configure a node after you appended it or before. appendChild()
returns the append node.
我根据结果xml中的级别缩进了调用:
I indented the calls depending on the level in the result 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();
这篇关于在XML中创建子元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文