可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I am trying to accomplish the following:
Harry potterAdventure | Family | Fantasy850Jhon Doehttp://wikipedia....lorem ipsum blabla
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:
Harry potterAdventure | Family | Fantasy850lorem ipsum blablacreateElement('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:
Addding nodes to the DOM requires 3 Steps
- Create the node using Document methods like
createElement()
or createTextNode()
- Configure the node and append child nodes
- Append the node to its parent node
Step 2 and 3 are exchangeable. You can configure a node after you appended it or before. appendChild()
returns the append node.
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();
回答2:
What you need to do here, is append the author details to the author element and not the root element. So something like this would work:
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();
Also note that you can save some space creating text nodes by adding the text inside createElement, though that may not suffice in cetain circumstances as the value is not escaped (ref: php.net - I just used it here for quickness).
Sample output:
John Doe