Reading XML data in PHP

Source: Internet
Author: User
Tags closing tag manual functions processing instruction reference
xml| data

There was a problem at work today because our project data is so small that we need to borrow data from web search, and they only provide us with an XML interface. Therefore, we need to transform the XML data into HTML to present to everyone. Because the project is based on PHP, so I abandoned the use of JS to read XML to choose to continue to use PHP. However, I have never done this kind of attempt before, so I found a lot of information on the Web at the same time referencing the PHP work manual, it is a good choice to use the parser function in a PHP4 environment (but you can also use DOM, but need to reconfigure the server PHP5 support the DOM better).

Although no previous contact with such a problem, but it was quickly resolved, but in the process of solving and groping on the internet about this kind of data, although a lot of, but uneven, many descriptions are not very detailed, or operation manual is more useful.

Okay, here's the job:

Parser is a built-in parser for working with XML in PHP, which consists of three events: starting tag, reading data, closing tag.

That is, when the XML is processed, the function moves to transform the XML data whenever it encounters the start tag, the data, and the end tag.

Introduction to XML reading related functions in PHP:

Reference:

------------------------------------------------------------------

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:

<?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;
}

?>

The XML file code is 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>

The results of this procedure are as follows:

Reference:
-----------------------------------------------------------------

Name: John Position: Manager
Name: Dick Position: Assistant

-----------------------------------------------------------------



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.