Php XML file interpretation application example

Source: Internet
Author: User
Tags learn php programming
This article mainly introduces php's XML file interpretation class and its application examples, including reading, interpreting, checking, and outputting XML files. it is very practical, you can refer to the example in this article to describe the php XML file interpretation class 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:

<? Php/** XML file analysis class * Date: 2013-02-01 * Author: fdipzone * Ver: 1.0 ** func: * loadXmlFile ($ xmlfile) read xml file output Array * loadXmlString ($ xmlstring) read the xmlstring output Array */class XMLParser {/** read the xml file * @ param String $ xmlfile * @ return Array */public function loadXmlFile ($ xmlfile) {// get xmlfile content $ xmlstring = file_exists ($ xmlfile )? File_get_contents ($ xmlfile): ''; // parser xml list ($ flag, $ data) = $ this-> 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 (); // check 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_obj = xml_parser_create (); if (xml_parse_into_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_array ($ value )) {$ this-> objectToArray ($ value); $ object [$ key] = $ value ;}}}/** output returns * @ 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:

<?php require "XMLParser.class.php";  $xmlfile = 'file.xml'; $xmlstring = '<?xml version="1.0" encoding="utf-8"?> 
  
  
   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.