Example of using xmlreader to read xml data in PHP

Source: Internet
Author: User
This article describes how to use xmlreader to read xml data in PHP. the example in this article is relatively simple and only contains one reading function. For more information, see the following XML file:

The code is as follows:


<? Xml version = "1.0"?>


Simpsons
FOX
8: 00 PM
30


Law & Order
NBC
8: 00 PM
60


Here we use XMLReader extension to parse and process the file. XMLReader also has the advantage of reading data in the form of Stream, that is, even if the xml file is large, it can still be processed with ease. The following describes how to process the above files:

The code is as follows:


$ IndexUrl = 'http: // www.xxx.com/xxx.xml ';
$ Reader = new XMLReader ();
$ Reader-> open ($ indexUrl );
$ CountElements = 0;

While ($ reader-> read ()){
If ($ reader-> nodeType = XMLReader: ELEMENT ){
$ NodeName = $ reader-> name;
}
If ($ reader-> nodeType = XMLReader: TEXT &&! Empty ($ nodeName )){
Switch ($ nodeName ){
Case 'name ':
$ Name = $ reader-> value;
Break;
Case 'channel ':
$ Channel = $ reader-> value;
Break;
Case 'start ':
$ Start = $ reader-> value;
Break;
Case 'duration ':
$ Duration = $ reader-> value;
Break;
}
}
}
$ Reader-> close ();

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.