Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
651 views
in Technique[技术] by (71.8m points)

php - Creating sub element in 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();
?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Addding nodes to the DOM requires 3 Steps

  1. Create the node using Document methods like createElement() or createTextNode()
  2. Configure the node and append child nodes
  3. 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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...