Three methods for php to read xml files

Source: Internet
Author: User
Tags foreach fread regular expression
The code is as follows: Copy code

<? Php
$ Doc = new DOMDocument ();
$ Doc-> load ('books. XML ');
 
$ Books = $ doc-> getElementsByTagName ("book ");
Foreach ($ books as $ book)
  {
$ Authors = $ book-> getElementsByTagName ("author ");
$ Author = $ authors-> item (0)-> nodeValue;
 
$ Publishers = $ book-> getElementsByTagName ("publisher ");
$ Publisher = $ publishers-> item (0)-> nodeValue;
 
$ Titles = $ book-> getElementsByTagName ("title ");
$ Title = $ titles-> item (0)-> nodeValue;
 
Echo "$ title-$ author-$ publisher/n ";
  }
?>


Regular expression parsing

The code is as follows: Copy code
<? Php
$ Xml = "";
$ F = fopen ('books. XML', 'r ');
While ($ data = fread ($ f, 4096) {$ xml. = $ data ;}
Fclose ($ f );
 
Preg_match_all ("// <book/> (.*?) /<// Book/>/s ",
$ Xml, $ bookblocks );
 
Foreach ($ bookblocks [1] as $ block)
  {
Preg_match_all ("// <author/> (.*?) /<// Author/> /",
$ Block, $ author );
Preg_match_all ("// <title/> (.*?) /<// Title/> /",
$ Block, $ title );
Preg_match_all ("// <publisher/> (.*?) /<// Publisher/> /",
$ Block, $ publisher );
Echo ($ title [1] [0]. "-". $ author [1] [0]. "-".
$ Publisher [1] [0]. "/n ");
  }
?>


The books. xml file is as follows:

The code is as follows: Copy code
<Books>
<Book>
<Author> Jack Herrington </author>
<Title> PHP Hacks </title>
<Publisher> O 'Reilly </publisher>
</Book>
<Book>
<Author> Jack Herrington </author>
<Title> Podcasting Hacks </title>
<Publisher> O 'Reilly </publisher>
</Book>
</Books>


Here is a small example to use the parser function to read xml data:

 

The code is as follows: Copy code

<? Php
$ Parser = xml_parser_create (); // Create a parser editor
Xml_set_element_handler ($ parser, "startElement", "endElement"); // corresponding functions for tag setting triggering are startElement and endElenment respectively.
Xml_set_character_data_handler ($ parser, "characterData"); // you can specify a function for reading data.
$ Xml_file = "1.xml"; // specify the xml file to be read, which can be a url
$ Filehandler = fopen ($ xml_file, "r"); // open the file

 


While ($ data = fread ($ filehandler, 4096 ))
{
Xml_parse ($ parser, $ data, feof ($ filehandler ));
} // Extract 4096 bytes each time for processing

Fclose ($ filehandler );
Xml_parser_free ($ parser); // Close and release the parser


$ Name = false;
$ Position = false;
Function startElement ($ parser_instance, $ element_name, $ attrs) // function of the start tag event
 {
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) // function used to read data
{
Global $ name, $ position;
If ($ position)
Echo $ xml_data. "<br> ";
If ($ name)
Echo $ xml_data. "<br> ";
}

Function endElement ($ parser_instance, $ element_name) // function for ending a tag event
{
Global $ name, $ position;
$ Name = false;
$ Position = false;
}

?>

The xml file code is as follows:

 

The code is as follows: Copy code

<? Xml version = "1.0"?>
<Employees>
<Employee>
<Name> Zhang San </name>
<Position age = "45"> manager </position>
</Employee>
<Employees>
<Employee>
<Name> Li Si </name>
<Position age = "45"> assistant </position>
</Employee>
</Employees>

Parser is a php built-in parser used to process xml. Its work consists of three events: start tag, read data, and end tag.

That is to say, when processing xml, the function performs the corresponding action to complete the conversion of xml data whenever the start tag, data, and end tag are encountered.

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.