Using PHP5 to easily parse xml_php skills

Source: Internet
Author: User

When you use sax, you have to build 3 functions yourself, and you want to use the three function directly to return the data, requiring strong logic. In the process of dealing with different structure of XML, but also to reconstruct these three functions, trouble!

It's better to use DOM, but he sees every node as nodes, and it takes a lot of code to do it, trouble!

There are a lot of open source XML parsing class library, had seen a few before, but in the mind always feel not steadfast, feeling always follow someone else's butt behind.

These days in the Java, very tired, so decided to change the head, write some PHP code, in order to prevent the XML parsing process again to make me puzzled, it took a day to write the following XML parsing class, so there is the following things.

The implementation is done by wrapping "analytic results of Sax methods". In general, for me it is very practical, performance is also possible, basically can complete most of the processing requirements.

Function:
1\ Query/Add/modify/delete work on the basic XML file nodes.
2\ exports all the data from an XML file into an array.
3\ the entire design takes an OO approach, using the method similar to Dom when manipulating the result set

Disadvantages:
1\ each node preferably with an ID (see the following example), each "node name" = "Node's label _ Node ID", if this ID value is not set, the program will automatically give him an ID, this ID is the node in his ancestor node in the number of positions, starting from 0.
2\ to query a node by using the "|" Symbolic Connection "node name" to proceed. These "node names" are the names of the ancestor nodes that are written in order.

Instructions for use:
Run the following example, and you can see how the function is used on the execution results page.

The code is implemented through PHP5 and cannot function properly in PHP4.

Since you have just finished, so there is no documentation, the following example shows only part of the function, the code is not very difficult, if you want to know more features, you can study the source code.

Directory structure:

test.php
Test.xml
xml/simpledocumentbase.php
xml/simpledocumentnode.php
xml/simpledocumentroot.php
xml/simpledocumentparser.php
File: Test.xml

<?xml version= "1.0" encoding= "GB2312"?>


<shop>


<name> Hualian </name>


<address> Beijing Changan Street-No. No. 9999 </address>


<desc> supermarket chain </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" > Good Stuff Recommendation </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>

File: 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 "echo "Below is an array of the entire XML data returned through the function getsavedata ()";
echo "</font>Print_r ($dom->getsavedata ());
echo "echo "Below is through the SetValue () function, to add information to the root node, add to show the content of the result XML file";
echo "</font>$dom->setvalue ("Telphone", "123456789");
Echo Htmlspecialchars ($dom->getsavexml ());
echo "echo "Below is a getnode () function that returns information for all items under a category";
echo "</font>$obj = $dom->getnode ("Cat_food");
$nodeList = $obj->getnode ();
foreach ($nodeList as $node) {
$data = $node->getvalue ();
echo "<font color=red> Product Name:". $data [name]. " </font><br> ";
Print_r ($data);
Print_r ($node->getattribute ());
}
echo "echo "Below is the Findnodebypath () function that returns information about a product";
echo "</font>$obj = $dom->findnodebypath ("Cat_food|goods_food11");
if (!is_object ($obj)) {
echo "The product does not exist";
}else{
$data = $obj->getvalue ();
echo "<font color=red> Product Name:". $data [name]. " </font><br> ";
Print_r ($data);
Print_r ($obj->getattribute ());
}
echo "echo "Below is through the SetValue () function, to the product \" food11\ "Add attributes, and then show the added results";
echo "</font>$obj = $dom->findnodebypath ("Cat_food|goods_food11");
$obj->setvalue ("Leaveword", Array ("Value" => "This product is good", "Attrs" =>array ("author" => "Hahawen", "date" => Date (' y-m-d ')));
Echo Htmlspecialchars ($dom->getsavexml ());
echo "echo "Below is through the RemoveValue ()/removeattribute () function, to the product \" food11\ "To change and delete attributes, and then show the results after the operation";
echo "</font>$obj = $dom->findnodebypath ("cat_food|goods_food12");
$obj->setvalue ("name", "New Food12");
$obj->removevalue ("desc");
Echo Htmlspecialchars ($dom->getsavexml ());
echo "echo "Below is through the CreateNode () function, add the product, and then show the added result";
echo "</font>$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 "echo "Below is through the Removenode () function, delete the product, and then show the deleted result";
echo "</font>$obj = $dom->findnodebypath ("Cat_food");
$obj->removenode ("goods_food12");
Echo Htmlspecialchars ($dom->getsavexml ());

?>
File: simpledocumentparser.php
<?php
/**
*================================================
*
* @author Hahawen (older youth)
* @since 2004-12-04
* @copyright Copyright (c), Nxcoder Group
*
*================================================
*/
/**
* Class Simpledocumentparser
* Use SAX parse XML file, and build Simpledocumentobject
* All this pachage's is work to XML file, and 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);
}
}
?>
File: simpledocumentbase.php
<?php
/**
 *=================================================
 *
 * @author      Hahawen (older youth)  
 * @since        2004-12-04
 * @copyright   Copyright (c), Nxcoder Group
 *
 *================ =================================
 */
 /**
 * abstract class Simpledocumentbase
  * Base class FOR XML file Parse
 * All this pachage's is work to XML file, and is action as DOM.
&nbs p;*
 * 1\ add/update/remove Data of XML file. The
 * 2\ explode data to array.
 * 3\ rebuild XML file
 *
 * @package SmartWeb.common.xml
 * @abstract
 * @ve Rsion 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-&gt;getnodeid ();


foreach ($tmpIds as $key =&gt; $id)


$tmpList [$key] = $ROOTNODEOBJ-&gt;getnodebyid ($id);


return $tmpList;


}


Else


{


$id = $this-&gt;getnodeid ($name);


if ($id ===null)


{


$tmpIds = $this-&gt;getnodeid ();
foreach ($tmpIds as $tkey =&gt; $tid)


{


if (Strpos ($key, $name) ==0)


{


$id = $tid;


Break


}


}


}


Return $ROOTNODEOBJ-&gt;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);
}
}
?>
File: simpledocumentroot.php
<?php
/**
*==============================================
*
* @author Hahawen (older youth)
* @since 2004-12-04
* @copyright Copyright (c), Nxcoder Group
*
*==============================================
*/
/**
* Class Simpledocumentroot
* XML root class, include Values/attributes/subnodes.
* All this pachage's is work to XML file, and 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);
}
}
?>
File: simpledocumentnode.php
<?php
/**
*===============================================
*
* @author Hahawen (older youth)
* @since 2004-12-04
* @copyright Copyright (c), Nxcoder Group
*
*===============================================
*/
/**
* Class Simpledocumentnode
* XML Node class, include Values/attributes/subnodes.
* All this pachage's is work to XML file, and 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);
}
}
?>
Here is an example of running the result
The following is an array of the entire XML data returned through the function getsavedata () Array


(


[Name] =&gt; Hualian


[Address] =&gt; Beijing Changan Street-No. No. 9999


[Desc] =&gt; supermarket chain


[Cat_food] =&gt; Array


(


[Attrs] =&gt; Array


(


[ID] =&gt; Food


)





[Goods_food11] =&gt; Array


(


[Name] =&gt; food11


[Price] =&gt; 12.90


[Attrs] =&gt; Array


(


[ID] =&gt; FOOD11


)





)





[Goods_food12] =&gt; Array


(


[Name] =&gt; food12


[Price] =&gt; 22.10


[desc] =&gt; Array


(


[value] =&gt; good things recommend


[Attrs] =&gt; Array


(


[Creator] =&gt; Hahawen


)





)





[Attrs] =&gt; Array


(


[ID] =&gt; food12


)





)





)





[Cat_1] =&gt; Array


(


[Goods_tel21] =&gt; Array


(


[Name] =&gt; tel21


[Price] =&gt; 1290


[Attrs] =&gt; Array


(


[ID] =&gt; tel21


)





)





)





[Cat_coat] =&gt; Array


(


[Attrs] =&gt; Array


(


[ID] =&gt; coat


)





[Goods_coat31] =&gt; Array


(


[Name] =&gt; coat31


[Price] =&gt; 112


[Attrs] =&gt; Array


(


[ID] =&gt; coat31


)





)





[Goods_coat32] =&gt; Array


(


[Name] =&gt; Coat32


[Price] =&gt; 45


[Attrs] =&gt; Array


(


[ID] =&gt; Coat32


)





)





)





[Special_hot] =&gt; Array


(


[Attrs] =&gt; Array


(


[ID] =&gt; Hot


)





[Goods_0] =&gt; Array


(


[Name] =&gt; hot41


[Price] =&gt; 99


)





)





)


The following is a SetValue () function that adds information to the root node and then displays the contents of the resulting XML file. &lt;?xml version= "1.0" encoding= "GB2312"?&gt;


&lt;shop&gt;


&lt;name&gt; Hualian &lt;/name&gt;


&lt;address&gt; Beijing Changan Street-No. No. 9999 &lt;/address&gt;


&lt;desc&gt; supermarket chain &lt;/desc&gt;


&lt;telphone&gt;123456789&lt;/telphone&gt;


&lt;cat id= "Food" &gt;


&lt;goods id= "Food11" &gt;


&lt;name&gt;food11&lt;/name&gt;


&lt;price&gt;12.90&lt;/price&gt;


&lt;/goods&gt;


&lt;goods id= "Food12" &gt;


&lt;name&gt;food12&lt;/name&gt;


&lt;price&gt;22.10&lt;/price&gt;


&lt;desc creator= "Hahawen" &gt; Good Stuff Recommendation &lt;/desc&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat&gt;


&lt;goods id= "Tel21" &gt;


&lt;name&gt;tel21&lt;/name&gt;


&lt;price&gt;1290&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat id= "Coat" &gt;


&lt;goods id= "Coat31" &gt;


&lt;name&gt;coat31&lt;/name&gt;


&lt;price&gt;112&lt;/price&gt;


&lt;/goods&gt;


&lt;goods id= "Coat32" &gt;


&lt;name&gt;coat32&lt;/name&gt;


&lt;price&gt;45&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;special id= "Hot" &gt;


&lt;goods&gt;


&lt;name&gt;hot41&lt;/name&gt;


&lt;price&gt;99&lt;/price&gt;


&lt;/goods&gt;


&lt;/special&gt;


&lt;/shop&gt; The following is a getnode () function that returns information about all the items under a category Product Name: Food11


Array


(


[Name] =&gt; food11


[Price] =&gt; 12.90


)


Array


(


[ID] =&gt; FOOD11


)


Product Name: Food12


Array


(


[Name] =&gt; food12


[Price] =&gt; 22.10


[desc] =&gt; Array


(


[value] =&gt; good things recommend


[Attrs] =&gt; Array


(


[Creator] =&gt; Hahawen


)





)





)


Array


(


[ID] =&gt; food12


)


The following is a message that returns a product via the Findnodebypath () function Product Name: Food11


Array


(


[Name] =&gt; food11


[Price] =&gt; 12.90


)


Array


(


[ID] =&gt; FOOD11


)


The following is a SetValue () function that adds a property to the product "Food11" and then displays the result of the addition &lt;?xml version= "1.0" encoding= "GB2312"?&gt;


&lt;shop&gt;


&lt;name&gt; Hualian &lt;/name&gt;


&lt;address&gt; Beijing Changan Street-No. No. 9999 &lt;/address&gt;


&lt;desc&gt; supermarket chain &lt;/desc&gt;


&lt;telphone&gt;123456789&lt;/telphone&gt;


&lt;cat id= "Food" &gt;


&lt;goods id= "Food11" &gt;


&lt;name&gt;food11&lt;/name&gt;


&lt;price&gt;12.90&lt;/price&gt;


&lt;leaveword author= "Hahawen" date= "2004-12-05" &gt; This product is good &lt;/leaveword&gt;


&lt;/goods&gt;


&lt;goods id= "Food12" &gt;


&lt;name&gt;food12&lt;/name&gt;


&lt;price&gt;22.10&lt;/price&gt;


&lt;desc creator= "Hahawen" &gt; Good Stuff Recommendation &lt;/desc&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat&gt;


&lt;goods id= "Tel21" &gt;


&lt;name&gt;tel21&lt;/name&gt;


&lt;price&gt;1290&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat id= "Coat" &gt;


&lt;goods id= "Coat31" &gt;


&lt;name&gt;coat31&lt;/name&gt;


&lt;price&gt;112&lt;/price&gt;


&lt;/goods&gt;


&lt;goods id= "Coat32" &gt;


&lt;name&gt;coat32&lt;/name&gt;


&lt;price&gt;45&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;special id= "Hot" &gt;


&lt;goods&gt;


&lt;name&gt;hot41&lt;/name&gt;


&lt;price&gt;99&lt;/price&gt;


&lt;/goods&gt;


&lt;/special&gt;


&lt;/shop&gt; The following is a removevalue ()/removeattribute () function that changes and deletes attributes to the product "Food11", and then displays the result of the Operation &lt;?xml version= "1.0" encoding= "GB2312"?&gt;


&lt;shop&gt;


&lt;name&gt; Hualian &lt;/name&gt;


&lt;address&gt; Beijing Changan Street-No. No. 9999 &lt;/address&gt;


&lt;desc&gt; supermarket chain &lt;/desc&gt;


&lt;telphone&gt;123456789&lt;/telphone&gt;


&lt;cat id= "Food" &gt;


&lt;goods id= "Food11" &gt;


&lt;name&gt;food11&lt;/name&gt;


&lt;price&gt;12.90&lt;/price&gt;


&lt;leaveword author= "Hahawen" date= "2004-12-05" &gt; This product is good &lt;/leaveword&gt;


&lt;/goods&gt;


&lt;goods id= "Food12" &gt;


&lt;name&gt;new food12&lt;/name&gt;


&lt;price&gt;22.10&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat&gt;


&lt;goods id= "Tel21" &gt;


&lt;name&gt;tel21&lt;/name&gt;


&lt;price&gt;1290&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat id= "Coat" &gt;


&lt;goods id= "Coat31" &gt;


&lt;name&gt;coat31&lt;/name&gt;


&lt;price&gt;112&lt;/price&gt;


&lt;/goods&gt;


&lt;goods id= "Coat32" &gt;


&lt;name&gt;coat32&lt;/name&gt;


&lt;price&gt;45&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;special id= "Hot" &gt;


&lt;goods&gt;


&lt;name&gt;hot41&lt;/name&gt;


&lt;price&gt;99&lt;/price&gt;


&lt;/goods&gt;


&lt;/special&gt;


&lt;/shop&gt; Here's the CreateNode () function, add the product, and then show the added results &lt;?xml version= "1.0" encoding= "GB2312"?&gt;


&lt;shop&gt;


&lt;name&gt; Hualian &lt;/name&gt;


&lt;address&gt; Beijing Changan Street-No. No. 9999 &lt;/address&gt;


&lt;desc&gt; supermarket chain &lt;/desc&gt;


&lt;telphone&gt;123456789&lt;/telphone&gt;


&lt;cat id= "Food" &gt;


&lt;goods id= "Food11" &gt;


&lt;name&gt;food11&lt;/name&gt;


&lt;price&gt;12.90&lt;/price&gt;


&lt;leaveword author= "Hahawen" date= "2004-12-05" &gt; This product is good &lt;/leaveword&gt;


&lt;/goods&gt;


&lt;goods id= "Food12" &gt;


&lt;name&gt;new food12&lt;/name&gt;


&lt;price&gt;22.10&lt;/price&gt;


&lt;/goods&gt;


&lt;goods id= "Food13" &gt;


&lt;name&gt;food13&lt;/name&gt;


&lt;price&gt;100&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat&gt;


&lt;goods id= "Tel21" &gt;


&lt;name&gt;tel21&lt;/name&gt;


&lt;price&gt;1290&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat id= "Coat" &gt;


&lt;goods id= "Coat31" &gt;


&lt;name&gt;coat31&lt;/name&gt;


&lt;price&gt;112&lt;/price&gt;


&lt;/goods&gt;


&lt;goods id= "Coat32" &gt;


&lt;name&gt;coat32&lt;/name&gt;


&lt;price&gt;45&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;special id= "Hot" &gt;


&lt;goods&gt;


&lt;name&gt;hot41&lt;/name&gt;


&lt;price&gt;99&lt;/price&gt;


&lt;/goods&gt;


&lt;/special&gt;


&lt;/shop&gt; The following is the Removenode () function that deletes the item and then displays the result of the deletion &lt;?xml version= "1.0" encoding= "GB2312"?&gt;


&lt;shop&gt;


&lt;name&gt; Hualian &lt;/name&gt;


&lt;address&gt; Beijing Changan Street-No. No. 9999 &lt;/address&gt;


&lt;desc&gt; supermarket chain &lt;/desc&gt;


&lt;telphone&gt;123456789&lt;/telphone&gt;


&lt;cat id= "Food" &gt;


&lt;goods id= "Food11" &gt;


&lt;name&gt;food11&lt;/name&gt;


&lt;price&gt;12.90&lt;/price&gt;


&lt;leaveword author= "Hahawen" date= "2004-12-05" &gt; This product is good &lt;/leaveword&gt;


&lt;/goods&gt;


&lt;goods id= "Food13" &gt;


&lt;name&gt;food13&lt;/name&gt;


&lt;price&gt;100&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat&gt;


&lt;goods id= "Tel21" &gt;


&lt;name&gt;tel21&lt;/name&gt;


&lt;price&gt;1290&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;cat id= "Coat" &gt;


&lt;goods id= "Coat31" &gt;


&lt;name&gt;coat31&lt;/name&gt;


&lt;price&gt;112&lt;/price&gt;


&lt;/goods&gt;


&lt;goods id= "Coat32" &gt;


&lt;name&gt;coat32&lt;/name&gt;


&lt;price&gt;45&lt;/price&gt;


&lt;/goods&gt;


&lt;/cat&gt;


&lt;special id= "Hot" &gt;


&lt;goods&gt;


&lt;name&gt;hot41&lt;/name&gt;


&lt;price&gt;99&lt;/price&gt;


&lt;/goods&gt;


&lt;/special&gt;


&lt;/shop&gt;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.