+ -
当前位置:首页 → 问答吧 → php4操作xml文件

php4操作xml文件

时间:2007-04-20

来源:互联网

xml文件内容
<book>
   php-xml
  <title>2007.04.13
  </title>
  <author>
   hamiguapi
  </author>
  <publisher>
  php-xml test
  </publisher>
  <date>
  2007
  </date>
  </book>
php文件内容
<?php
/*****读取节点及其内容*******/
$doc = domxml_open_file(realpath("books.xml"));
$root = $doc->document_element();
$node=$root->node_name();
echo "读取节点及其内容<br>";
echo 'Attributes of '.$node."<br>";
foreach($root->child_nodes() as $node)
{
  if ($node->node_type() == XML_ELEMENT_NODE)
   {
    echo $node->node_name().":";
     $value=$node->get_content();
     echo $value;echo "<br>";
    }
  }
    echo htmlentities($doc->dump_mem(true));
    echo "<br><br>";
   $doc->dump_file("test1.xml",true);
  /******删除节点及内容********/
  echo "删除节点及内容........<br>";
$doc = domxml_open_file(realpath("books.xml"));
$denode='title';
$root = $doc->document_element();
$node=$root->node_name();
$node_array=$root->get_elements_by_tagname('title');
$child = $root->remove_child($node_array[0]);
foreach ($node_array as $node) {
    echo 'YOU will delete '.$denode.' the value is ' .$node->get_content()."<br>";
}

$root=$doc->document_element();
$node=$root->node_name();
echo 'Attributes of '.$node."<br>";
foreach($root->child_nodes() as $node)
{
  if ($node->node_type() == XML_ELEMENT_NODE)
   {
    echo $node->node_name().":";
     $value=$node->get_content();
     echo $value;echo "<br>";
    }
  }
  echo htmlentities($doc->dump_mem(true));
    echo "<br><br>";
   $doc->dump_file("test1.xml",true);

/**********修改节点及内容**********/
  echo "修改节点及内容<br>";
$doc = domxml_open_file(realpath("books.xml"));
$addnode='time';
$root = $doc->document_element();
$node=$root->node_name();
echo 'the old Attributes of '.$node."<br>";
foreach($root->child_nodes() as $node)
{
  if ($node->node_type() == XML_ELEMENT_NODE)
   {
    echo $node->node_name().":";
     $value=$node->get_content();
     echo $value;echo "<br>";
    }
  }
  
$node = $doc->create_element("date");
$node->set_content("17:52");
$elements = $doc->get_elements_by_tagname("date");
$element = $elements[0];
$newnode = $element->replace_node($node);

echo "<br>";  
$root = $doc->document_element();
$node=$root->node_name();
echo 'the new Attributes of '.$node."<br>";
foreach($root->child_nodes() as $node)
{
  if ($node->node_type() == XML_ELEMENT_NODE)
   {
    echo $node->node_name().":";
     $value=$node->get_content();
     echo $value;echo "<br>";
    }
  }
  echo htmlentities($doc->dump_mem(true));
    echo "<br><br>";
   $doc->dump_file("test1.xml",true);
/**********添加节点及内容**********/
  echo "添加节点及内容......<br>";
$doc = domxml_open_file(realpath("books.xml"));
$addnode='time';
$root = $doc->document_element();
$node=$root->node_name();
echo 'the old Attributes of '.$node."<br>";
foreach($root->child_nodes() as $node)
{
  if ($node->node_type() == XML_ELEMENT_NODE)
   {
    echo $node->node_name().":";
     $value=$node->get_content();
     echo $value;echo "<br>";
    }
  }

$item = $doc->create_Element("time");
$item=$root->append_Child($item);

// create text node
$text = $doc->create_Text_Node("11:07");
$text=$item->append_Child($text);

echo "<br>";  
$root = $doc->document_element();
$node=$root->node_name();
echo 'the new Attributes of '.$node."<br>";
foreach($root->child_nodes() as $node)
{
  if ($node->node_type() == XML_ELEMENT_NODE)
   {
    echo $node->node_name().":";
     $value=$node->get_content();
     echo $value;echo "<br>";
    }
  }
   echo htmlentities($doc->dump_mem(true));
    echo "<br><br>";
   $doc->dump_file("test1.xml",true);
  /*******php写xml文件*******/
echo "php写xml文件";
echo "<br>";
// create doctype
$dom = domxml_new_doc("1.0");

// create root element
$root = $dom->create_Element("top");
$root=$dom->append_Child($root);

// create child element
$item = $dom->create_Element("item");
$item=$root->append_Child($item);

// create text node
$text = $dom->create_Text_Node("hamigapi");
$text=$item->append_Child($text);

// create child element
$item = $dom->create_Element("item");
$item=$root->append_Child($item);

// create another text node
$text = $dom->create_Text_Node("qqbaobao");
$text=$item->append_Child($text);

// save and display tree
echo htmlentities($dom->dump_mem(true));
$dom->dump_file("test.xml",true);
?>

作者: c3125815   发布时间: 2007-04-19

看的好晕...整理一下代码再发吧

作者: orclord   发布时间: 2007-04-19

不错,谢谢!

作者: dongdonga   发布时间: 2007-04-20

:) 希望更多的朋友给我支持与肯定,也同样希望给我提出更好的建议,多谢大家的回帖!!!

作者: c3125815   发布时间: 2007-04-20

<?php
        //Message_XML类,继承PHP5的DomDocument类
        class Message_XML extends DomDocument{
        //属性
        private $Root;
        //方法
        //构造函数
        public function __construct() {
                parent:: __construct();
        //创建或读取存储留言信息的XML文档message.xml
        if (!file_exists("message.xml")){
                $xmlstr = "<?xml verecordion='1.0' encoding='GB2312'?><message></message>";
                $this->loadXML($xmlstr);
                $this->save("message.xml");
        }
        else
                $this->load("message.xml");
}
//增加留言
public function add_message($Subject,$Content){
        $Root = $this->documentElement;
        //获取留言消息
        $AutoID =date("Ynjhis");
        $Node_AutoID= $this->createElement("autoid");
        $text= $this->createTextNode(iconv("GB2312","UTF-8",$AutoID));
        $Node_AutoID->appendChild($text);
        $Node_Subject = $this->createElement("subject");
        $text  = $this->createTextNode(iconv("GB2312","UTF-8",$Subject));
        $Node_Subject->appendChild($text);
        $Node_Content = $this->createElement("content");
        $text= $this->createTextNode(iconv("GB2312","UTF-8",$Content));
        $Node_Content->appendChild($text);
        //建立一条留言记录
        $Node_Record = $this->createElement("record");
        $Node_Record->appendChild($Node_AutoID);
        $Node_Record->appendChild($Node_Subject);
        $Node_Record->appendChild($Node_Content);
        //加入到根结点下
        $Root->appendChild($Node_Record);
        $this->save("message.xml");  
        echo "<script>alert('添加成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
}

//删除留言
public function delete_message($AutoID){
        $Root = $this->documentElement;
        //查询用户选择删除的留言记录
        $xpath = new DOMXPath($this);
        $Node_Record= $xpath->query("//record[autoid=$AutoID]");
        $Root->removeChild($Node_Record->item(0));
        $this->save("message.xml");
        echo "<script>alert('删除成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
}
               
//显示留言
public function show_message()        {
        $Root  = $this->documentElement;
        $xpath = new DOMXPath($this);
        //查询所有的留言记录
        $Node_Record = $this->getElementsByTagName("record");
        $Node_Record_Length  =$Node_Record->length;
        //循环输出其留言标题和内容信息
        for($i=0;$i<$Node_Record->length;$i++)        {
                $K=0;
                foreach ($Node_Record->item($i)->childNodes as $articles) {
                        $Field[$K]=iconv("UTF-8","GB2312",$articles->textContent);
                        $K++;
                }
                print "<tr><td bgcolor=#FFFFFF>";
                print "<b>留言标题:</b>$Field[1]<br><b>留言内容:</b><br>$Field[2]<div align=right><a href='?Action=update_message&AutoID=$Field[0]'>编辑</a> <a href='?Action=delete_message&AutoID=$Field[0]'>删除</a></div>\n";
                print "</td></tr>";
        }
}  

//修改留言
public function update_message($AutoID){
        $Root  = $this->documentElement;
        $xpath = new DOMXPath($this);
        $Node_Record = $xpath->query("//record[autoid=$AutoID]");
        $K=0;
        foreach ($Node_Record->item(0)->childNodes as $articles) {
                $Field[$K]=iconv("UTF-8","GB2312",$articles->textContent);
                $K++;
        }
        print "<form method='post' action='?Action=save_message&AutoID=$AutoID'>";
        print "<tr><td>留言标题:<input type=text name='Subject' value='$Field[1]' size=20><br/></td></tr>";
        print "<tr><td valign=top>留言内容:<textarea name='Content' cols=50 rows=5>$Field[2]</textarea></td></tr>";
        print "<tr><td><input type='submit' value='添加留言'></td></tr></form>";
}
          
//保存留言
public function save_message($AutoID,$Subject,$Content)        {
        $Root  = $this->documentElement;
        //查询待修改的记录
        $xpath = new DOMXPath($this);
        $Node_Record  = $xpath->query("//record[autoid=$AutoID]");
        $Replace[0]=$AutoID;
        $Replace[1]=$Subject;
        $Replace[2]=$Content;
                $K=0;
                //修改
                foreach ($Node_Record->item(0)->childNodes as $articles) {
                        $Node_newText = $this->createTextNode(iconv("GB2312","UTF-8",$Replace[$K]));  
                        $articles->replaceChild($Node_newText,$articles->lastChild);
                        $K++;
                }
                echo "<script>alert('修改成功');location.href='".$_SERVER['PHP_SELF']."'</script>";
                $this->save("message.xml");
        }
                       
        //上传留言信息
        public function post_message(){
                print "<form method='post' action='?Action=add_message'>";
                print "<tr><td>留言标题:<input type=text name='Subject' size=20><br/></td></tr>";
                print "<tr><td valign=top>留言内容:<textarea name='Content' cols=50 rows=5></textarea></td></tr>";
                print "<tr><td><input type='submit' value='添加留言'></td></tr></form>";
        }
        }//class end
        ?>

<html>                                                                                                                           
        <head>                                                                                                                        
                <title>PHP+XML留言板</title>                                                                                                
                <style>td,body{font-size:14        px}</style>                                                                                      
        </head>                                                                                                                        
        <body>                                                                                                                        
        <table width=100% height=100% border=1 align=center cellpadding=3 cellspacing=1 bgcolor=silver>                                
        <tr>                                                                                                                           
                <td height=20 bgcolor=silver><div align=center><strong>PHP+XML留言板</strong></div></td>                                    
                </tr>                                                                                                                        
                <tr>                                                                                                                        
                        <td height=20 bgcolor=white>〖<a href=?Action=post_message>发表留言</a>〗〖<a href=?Action=show_message>显示留言</a>〗</td>
                </tr>  
                <?php                                                                                                                     
                                                                                                                                       
                 //使用Message_XML类完成留言板                                                                                                
                $HawkXML = new Message_XML;   
                $Action ="";      
                if(isset($_GET['Action']))     
                        $Action = $_GET['Action'];                                                                                                   
                  switch($Action){                                                                                                           
                        case "show_message":        //查看                                                                                               
                                $HawkXML->show_message();                                                                                                
                                break;                                                                                                                  
                        case "post_message"://提交                                                                                                
                                $HawkXML->post_message();                                                                                                
                                break;                                                                                                                  
                        case "add_message"://增加                                                                                                  
                                $HawkXML->add_message($_POST['Subject'],$_POST['Content']);                                                              
                                break;                                                                                                                  
                          case "delete_message"://删除                                                                                             
                                $HawkXML->delete_message($_GET["AutoID"]);                                                                                       
                                break;                                                                                                                  
                        case "update_message"://修改                                                                                               
                                $HawkXML->update_message($_GET["AutoID"]);                                                                                       
                                break;                                                                                                                  
                        case "save_message"://保存                                                                                                
                                $HawkXML->save_message($_GET["AutoID"],$_POST['Subject'],$_POST['Content']);                                                     
                                break;                                                                                                                  
                        default://默认查看                                                                                                         
                                $HawkXML->show_message();                                                                                                
                                break;                                                                                                                  
          }                                                                                                                           
        ?>
                        </table>                                                                                                                  
                </body>                                                                                                                     
        </html>

作者: shenyuxiang2008   发布时间: 2007-04-25

不错的!!!!!!!11

作者: swordxj   发布时间: 2007-05-19