Php XML file interpretation class application instance, php XML class application instance

Source: Internet
Author: User
Tags learn php programming

Php XML file interpretation class application instance, php XML class application instance

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 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"?> <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>'; ?> 

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.


How does php generate xml files?

<? Php
# Use dom to generate xml. Note that the generated xml contains no spaces.
$ Dom = new DOMDocument ('1. 0', 'utf-8 ');
$ Path = "test. xml"; // $ path is the storage path of 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 [latest news]> ');
$ Title-> appendChild ($ title_value );
$ News-> appendChild ($ title );

$ Info = $ dom-> createElement ('info ');
$ Info_value = $ dom-> createTextNode ('<! [CDATA [the surface of the system is made of imported wooden skins. It is perfectly presented with natural and clear wood textures. The reasonable combination of various materials creates elegant series of exquisite furniture products.
]> ');
$ Info-> appendChild ($ info_value );
$ News-> appendChild ($ info );

Echo $ dom-> saveXML ();
$ Dom-> save ($ path );
?>

Read the xml content in the PHP File

<%
Dim xml, objNode, objasd, nCntChd, nCntAtr
Set xml = Server. CreateObject ("Microsoft. XMLDOM ")
Xml. Async = False
Xml. Load (Server. MapPath ("test. xml "))

Set objnode1_xml.doc umentElement
NCntChd = objNode. ChildNodes. length-1

'This can define the value that asp reads from the xml file, and pass this value to determine the data to be read.

For I = 0 to nCntChd
Set objasd = objNode. ChildNodes. item (I)
NCntAtr = objret. Attributes. length-1

'All record items in a record are recorded starting from 0.

For j = 0 to nCntAtr
Response. write objret. Attributes. item (j). Text & "<br>"
Next
Response. write "<br>"
Next

Set objrecognition = Nothing
Set objNode = Nothing
Set xml = Nothing
%>
Reference: homepage.yesky.com/#/3074869.shtml

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.