设为首页收藏本站

就爱编程论坛

 找回密码
 注册

人人连接登陆

无需注册,直接登录

用新浪微博连接

一步搞定

QQ登录

只需一步,快速开始

查看: 610|回复: 0
打印 上一主题 下一主题

php读写xml实例 [复制链接]

Rank: 9Rank: 9Rank: 9

  • TA的每日心情
    无聊
    2025-5-27 03:37:20
  • 签到天数: 366 天

    [LV.9]以坛为家II

    论坛先锋 学习至圣 荣誉成员 论坛元老 活跃之星 终极领袖

    我玩的应用:

    跳转到指定楼层
    楼主
    发表于 2011-4-10 03:59:29 |只看该作者 |倒序浏览
    library.xml:
    <?xml version="1.0" encoding="gb2312"?>
    <root>
    <groups>
    <group gid="1">super</group>
      <group gid="2">admin</group>
        <group gid="3">change</group>
        <group gid="4">program</group>
    </groups>
    <users>
       <user>
         <name>Apache 2</name>
         <author>Peter Wainwright</author>
         <publisher>Wrox</publisher>
         <group>1</group>
       </user>
       <user>
         <name>Advanced PHP Programming</name>
         <author>George Schlossnagle</author>
         <publisher>Developer Library</publisher>
         <group>1</group>
         <group>3</group>
       </user>
       <user>
         <name>Visual FoxPro 6 - Programmers Guide</name>
         <author>Eric Stroo</author>
         <publisher>Microsoft Press</publisher>
         <group>2</group>
       </user>
       <user>
         <name>Mastering Java 2</name>
         <author>John Zukowski</author>
         <publisher>Sybex</publisher>
         <group>4</group>
       </user>
    </users>
    </root>
    /**********************************************/
    readlibrary.php:
    <?php
    $xml = new DOMDocument('1.0');
    $xml->load( 'library.xml' );
    $groups = array();
    $XMLGroups = $xml->getElementsByTagName('groups')->item(0);
    foreach($XMLGroups->getElementsByTagName('group') as $groupNode) {
        /*注意我们是如何得到属性的*/
        $gid = $groupNode->getAttribute('gid');
        $groups[$gid] = $groupNode->firstChild->nodeValue;
    }
    ?>
    <html>
    <head>
    <title>XML Library</title>
    </head>
    <body>
    <?
    foreach($xml->getElementsBytagName('user') as $user):
       $name = $user->getElementsByTagName('name')->item(0)->firstChild->nodeValue;
       $author = $user->getElementsByTagName('author')->item(0)->firstChild->nodeValue;
       $userCategories = $user->getElementsByTagName('group');
       $catList = '';
       foreach($userCategories as $category) {
         $catList .= $groups[$category->firstChild->nodeValue] . ', ';
       }
       $catList = substr($catList, 0, -2); ?>
    <!--
    <div>
    <h2><?php echo($name) ?></h2>
    <b>Author:</b>: <?php echo($author) ?></br>
    <b>group: </b>: <?php echo($catList) ?></br>
    </div>
    -->
    <?php
    //echo($name ." - " . $author. " - ". $catList ."<br>\n" );
    echo($name ." || " . $catList ."<br>\n" );
    endforeach; ?>
    </html>
    /************************************/
    writelibrary.php:
    <?php
    $doc = new DOMDocument();
    $doc->formatOutput = true;
    $r = $doc->createElement( "root" );
    $doc->appendChild( $r );
    setGroup();
    setUser();
    echo $doc->saveXML();
    $doc->save("book1.xml");
    function setGroup()
    {
    global $doc, $r;
    $groups = array();
    $groups [] = array(
    'id' => '1',
    'name' => 'Jack',
    );
    $groups [] = array(
    'id' => '2',
    'name' => 'Herrington',
    );
    $groups [] = array(
    'id' => '3',
    'name' => 'Hello',
    );
    $b = $doc->createElement( "groups" );
       
    foreach( $groups as $group )
    {
       $grp = $doc->createElement( "group" );
       $grp->appendChild( $doc->createTextNode( $group['name'] ) );
       
       // create attribute node
    $id = $doc->createAttribute("gid");
    $grp->appendChild($id);
    $idValue = $doc->createTextNode( $group['id'] );
    $id->appendChild($idValue);
       $b->appendChild( $grp );
    }
    $r->appendChild( $b );
    }
    function setUser()
    {
    global $doc, $r;
    $users [] = array(
    'title' => 'PHP Hacks',
    'author' => 'Jack Herrington',
    'publisher' => "O'Reilly"
    );
    $users [] = array(
    'title' => 'Podcasting Hacks',
    'author' => 'Jack Herrington',
    'publisher' => "O'Reilly"
    );
    $u = $doc->createElement( "users" );
    $doc->appendChild( $u );
    foreach( $users as $user )
    {
       $b = $doc->createElement( "user" );
       
       $author = $doc->createElement( "author" );
       $author->appendChild( $doc->createTextNode( $user['author'] ) );
       $b->appendChild( $author );
       
       $title = $doc->createElement( "title" );
       $title->appendChild( $doc->createTextNode( $user['title'] ) );
       $b->appendChild( $title );
       
       $publisher = $doc->createElement( "publisher" );
       $publisher->appendChild( $doc->createTextNode( $user['publisher'] ) );
       $b->appendChild( $publisher );
       
       $u->appendChild( $b );
    }
    $r->appendChild( $u );
    }
    ?>

    分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    分享分享0 收藏收藏0 支持支持0 反对反对0 分享到人人 转发到微博
    [img=http://mail.qq.com/cgi-bin/qm_share?t=qm_mailme&email=fRUcHhYWGAQ9GxIFEBwUEVMeEhA]http://rescdn.qqmail.com/zh_CN/htmledition/images/function/qm_open/ico_mailme_02.png[/img]

    使用道具 举报

    您需要登录后才可以回帖 登录 | 注册 人人连接登陆

    晴云孤魂's Blog|就爱编程搜帖|手机版|Archiver|就爱编程论坛     

    GMT+8, 2025-7-2 14:02 , Processed in 0.099317 second(s), 27 queries .

    Powered by Discuz! X2

    © 2001-2011 Comsenz Inc.

    回顶部