Php XML file interpretation application example

Source: Internet
Author: User
This article describes the php XML file interpretation class and its usage. it is a very practical technique. Share it with you for your reference. The XMLParser. class. php class file is as follows :? Php ** XML file analysis class * Date: 2013-02-01 * Author: fdipzone * This article describes the XML file interpretation class of php and its usage, which is a very practical technique. Share it with you for your reference. The details are as follows:

The XMLParser. class. php class file is as follows:

 Parser ($ xmlstring); return $ this-> response ($ flag, $ data );} /** read xmlstring * @ param String $ xmlstring * @ return Array */public function loadXmlString ($ xmlstring) {// parser xml list ($ flag, $ data) = $ this-> parser ($ xmlstring); return $ this-> response ($ flag, $ data );} /** interpret xml content * @ param String $ xmlstring * @ return Array */private function parser ($ xmlstring) {$ flag = false; $ data = array (); // chec K xml format if ($ this-> checkXmlFormat ($ xmlstring) {$ flag = true; // xml to object $ data = simpleXML_load_string ($ xmlstring, 'simplexmlelement ', LIBXML_NOCDATA); // object to array $ this-> objectToArray ($ data);} return array ($ flag, $ data );} /** check whether the xml format is correct * @ param String $ xmlstring * @ return boolean */private function checkXmlFormat ($ xmlstring) {if ($ xmlstring = '') {return false;} $ xml_parser_o Bj = xml_parser_create (); if (xml_parse_ct _struct ($ xml_parser_obj, $ xmlstring, $ vals, $ indexs) === 1) {// 1: success 0: fail return true ;} else {return false ;}}/** convert object to Array * @ param object $ object * @ return Array */private function objectToArray (& $ object) {$ object = (array) $ object; foreach ($ object as $ key => $ value) {if ($ value = '') {$ object [$ key] = "";} else {if (is_object ($ value) | is_arra Y ($ value) {$ this-> objectToArray ($ value); $ object [$ key] = $ value ;}}}} /** output return * @ param boolean $ flag true: false * @ param Array $ data converted data * @ return Array */private function response ($ flag = false, $ data = array () {return array ($ flag, $ data) ;}}?>

The Demo sample program is as follows:

  
  
  
   1000
   
   
  
   
    100
    
   
    fdipzone
    
   
    1
    28 
   
 '; echo '
'; $xml_parser = new XMLParser(); echo "response xmlfile\r\n"; list($flag, $xmldata) = $xml_parser->loadXmlFile($xmlfile); if($flag){   print_r($xmldata); } echo "response xmlstring\r\n"; list($flag, $xmldata) = $xml_parser->loadXmlString($xmlstring); if($flag){   print_r($xmldata); } echo '
'; ?>

For XML pre-defined constants in PHP, refer to the official documentation:
Http://www.php.net/manual/en/libxml.constants.php

I hope this article will help you learn PHP programming.

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.