Easily parse XML documents with PHP 5.0 (1)

Source: Internet
Author: User
When XML is in sax, it has to build 3 functions, and to return data directly with the three function, it requires 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 a nodal, and it's a lot of code to write, 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 the basic XML file nodes.

2, export all the data of the XML file into an array.

3, the entire design using OO way, in the operation of the result set, the use of methods similar to the DOM

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. When querying a node, you can use 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

  
  
   
Hualian
Chang ' An Street, Beijing, No. No. 9999
Supermarket chains
Food11
12.90

food12
22.10
good thing recommended



tel21
1290


coat31
112

Coat32





hot41




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,
Change and delete attributes for the product \ "Food11\", and then display 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 ());
? >



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.