duanfangfei5558 2014-02-13 16:17
浏览 109

用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();
?>
  • 写回答

2条回答 默认 最新

  • dongyou5068 2014-02-13 16:41
    关注

    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:

    <book>
      <author>
        <author_name>John Doe</author_name>
      </author>
    </book>
    
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?