Introduction to XML-read-related functions in PHP a _php technique

Source: Internet
Author: User
Tags fread php regular expression processing instruction
Object XML parsing function description
Start and end of element Xml_set_element_handler () element
Start of character data Xml_set_character_data_handler () character data
External entity Xml_set_external_entity_ref_handler () external entity appears
unresolved external entity xml_set_unparsed_entity_decl_handler () unresolved external entities appear
Processing instruction Xml_set_processing_instruction_handler () the appearance of processing instructions
The appearance of the Declaration of Xml_set_notation_decl_handler () notation of notation
Default Xml_set_default_handler () other events that do not have a handler function specified

Let's give you a little example of using the parser function to read XML data:

The XML file code is as follows:

The results of this procedure are as follows:

References:--------------------------------------------------------------------------------
Name: John Position: Manager
Name: Dick Position: Assistant
Copy Code code as follows:

<?xml version= "1.0"?>
<employees>
<employee>
<name> John </name>
<position age= > Manager </position>
</employee>
<employees>
<employee>
<name> Dick </name>
<position age= > Assistant </position>
</employee>
</employees>


Copy Code code as follows:

<?php
$parser = Xml_parser_create (); Create a parser editor
Xml_set_element_handler ($parser, "startelement", "endelement")//Set up the corresponding function when the label is triggered here are startelement and endelenment, respectively.
Xml_set_character_data_handler ($parser, "characterdata"); corresponding function to set up data reading
$xml _file= "1.xml";//Specify the XML file you want to read, which can be a URL
$filehandler = fopen ($xml _file, "R");//Open File

while ($data = Fread ($filehandler, 4096))
{
Xml_parse ($parser, $data, feof ($filehandler));
}//4,096 bytes at a time for processing

Fclose ($filehandler);
Xml_parser_free ($parser);//Close and Release parser parser

$name =false;
$position =false;
function startelement ($parser _instance, $element _name, $attrs)//start tag event functions
{
Global $name, $position;
if ($element _name== "name")
{
$name =true;
$position =false;
echo "Name:";
}
if ($element _name== "POSITION")
{$name =false;
$position =true;
echo "Position:";
}
}

function Characterdata ($parser _instance, $xml _data)//functions when reading data
{
Global $name, $position;
if ($position)
echo $xml _data. " <br> ";
if ($name)
echo $xml _data. " <br> ";
}

function EndElement ($parser _instance, $element _name)//END tag event functions
{
Global $name, $position;
$name =false;
$position =false;
}

?>


Introduction to PHP reading XML methods

One, what is Xml,xml's purpose?
XML (extensible Markup Language) expands the markup language, which, like HTML, is SGML (Standard generalized Markup Language, Standard Universal Markup Language). XML is a cross-platform, content-dependent technology in an Internet environment and a powerful tool for dealing with structured document information at the moment. Extensible Markup Language XML is a simple data storage language that uses a series of simple tags to describe data that can be built in a convenient way, although XML takes up more space than binary data, but XML is extremely simple and easy to master and use.
XML can be used to store data, to exchange data, to prompt data for many kinds of applications, and so on.
two, the way PHP reads XML
XML source files
Copy Code code as follows:

<?xml version= "1.0 encoding=" UTF-8 "?>"
<zhangying>
<name> Zhang Ying </name>
<sex> male </sex>
<old>28</old>
</zhangying>
<tank>
<name>tank</name>
<sex> male </sex>
<old>28</old>
</tank>

1) DOMDocument Read XML
Copy Code code as follows:

<?php
$doc = new DOMDocument ();
$doc->load (' person.xml '); reading XML files
$humans = $doc->getelementsbytagname ("humans"); An array of objects to get the humans label
foreach ($humans as $human)
{
$names = $human->getelementsbytagname ("name"); An array of objects that gets the label for name
$name = $names->item (0)->nodevalue; Get the values in node, such as <name> </name>
$sexs = $human->getelementsbytagname ("sex");
$sex = $sexs->item (0)->nodevalue;
$olds = $human->getelementsbytagname ("old");
$old = $olds->item (0)->nodevalue;
echo "$name-$sex-$old \ n";
}
?>

2) simplexml Read XML
Copy Code code as follows:

<?php
$xml _array=simplexml_load_file (' person.xml '); Reads data from XML into an array object
foreach ($xml _array as $tmp) {
echo $tmp->name. " -". $tmp->sex." -". $tmp->old." <br> ";
}
?>

3 Use PHP Regular expression to memorize the data
Copy Code code as follows:

<?php
$xml = "";
$f = fopen (' person.xml ', ' R ');
while ($data = Fread ($f, 4096)) {
$xml. = $data;
}
Fclose ($f);
Read Data above
Preg_match_all ("/\foreach ($humans [1] as $k => $human)
{
Preg_match_all ("/\<name\>" (. *?) \<\/name\>/", $human, $name); Match a name
Preg_match_all ("/\<sex\>" (. *?) \<\/sex\>/", $human, $sex); Match a gender
Preg_match_all ("/\<old\>" (. *?) \<\/old\>/", $human, $old); Match out of age
}
foreach ($name [1] as $key => $val) {
echo $val. "-". $sex [$key][1]. "-" $old [$key][1]. " <br> ";
}
?>

4) XmlReader to read XML data
Copy Code code as follows:

<?php
$reader = new XMLReader ();
$reader->open (' person.xml '); Reading XML data
$i = 1;
while ($reader->read ()) {//is read
if ($reader->nodetype = = Xmlreader::text) {//Determine node type
if ($i%3) {
Echo $reader->value; Gets the value of node
}else{
echo $reader->value. " <br> ";
}
$i + +;
}
}
?>

   Third, summary
There are a lot of ways to read XML, simply to mention a few. All four of the above methods are able to read the data in the tag, Zhang Ying. But their focus is different, the first three methods of reading the XML function is designed to read the value in the tag, the equivalent of jquery in the text () method, and XmlReader he is not the same, He's not focused on reading the values in the label, and read the properties of the tag, put the data to be transferred, all in the attribute (but the method I wrote above still takes the value in the tag, because the XML file is already given, I don't want to get out of the XML file).
To illustrate, for example,
<data name= ' Zhang Ying ' sex= ' old= ' 28′></data>
XmlReader's design focus is to read the value of the name sex old in data, and the content is more cumbersome to read. He is equivalent to the attr in jquery (");
  The above is purely personal opinion, please correct me. Hope to be helpful to everyone.

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.