Introduction to XML read-related functions in PHP _php tutorial

Source: Internet
Author: User
Tags processing instruction
Object XML parsing function description
The start and end of the 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 entities xml_set_unparsed_entity_decl_handler () unresolved external entities appear
Processing instruction Xml_set_processing_instruction_handler () the appearance of processing instructions
Notation Statement Xml_set_notation_decl_handler () The appearance of the notation statement
Default Xml_set_default_handler () other events that do not specify a handler function

Here is a small 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: Zhang San Position: Manager
Name: John Doe Position: Assistant
Copy CodeThe code is as follows:



Tom
Manager



John doe
Assistant




Copy CodeThe code is as follows:
$parser = Xml_parser_create (); Create a parser editor
Xml_set_element_handler ($parser, "startelement", "endElement");//Set the corresponding function when the tag is triggered here are startelement and endelenment respectively
Xml_set_character_data_handler ($parser, "characterdata");//Set the corresponding function when reading data
$xml _file= "1.xml";//Specifies the XML file to be 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 per fetch 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. "
";
if ($name)
echo $xml _data. "
";
}

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

?>


Introduction to PHP Read XML method

One, what is the purpose of xml,xml?
XML (extensible Markup Language) extends the markup language, which, like HTML, is SGML (Standard generalized Markup Language, the standardized universal Markup language). XML is a cross-platform, content-dependent technology in an Internet environment, and is a powerful tool for the current processing of structured document information. 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 occupies more space than binary data, but XML is extremely simple and easy to master and use.
XML has many uses, can be used to store data, can be used to do data exchange, for many kinds of application software to prompt data and so on.
Two, PHP method of reading XML
XML source file
Copy CodeThe code is as follows:



Zhang Ying
Man
28


Tank
Man
28



1) DOMDocument Read XML
Copy CodeThe code is as follows:
$doc = new DOMDocument ();
$doc->load (' person.xml '); Reading an XML file
$humans = $doc->getelementsbytagname ("humans"); An array of objects that get humans tags
foreach ($humans as $human)
{
$names = $human->getelementsbytagname ("name"); An array of objects that get the label of name
$name = $names->item (0)->nodevalue; Gets the value in node, such as
$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 CodeThe code is as follows:
$xml _array=simplexml_load_file (' person.xml '); Reads the data from the XML into the array object
foreach ($xml _array as $tmp) {
echo $tmp->name. " -". $tmp->sex." -". $tmp->old."
";
}
?>

3) Use PHP Regular expressions to memorize the data
Copy CodeThe code is as follows:
$xml = "";
$f = fopen (' person.xml ', ' R ');
while ($data = Fread ($f, 4096)) {
$xml. = $data;
}
Fclose ($f);
Read Data above
Preg_match_all ("/\ (.*?) \<\/humans\>/s ", $xml, $humans); Match the contents of the outermost label
foreach ($humans [1] as $k = $human)
{
Preg_match_all ("/\ (.*?) \<\/name\>/", $human, $name); Match a name
Preg_match_all ("/\ (.*?) \<\/sex\>/", $human, $sex); Match out gender
Preg_match_all ("/\ (. *?) \<\/old\>/", $human, $old); Match age
}
foreach ($name [1] as $key = + $val) {
echo $val. "-". $sex [$key][1]. "-" $old [$key][1]. "
";
}
?>

4) XmlReader to read XML data
< The u> copy code code is as follows:
!--? php $reader = new XMLReader ();
$reader->open (' person.xml ');//Read XML data
$i = 1;
while ($reader->read ()) {//Read
if ($reader->nodetype = = Xmlreader::text) {//Determine node type
if ($i%3) {
Echo $reader->value;//Gets the value of node
}else{
Echo $reader->value.
";
}
$i + +;
}
}
?>

Three, summary
There are many ways to read XML, just to name a few. The above four methods can read the data in the label, Zhang Ying. But their focus is different, the first three methods of reading the function of XML is designed to read the value in the tag, equivalent to the text () method in jquery, and XmlReader he is not quite the same, His focus is not to read the value in the tag, but to read the label's properties and put the data to be transferred in the attribute (although 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 the XML file out).
For an example,
Related Article

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.