用 PHP5 輕鬆解析 XML

NO IMAGE

用 sax 方式的時候,要自己構建3個函式,而且要直接用這三的函式來返回資料,要求較強的邏輯。在處理不同結構的 xml 的時候,還要重新進行構造這三個函式,麻煩!

用 dom 方式,倒是好些,但是他把每個節點都看作是一個 node,,操作起來要寫好多的程式碼,麻煩!

網上有好多的開源的 xml 解析的類庫,以前看過幾個,但是心裡總是覺得不踏實,感覺總是跟在別人的屁股後面。

這幾天在搞 Java,挺累的,所以決定換換腦袋,寫點 PHP 程式碼,為了防止以後 XML 解析過程再令我犯難,就花了一天的時間寫了下面一個 XML 解析的類,於是就有了下面的東西。

實現方式是通過包裝“sax方式的解析結果”來實現的。總的來說,對於我個人來說挺實用的,效能也還可以,基本上可以完成大多數的處理要求。

功能:
1\ 對基本的 XML 檔案的節點進行 查詢 / 新增 / 修改 / 刪除 工作。
2\ 匯出 XML 檔案的所有資料到一個陣列裡面。
3\ 整個設計採用了 OO 方式,在操作結果集的時候,使用方法類似於 dom

缺點:
1\ 每個節點最好都帶有一個id(看後面的例子),每個“節點名字”=“節點的標籤_節點的id”,如果這個 id 值沒有設定,程式將自動給他產生一個 id,這個 id 就是這個節點在他的上級節點中的位置編號,從 0 開始。
2\ 查詢某個節點的時候可以通過用“|”符號連線“節點名字”來進行。這些“節點名字”都是按順序寫好的上級節點的名字。

使用說明:
執行下面的例子,在執行結果頁面上可以看到函式的使用說明

程式碼是通過 PHP5 來實現的,在 PHP4 中無法正常執行。

由於剛剛寫完,所以沒有整理文件,下面的例子演示的只是一部分的功能,程式碼不是很難,要是想知道更多的功能,可以研究研究原始碼。

目錄結構:

test.php
test.xml
xml / SimpleDocumentBase.php
xml / SimpleDocumentNode.php
xml / SimpleDocumentRoot.php
xml / SimpleDocumentParser.php

 
檔案:test.xml

<?xml version=”1.0″ encoding=”GB2312″?>
<shop>
 <name>華聯</name>
 <address>北京長安街-9999號</address>
 <desc>連鎖超市</desc>
 <cat id=”food”>
  <goods id=”food11″>
   <name>food11</name>
   <price>12.90</price>
  </goods>
  <goods id=”food12″>
   <name>food12</name>
   <price>22.10</price>
   <desc creator=”hahawen”>好東西推薦</desc>
  </goods>
 </cat>
 <cat>
  <goods id=”tel21″>
   <name>tel21</name>
   <price>1290</price>
  </goods>
 </cat>
 <cat id=”coat”>
  <goods id=”coat31″>
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id=”coat32″>
   <name>coat32</name>
   <price>45</price>
  </goods>
 </cat>
 <special id=”hot”>
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
 </special>
</shop>

檔案:test.php

<?php
require_once “xml/SimpleDocumentParser.php”;
require_once “xml/SimpleDocumentBase.php”;
require_once “xml/SimpleDocumentRoot.php”;
require_once “xml/SimpleDocumentNode.php”;

$test = new SimpleDocumentParser();
$test->parse(“test.xml”);
$dom = $test->getSimpleDocument();

echo “<pre>”;
echo “<hr><font color=red>”;
echo “下面是通過函式getSaveData()返回的整個xml資料的陣列”;
echo “</font><hr>”;
print_r($dom->getSaveData());

echo “<hr><font color=red>”;
echo “下面是通過setValue()函式,給給根節點新增資訊,新增後顯示出結果xml檔案的內容”;
echo “</font><hr>”;
$dom->setValue(“telphone”, “123456789”);
echo htmlspecialchars($dom->getSaveXml());

echo “<hr><font color=red>”;
echo “下面是通過getNode()函式,返回某一個分類下的所有商品的資訊”;
echo “</font><hr>”;
$obj = $dom->getNode(“cat_food”);
$nodeList = $obj->getNode();
foreach($nodeList as $node){
    $data = $node->getValue();
    echo “<font color=red>商品名:”.$data[name].”</font><br>”;
    print_R($data);
    print_R($node->getAttribute());
}

echo “<hr><font color=red>”;
echo “下面是通過findNodeByPath()函式,返回某一商品的資訊”;
echo “</font><hr>”;
$obj = $dom->findNodeByPath(“cat_food|goods_food11”);
if(!is_object($obj)){
    echo “該商品不存在”;
}else{
    $data = $obj->getValue();
    echo “<font color=red>商品名:”.$data[name].”</font><br>”;
    print_R($data);
    print_R($obj->getAttribute());
}

echo “<hr><font color=red>”;
echo “下面是通過setValue()函式,給商品\”food11\”新增屬性, 然後顯示新增後的結果”;
echo “</font><hr>”;
$obj = $dom->findNodeByPath(“cat_food|goods_food11”);
$obj->setValue(“leaveword”, array(“value”=>”這個商品不錯”, “attrs”=>array(“author”=>”hahawen”, “date”=>date(‘Y-m-d’))));
echo htmlspecialchars($dom->getSaveXml());

echo “<hr><font color=red>”;
echo “下面是通過removeValue()/removeAttribute()函式,給商品\”food11\”改變和刪除屬性, 然後顯示操作後的結果”;
echo “</font><hr>”;
$obj = $dom->findNodeByPath(“cat_food|goods_food12”);
$obj->setValue(“name”, “new food12”);
$obj->removeValue(“desc”);
echo htmlspecialchars($dom->getSaveXml());

echo “<hr><font color=red>”;
echo “下面是通過createNode()函式,新增商品, 然後顯示新增後的結果”;
echo “</font><hr>”;
$obj = $dom->findNodeByPath(“cat_food”);
$newObj = $obj->createNode(“goods”, array(“id”=>”food13”));
$newObj->setValue(“name”, “food13”);
$newObj->setValue(“price”, 100);
echo htmlspecialchars($dom->getSaveXml());

echo “<hr><font color=red>”;
echo “下面是通過removeNode()函式,刪除商品, 然後顯示刪除後的結果”;
echo “</font><hr>”;
$obj = $dom->findNodeByPath(“cat_food”);
$obj->removeNode(“goods_food12”);
echo htmlspecialchars($dom->getSaveXml());


?>
 
檔案:SimpleDocumentParser.php
 

<?php
/**
 *================================================
 * 
 * @author     hahawen(大齡青年) 

 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group 
 *
 *================================================
 */
 /**
 * class SimpleDocumentParser
 * use SAX parse xml file, and build SimpleDocumentObject 
 * all this pachage’s is work for xml file, and method is action as DOM.
 *
 * @package SmartWeb.common.xml
 * @version 1.0
 */
 class SimpleDocumentParser
 {

     private $domRootObject = null;
     private $currentNO = null;
     private $currentName  = null;
     private $currentValue = null;
     private $currentAttribute = null;

     public
     function getSimpleDocument()
     {
         return $this->domRootObject;
     }

     public function parse($file)
     {
         $xmlParser = xml_parser_create();
         xml_parser_set_option($xmlParser,XML_OPTION_CASE_FOLDING,
         0);
         xml_parser_set_option($xmlParser,XML_OPTION_SKIP_WHITE, 1);
         xml_parser_set_option($xmlParser,
         XML_OPTION_TARGET_ENCODING, ‘UTF-8’);
         xml_set_object($xmlParser, $this);

         xml_set_element_handler($xmlParser, “startElement”, “endElement”);
         xml_set_character_data_handler($xmlParser,
         “characterData”);

         if (!xml_parse($xmlParser, file_get_contents($file)))
         die(sprintf(“XML error: %s at line %d”, xml_error_string(xml_get_error_code($xmlParser)),
         xml_get_current_line_number($xmlParser)));

         xml_parser_free($xmlParser);
     }
     private function startElement($parser, $name, $attrs)
     {
         $this->currentName = $name;
         $this->currentAttribute = $attrs;
         if($this->currentNO == null)
         {
             $this->domRootObject = new SimpleDocumentRoot($name);

             $this->currentNO = $this->domRootObject;
         }
         else
         {
             $this->currentNO = $this->currentNO->createNode($name, $attrs);

         }
     }

     private function endElement($parser, $name)
     {
         if($this->currentName==$name)

         {
             $tag = $this->currentNO->getSeq();
             $this->currentNO  = $this->currentNO->getPNodeObject();
             if($this->currentAttribute!=null && sizeof($this->currentAttribute)>0)
             $this->currentNO->setValue($name, array(‘value’=>$this->currentValue, ‘attrs’=>$this->currentAttribute));
             else
             $this->currentNO->setValue($name, $this->currentValue);

             $this->currentNO->removeNode($tag);
         }
         else
         {
             $this->currentNO = (is_a($this->currentNO, ‘SimpleDocumentRoot’))?   null:
             $this->currentNO->getPNodeObject();
         }
     }

     private function characterData($parser,  $data)
     {
         $this->currentValue = iconv(‘UTF-8’, ‘GB2312’, $data);
     }


     function __destruct()
     {
         unset($this->domRootObject);
     }

 }
?>

 
檔案:SimpleDocumentBase.php
 

<?php
/**
 *=================================================
 *
 * @author     hahawen(大齡青年) 
 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group
 *
 *=================================================
 */
 /**
 * abstract class SimpleDocumentBase
 * base class for xml file parse
 * all this pachage’s is work for xml file, and method is action as DOM.
 *
 * 1\ add/update/remove data of xml file.
 * 2\ explode data to array.
 * 3\ rebuild xml file
 *
 * @package SmartWeb.common.xml
 * @abstract
 * @version 1.0
 */
 abstract class SimpleDocumentBase
 {

     private $nodeTag = null;
     private $attributes = array();
     private $values =
     array();

     private $nodes = array();
     function __construct($nodeTag)
     {
         $this->nodeTag = $nodeTag;
     }

     public function getNodeTag()
     {
         return $this->nodeTag;
     }

     public function setValues($values){
         $this->values = $values;
     }

     public function setValue($name, $value)
     {
         $this->values[$name] = $value;
     }

     public function getValue($name=null)
     {
         return $name==null?
         $this->values: $this->values[$name];
     }

 
     public function removeValue($name)
     {
         unset($this->values[“$name”]);
     }

     public function setAttributes($attributes){
         $this->attributes = $attributes;
     }

     public function setAttribute($name, $value)
     {
         $this->attributes[$name] = $value;
     }

     public function getAttribute($name=null)
     {
         return $name==null? $this->attributes:
         $this->attributes[$name];
     }

     public function removeAttribute($name)
     {
         unset($this->attributes[“$name”]);
     }

     public function getNodesSize()
     {
         return sizeof($this->nodes);
     }

     protected function setNode($name, $nodeId)
     {
         $this->nodes[$name]
         = $nodeId;
     }

     public abstract function createNode($name, $attributes);
     public abstract function removeNode($name);
     public abstract function getNode($name=null);
     protected function getNodeId($name=null)
     {
         return $name==null? $this->nodes: $this->nodes[$name];
     }

     protected function createNodeByName($rootNodeObj, $name, $attributes, $pId)
     {
         $tmpObject = $rootNodeObj->createNodeObject($pId, $name, $attributes);
         $key = isset($attributes[id])?
         $name.’_’.$attributes[id]: $name.’_’.$this->getNodesSize();
         $this->setNode($key, $tmpObject->getSeq());
         return $tmpObject;
     }

     protected function removeNodeByName($rootNodeObj, $name)
     {
         $rootNodeObj->removeNodeById($this->getNodeId($name));
         if(sizeof($this->nodes)==1)
         $this->nodes = array();
         else
         unset($this->nodes[$name]);
     }

     protected function getNodeByName($rootNodeObj, $name=null)
     {
         if($name==null)
         {
             $tmpList = array();
             $tmpIds = $this->getNodeId();
             foreach($tmpIds as $key=>$id)
             $tmpList[$key] = $rootNodeObj->getNodeById($id);
             return $tmpList;
         }
         else
         {
             $id = $this->getNodeId($name);
             if($id===null)
             {
                 $tmpIds = $this->getNodeId();

                 foreach($tmpIds as $tkey=>$tid)
                 {
                     if(strpos($key, $name)==0)
                     {
                         $id = $tid;
                         break;
                     }
                 }
             }
             return $rootNodeObj->getNodeById($id);
         }
     }

     public function findNodeByPath($path)
     {
         $pos = strpos($path, ‘|’);
         if($pos<=0)
         {
             return $this->getNode($path);


         }
         else
         {

             $tmpObj = $this->getNode(substr($path, 0,
             $pos));

             return is_object($tmpObj)?
             $tmpObj->findNodeByPath(substr($path,
             $pos 1)):
             null;
         }
     }

     public function getSaveData()
     {
         $data = $this->values;
         if(sizeof($this->attributes)>0)

         $data[attrs] = $this->attributes;
         $nodeList = $this->getNode();


         if($nodeList==null)
         return $data;
         foreach($nodeList as $key=>$node)
         {
             $data[$key] = $node->getSaveData();
         }

         return $data;
     }


     public function getSaveXml($level=0)
     {

         $prefixSpace
         = str_pad(“”,
         $level, “\t”);
         $str = “$prefixSpace<$this->nodeTag”;

 
         foreach($this->attributes as $key=>$value)
         $str .= ” $key=\”$value\””;

         $str .= “>\r\n”;

         foreach($this->values as $key=>$value){
             if(is_array($value))
             {
                 $str .= “$prefixSpace\t<$key”;

                 foreach($value[attrs] as $attkey=>$attvalue)
                 $str .= ” $attkey=\”$attvalue\””;
                 $tmpStr = $value[value];

             }
             else

             {
                 $str .= “$prefixSpace\t<$key”;

                 $tmpStr = $value;
             }
             $tmpStr = trim(trim($tmpStr, “\r\n”));

             $str .= ($tmpStr===null || $tmpStr===””)? ” />\r\n”: “>$tmpStr</$key>\r\n”;
         }
         foreach($this->getNode() as $node)
         $str .= $node->getSaveXml($level 1).”\r\n”;


         $str .= “$prefixSpace</$this->nodeTag>”;
         return $str;
     }

 
     function __destruct()
     {
         unset($this->nodes, $this->attributes, $this->values);

     }
 }
?>

 
檔案:SimpleDocumentRoot.php
 

<?php
/**
 *==============================================
 *
 * @author     hahawen(大齡青年) 
 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group
 *
 *==============================================
 */
 /**
 * class SimpleDocumentRoot
 * xml root class, include values/attributes/subnodes.
 * all this pachage’s is work for xml file, and method is action as DOM.
 *
 * @package SmartWeb.common.xml
 * @version 1.0
 */

class SimpleDocumentRoot extends SimpleDocumentBase
{
    private $prefixStr = ”;
    private $nodeLists = array();

    function __construct($nodeTag)
    {
        parent::__construct($nodeTag);
    }

    public function createNodeObject($pNodeId, $name, $attributes)
    {
        $seq = sizeof($this->nodeLists);
        $tmpObject = new SimpleDocumentNode($this,
        $pNodeId, $name, $seq);
        $tmpObject->setAttributes($attributes);

        $this->nodeLists[$seq] = $tmpObject;
        return $tmpObject;
    }

    public function removeNodeById($id)
    {
        if(sizeof($this->nodeLists)==1)
        $this->nodeLists = array();
        else
        unset($this->nodeLists[$id]);
    }

    public function getNodeById($id)
    {
        return $this->nodeLists[$id];
    }

    public function createNode($name, $attributes)
    {
        return $this->createNodeByName($this, $name, $attributes, -1);
    }

    public function removeNode($name)
    {
        return $this->removeNodeByName($this, $name);
    }

    public function getNode($name=null)
    {
        return $this->getNodeByName($this, $name);
    }

    public function getSaveXml()
    {
        $prefixSpace = “”;
        $str = $this->prefixStr.”\r\n”;
        return $str.parent::getSaveXml(0);
    }
}
?>

 
檔案:SimpleDocumentNode.php
 
<?php
/**
 *===============================================
 *
 * @author     hahawen(大齡青年) 
 * @since      2004-12-04
 * @copyright  Copyright (c) 2004, NxCoder Group
 *
 *===============================================
 */
 /**
 * class SimpleDocumentNode
 * xml Node class, include values/attributes/subnodes.
 * all this pachage’s is work for xml file, and method is action as DOM.
 *
 * @package SmartWeb.common.xml
 * @version 1.0
 */
 class SimpleDocumentNode extends SimpleDocumentBase
 {
     private $seq = null;
     private $rootObject = null;
     private $pNodeId = null;

     function __construct($rootObject, $pNodeId, $nodeTag, $seq)
     {
         parent::__construct($nodeTag);
         $this->rootObject = $rootObject;
         $this->pNodeId = $pNodeId;
         $this->seq = $seq;
     }

     public function getPNodeObject()
     {
         return ($this->pNodeId==-1)?
         $this->rootObject:
         $this->rootObject->getNodeById($this->pNodeId);
     }

     public function getSeq(){
         return $this->seq;
     }

     public function createNode($name, $attributes)
     {
         return $this->createNodeByName($this->rootObject,
         $name, $attributes,
         $this->getSeq());
     }

     public function removeNode($name)
     {
         return $this->removeNodeByName($this->rootObject, $name);
     }


     public function getNode($name=null)
     {
         return $this->getNodeByName($this->rootObject,
         $name);
     }
 }
?>

 

下面是例子執行對結果
 


下面是通過函式getSaveData()返回的整個xml資料的陣列


Array
(
    [name] => 華聯
    [address] => 北京長安街-9999號
    [desc] => 連鎖超市
    [cat_food] => Array
        (
            [attrs] => Array
                (
                    [id] => food
                )

            [goods_food11] => Array
                (
                    [name] => food11
                    [price] => 12.90
                    [attrs] => Array
                        (
                            [id] => food11
                        )

                )

            [goods_food12] => Array
                (
                    [name] => food12
                    [price] => 22.10
                    [desc] => Array
                        (
                            [value] => 好東西推薦
                            [attrs] => Array
                                (
                                    [creator] => hahawen
                                )

                        )

                    [attrs] => Array
                        (
                            [id] => food12
                        )

                )

        )

    [cat_1] => Array
        (
            [goods_tel21] => Array
                (
                    [name] => tel21
                    [price] => 1290
                    [attrs] => Array
                        (
                            [id] => tel21
                        )

                )

        )

    [cat_coat] => Array
        (
            [attrs] => Array
                (
                    [id] => coat
                )

            [goods_coat31] => Array
                (
                    [name] => coat31
                    [price] => 112
                    [attrs] => Array
                        (
                            [id] => coat31
                        )

                )

            [goods_coat32] => Array
                (
                    [name] => coat32
                    [price] => 45
                    [attrs] => Array
                        (
                            [id] => coat32
                        )

                )

        )

    [special_hot] => Array
        (
            [attrs] => Array
                (
                    [id] => hot
                )

            [goods_0] => Array
                (
                    [name] => hot41
                    [price] => 99
                )

        )

)


下面是通過setValue()函式,給給根節點新增資訊,新增後顯示出結果xml檔案的內容


<?xml version=”1.0″ encoding=”GB2312″ ?>
<shop>
<name>華聯</name>
<address>北京長安街-9999號</address>
<desc>連鎖超市</desc>
<telphone>123456789</telphone>
<cat id=”food”>
  <goods id=”food11″>
   <name>food11</name>
   <price>12.90</price>
  </goods>
  <goods id=”food12″>
   <name>food12</name>
   <price>22.10</price>
   <desc creator=”hahawen”>好東西推薦</desc>
  </goods>
</cat>
<cat>
  <goods id=”tel21″>
   <name>tel21</name>
   <price>1290</price>
  </goods>
</cat>
<cat id=”coat”>
  <goods id=”coat31″>
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id=”coat32″>
   <name>coat32</name>
   <price>45</price>
  </goods>
</cat>
<special id=”hot”>
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
</special>
</shop>


下面是通過getNode()函式,返回某一個分類下的所有商品的資訊


商品名:food11
Array
(
    [name] => food11
    [price] => 12.90
)
Array
(
    [id] => food11
)
商品名:food12
Array
(
    [name] => food12
    [price] => 22.10
    [desc] => Array
        (
            [value] => 好東西推薦
            [attrs] => Array
                (
                    [creator] => hahawen
                )

        )

)
Array
(
    [id] => food12
)


下面是通過findNodeByPath()函式,返回某一商品的資訊


商品名:food11
Array
(
    [name] => food11
    [price] => 12.90
)
Array
(
    [id] => food11
)


下面是通過setValue()函式,給商品”food11″新增屬性, 然後顯示新增後的結果


<?xml version=”1.0″ encoding=”GB2312″ ?>
<shop>
<name>華聯</name>
<address>北京長安街-9999號</address>
<desc>連鎖超市</desc>
<telphone>123456789</telphone>
<cat id=”food”>
  <goods id=”food11″>
   <name>food11</name>
   <price>12.90</price>
   <leaveword author=”hahawen” date=”2004-12-05″>這個商品不錯</leaveword>
  </goods>
  <goods id=”food12″>
   <name>food12</name>
   <price>22.10</price>
   <desc creator=”hahawen”>好東西推薦</desc>
  </goods>
</cat>
<cat>
  <goods id=”tel21″>
   <name>tel21</name>
   <price>1290</price>
  </goods>
</cat>
<cat id=”coat”>
  <goods id=”coat31″>
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id=”coat32″>
   <name>coat32</name>
   <price>45</price>
  </goods>
</cat>
<special id=”hot”>
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
</special>
</shop>


下面是通過removeValue()/removeAttribute()函式,給商品”food11″改變和刪除屬性, 然後顯示操作後的結果


<?xml version=”1.0″ encoding=”GB2312″ ?>
<shop>
<name>華聯</name>
<address>北京長安街-9999號</address>
<desc>連鎖超市</desc>
<telphone>123456789</telphone>
<cat id=”food”>
  <goods id=”food11″>
   <name>food11</name>
   <price>12.90</price>
   <leaveword author=”hahawen” date=”2004-12-05″>這個商品不錯</leaveword>
  </goods>
  <goods id=”food12″>
   <name>new food12</name>
   <price>22.10</price>
  </goods>
</cat>
<cat>
  <goods id=”tel21″>
   <name>tel21</name>
   <price>1290</price>
  </goods>
</cat>
<cat id=”coat”>
  <goods id=”coat31″>
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id=”coat32″>
   <name>coat32</name>
   <price>45</price>
  </goods>
</cat>
<special id=”hot”>
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
</special>
</shop>


下面是通過createNode()函式,新增商品, 然後顯示新增後的結果


<?xml version=”1.0″ encoding=”GB2312″ ?>
<shop>
<name>華聯</name>
<address>北京長安街-9999號</address>
<desc>連鎖超市</desc>
<telphone>123456789</telphone>
<cat id=”food”>
  <goods id=”food11″>
   <name>food11</name>
   <price>12.90</price>
   <leaveword author=”hahawen” date=”2004-12-05″>這個商品不錯</leaveword>
  </goods>
  <goods id=”food12″>
   <name>new food12</name>
   <price>22.10</price>
  </goods>
  <goods id=”food13″>
   <name>food13</name>
   <price>100</price>
  </goods>
</cat>
<cat>
  <goods id=”tel21″>
   <name>tel21</name>
   <price>1290</price>
  </goods>
</cat>
<cat id=”coat”>
  <goods id=”coat31″>
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id=”coat32″>
   <name>coat32</name>
   <price>45</price>
  </goods>
</cat>
<special id=”hot”>
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
</special>
</shop>


下面是通過removeNode()函式,刪除商品, 然後顯示刪除後的結果


<?xml version=”1.0″ encoding=”GB2312″ ?>
<shop>
<name>華聯</name>
<address>北京長安街-9999號</address>
<desc>連鎖超市</desc>
<telphone>123456789</telphone>
<cat id=”food”>
  <goods id=”food11″>
   <name>food11</name>
   <price>12.90</price>
   <leaveword author=”hahawen” date=”2004-12-05″>這個商品不錯</leaveword>
  </goods>
  <goods id=”food13″>
   <name>food13</name>
   <price>100</price>
  </goods>
</cat>
<cat>
  <goods id=”tel21″>
   <name>tel21</name>
   <price>1290</price>
  </goods>
</cat>
<cat id=”coat”>
  <goods id=”coat31″>
   <name>coat31</name>
   <price>112</price>
  </goods>
  <goods id=”coat32″>
   <name>coat32</name>
   <price>45</price>
  </goods>
</cat>
<special id=”hot”>
  <goods>
   <name>hot41</name>
   <price>99</price>
  </goods>
</special>
</shop>

您可能感興趣的文章:

PHP5中使用DOM控制XML實現程式碼php5 and xml示例PHP5的XML新特性PHP XML操作的各種方法解析(比較詳細)php的SimpleXML方法讀寫XML介面檔案例項解析php解析xml方法例項詳解PHP用SAX解析XML的實現程式碼與問題分析PHP 以POST方式提交XML、獲取XML,解析XML詳解及例項使用PHP DOM-XML建立和解析XML檔案解析php DOMElement 操作xml 文件的實現程式碼php解析xml 的四種簡單方法(附例項)PHP XML資料解析程式碼PHP4和PHP5版本下解析XML文件的操作方法例項分析