PHP XML File Interpretation class application example _php skill

Source: Internet
Author: User

This article illustrates the PHP XML file interpretation class and its usage, which is a very practical technique. Share to everyone for your reference. Specifically as follows:

The XMLParser.class.php class files are as follows:

<?php/** XML File Analysis class * date:2013-02-01 * author:fdipzone * ver:1.0 * Func: * Loadxmlfile ($xmlfile) Read in XML file output array * loadxmlstring ($xmlstring) read into xmlstring output array/class xmlparser{/** read XML file * @param Str ing $xmlfile * @return Array */Public Function Loadxmlfile ($xmlfile) {//Get xmlfile content $xmlstrin g = 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) {$f 
    lag = 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 that the XML format is correct * @param String $xmlstring * @return Boolean/Private function Checkxmlformat ($xmlstr 
    ing) {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; }/** 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 ARR 
  AY */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"?> 
<xmlroot> 
<status>1000</status > 
<info></info> 
<result><id>100</id> 
<name>fdipzone</ name> 
<gender>1</gender> 
<age>28</age> 
</result> 
</ Xmlroot> '; 
Echo ' <pre> '; 
$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 ' </pre> '; 
? > 

XML predefined constants for PHP refer to the official documentation:
http://www.php.net/manual/en/libxml.constants.php

I hope this article will help you with the learning of 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.