simplexml学习

user.xml文件路径:"./model/user.xml"

如何调用xml:

1 header("Content-type: text/html; charset=utf-8"); 
2 $str=simplexml_load_file("./model/user.xml");
View Code

像空文件写入xml内容:

 1 header ( "Content-Type:text/html;charset=utf-8" );
 2 $string = <<<XML
 3 <a>
 4  <b>
 5   <c>text</c>
 6   <c>stuff</c>
 7  </b>
 8  <d>
 9   <c>code</c>
10  </d>
11 </a>
12 XML;
13 
14 $xml = new SimpleXMLElement ( $string );
15 if ($xml->asXML ( 'user.xml' )) {
16     echo "成功写入xml文件";
17 }
18 else{
19     echo "未写入成功";
20 }
View Code

如何更改属性值:

注意一定要让文件有读写权限,不然 asXML() 无法写入值

1  $str->name="鬼泣";
2  $str->age="14";
3  $str->asXML("./model/user.xml")
View Code 

user.xml文件结构:

 1 <?xml version="1.0"?>
 2 <root>
 3     <user>
 4         <username>
 5              xyt
 6         </username>
 7         <password>
 8             123
 9         </password>
10     </user>
11         <user>
12         <username>
13              lzj
14         </username>
15         <password>
16             1111
17         </password>
18     </user>
19 </root>
View Code

读取用户名和密码

 1 <?php
 2 header ( "Content-Type:text/html;charset=utf-8" );
 3 $xml=simplexml_load_file("./model/user.xml");
 4 
 5 $_user=$xml->xpath("/root/user");
 6 $user=array();
 7 
 8 
 9 while(list(,$node)=each($_user)){
10     $a=array(trim($node->username    ->__toString()),trim($node->password->__toString()));
11     $user[]=$a;
12 }
13 
14 print_r($user);
15 ?>
View Code

 Array (

[0] => Array ( [0] => xyt [1] => 123 )

[1] => Array ( [0] => lzj [1] => 1111 )

)

增加用户名和密码:

header ( "Content-Type:text/html;charset=utf-8" );
$xml = simplexml_load_file ( "./model/user.xml" );

$_user = $xml->xpath ( "/root/user" );
function insertintoxml($username, $password, $xml, $url = "./model/user.xml") {
    try {
        $strname = $xml->xpath ( "/root/user/username" );
        print_r ( $strname );
        
        // 不能存在相同的用户名:
        foreach ( $strname as $key => $value ) {
            if ($username == trim ( $value )) {            
                throw new Exception("Sorry,name was used!");
            }    
        }
        
             $length=count($xml->user);
             //添加一个子节点 user;
             $xml->addChild("user","");
             $xml->user[$length]->addChild("username",$username);
             $xml->user[$length]->addChild("password",$password);
        if (! $xml->asxml ( $url )) {
            throw new Exception("failed,make you configfile rewriteable!");        
        }
        return true;
    } catch ( Exception $e ) {
        echo $e->getMessage();
    }
}

if (insertintoxml ( "xyt", "asdfads", $xml )) {
    echo "插入成功!";
}
View Code

 

删除用户名和密码:

 

 1 function deletefrom($username, $xml, $url = "./model/user.xml") {
 2     try {
 3         
 4         $strname = $xml->xpath ( "/root/user/username" );
 5         foreach ( $strname as $key => $value ) {
 6             if (trim($value) == $username){
 7                 unset ( $xml->user [$key] );            
 8             }
 9         }
10         
11 
12         if (! $xml->asXML ( $url )) {
13             throw new Exception ( "failed,make you configfile rewriteable!" );
14         }
15         return true;
16     } catch ( Exception $e ) {
17         echo $e->getMessage ();
18     }
19 }
20 //   insertintoxml("1", "asdf", $xml);
21 //   insertintoxml("2", "asdf", $xml);
22 //   insertintoxml("3", "asdf", $xml);
23 //   insertintoxml("4", "asdf", $xml);
24 //   insertintoxml("5", "asdf", $xml);
25 (deletefrom ( "4", $xml ));
View Code

 

整体代码:

 1 <?php
 2 header ( "Content-Type:text/html;charset=utf-8" );
 3 $xml = simplexml_load_file ( "./model/user.xml" );
 4 
 5 $_user = $xml->xpath ( "/root/user" );
 6 function insertintoxml($username, $password, $xml, $url = "./model/user.xml") {
 7     try {
 8         $strname = $xml->xpath ( "/root/user/username" );
 9         
10         // 不能存在相同的用户名:
11         foreach ( $strname as $key => $value ) {
12             if ($username == trim ( $value )) {
13                 throw new Exception ( "Sorry,name was used!" );
14             }
15         }
16         
17         $length = count ( $xml->user );
18         // 添加一个子节点 user;
19         $xml->addChild ( "user", "" );
20         $xml->user [$length]->addChild ( "username", $username );
21         $xml->user [$length]->addChild ( "password", $password );
22         if (! $xml->asXML ( $url )) {
23             throw new Exception ( "failed,make you configfile rewriteable!" );
24         }
25         return true;
26     } catch ( Exception $e ) {
27         echo $e->getMessage ();
28     }
29 }
30 function deletefrom($username, $xml, $url = "./model/user.xml") {
31     try {
32         
33         $strname = $xml->xpath ( "/root/user/username" );
34         foreach ( $strname as $key => $value ) {
35             if (trim($value) == $username){
36                 unset ( $xml->user [$key] );            
37             }
38         }
39         
40 
41         if (! $xml->asXML ( $url )) {
42             throw new Exception ( "failed,make you configfile rewriteable!" );
43         }
44         return true;
45     } catch ( Exception $e ) {
46         echo $e->getMessage ();
47     }
48 }
49 //   insertintoxml("1", "asdf", $xml);
50 //   insertintoxml("2", "asdf", $xml);
51 //   insertintoxml("3", "asdf", $xml);
52 //   insertintoxml("4", "asdf", $xml);
53 //   insertintoxml("5", "asdf", $xml);
54 (deletefrom ( "4", $xml ));
55 
56 ?>
View Code

 

 

总结:

$xml=$strname = $xml->xpath ( "//username" ); //两反斜杠代表所有深度。

simplexml简单实用。

 

posted @ 2014-04-09 17:54  UCanBeFree  阅读(159)  评论(0编辑  收藏  举报