php – 用XML创建子元素
发布时间:2020-12-13 13:53:48 所属栏目:PHP教程 来源:网络整理
导读:我正在努力完成以下任务: ?xml version="1.0"?booksbooknameHarry potter/namecategoryAdventure | Family | Fantasy/categorypages850/pagesauthorauthor_nameJhon Doe/author_nameauthor_wikihttp://wikipedia..../author_wiki/authordescriptionlorem ip
我正在努力完成以下任务:
<?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元素. <?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(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |