PHP XML File Interpretation class application example, Phpxml class application Example _php tutorial

Source: Internet
Author: User
Tags cdata learn php learn php programming php programming

PHP XML File Interpretation class application example, Phpxml class application Example


The examples in this paper describe the PHP XML file interpretation class and its usage, which is a very practical technique. Share to everyone for your reference. Specific as follows:

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

<?php/** XML File Parsing class * date:2013-02-01 * author:fdipzone * ver:1.0 * * Func: * Loadxmlfile ($xmlfile) read into XML file   Output array * loadxmlstring ($xmlstring) read into xmlstring output array */class xmlparser{/** read XML file * @param String $xmlfile * @return Array */Public Function Loadxmlfile ($xmlfile) {//get xmlfile content $xmlstring = file_exists ($xml File)?     File_get_contents ($xmlfile): ";     Parser XML List ($flag, $data) = $this->parser ($xmlstring);   return $this->response ($flag, $data);     }/** reads xmlstring * @param String $xmlstring * @return Array */Public Function loadxmlstring ($xmlstring) {     Parser XML List ($flag, $data) = $this->parser ($xmlstring);   return $this->response ($flag, $data); }/** interprets XML content * @param String $xmlstring * @return Array */Private Function parser ($xmlstring) {$flag = f     Alse;     $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 ($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; }}/** 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 */privat   e 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 "?> 
 
   
  
   
  
    
  
    
  
   
   
    
     
    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 '

The XML pre-defined constants for PHP can be consulted in the official documentation:
http://www.php.net/manual/en/libxml.constants.php

I hope this article will be helpful for you to learn PHP programming.


How PHP generates XML files

#使用dom生成xml, note that there are no spaces in the generated XML.
$dom =new DOMDocument (' 1.0 ', ' utf-8 ');
$path = "Test.xml"; $path the storage path for the XML file.

$module = $dom->createelement (' newmodule ');//root node
$dom->appendchild ($module);

$year = $dom->createelement (' year '); Add Attribute Node
$name = $dom->createattribute (' name ');
$name->nodevalue= "Latest News";
$year->setattributenode ($name);
$module->appendchild ($year);

$news = $dom->createelement (' News ');
$year->appendchild ($news);

$date = $dom->createelement (' Date ');
$date _value= $dom->createtextnode (' 01-24 ');
$date->appendchild ($date _value);
$news->appendchild ($date);

$title = $dom->createelement (' title ');
$title _value= $dom->createtextnode ('<![CDATA[最新动态]]>');
$title->appendchild ($title _value);
$news->appendchild ($title);

$info = $dom->createelement (' info ');
$info _value= $dom->createtextnode ('<![CDATA[ 表面采用进口楸木木皮拼贴成精美的拼花,自然清晰的木材纹理得到完美的呈现,各种材质的合理搭配缔造了雅意系列精致的家具产品。 <br /> ]] >');
$info->appendchild ($info _value);
$news->appendchild ($info);

echo $dom->savexml ();
$dom->save ($path);
?>

Reading XML content from a PHP file

<%
Dim Xml,objnode,objatr,ncntchd,ncntatr
Set xml=server.createobject ("Microsoft.XMLDOM")
Xml. Async=false
Xml. Load (Server.MapPath ("Test.xml"))

Set objnode=xml.documentelement
Ncntchd=objnode.childnodes.length-1

' This defines the value that ASP reads the XML file by passing this value to determine what data is being read

For I=0 to Ncntchd
Set Objatr=objnode.childnodes.item (i)
Ncntatr=objatr.attributes.length-1

' All the records in a record, the record is starting from 0

For J=0 to Ncntatr
Response.Write ObjAtr.Attributes.item (j). text& "
"
Next
Response.Write "
"
Next

Set objatr=nothing
Set objnode=nothing
Set xml=nothing
%>
Reference: homepage.yesky.com/369/3074869.shtml

http://www.bkjia.com/PHPjc/882903.html www.bkjia.com true http://www.bkjia.com/PHPjc/882903.html techarticle PHP XML File Interpretation class application example, Phpxml class application Example This article describes the PHP XML file interpretation class and its usage, is a very practical technique. Share to everyone for your reference. ...

  • Related Article

    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.