Getting Started with PHP (+ PHP) and XML

Source: Internet
Author: User
Tags getting started with php tag name

    1. What is XML?

XML (extensible Markup Languge) is an Extensible markup language, similar to HTML, that allows users to customize their own labels.


What can 2.XML do?

XML is used to store data, store data in one format into an XML document, parse the XML document, and finally output the data in another way.

The purpose of XML is generally:

Structured data, such as mail Address Book, System configuration file

Standard exchange data for exchanging data between multiple platforms or application systems

Create a new markup language for more functionality, such as RSS (easy information Aggregation)


Syntax format for 3.XML

Here is a simple XML file, note that when the file is saved, the encoding format should be selected Utf-8

<?xml version= "1.0"  encoding= "UTF-8"  standalone= "yes"? ><?xml-stylesheet type= " Text/css "?>< Computer books  name=" book ">    <php type=" Computer_book ">          <bookname> Development Typical Module Daquan </bookname>          <price unit= "Dollar each" >79.00</price>          <publishdate>2008-09-10</publishdate>     </php>    <php type= "Computer_book" >         <bookname>Java</bookname>         <price unit= "Dollar each" >79.00</price>         <publishdate>2008-09-10</publishdate>    </PHP>     <php type= "Computer_book ">        <bookname>Java</bookname>         <price unit= "Dollar each" >79.00</price>         <publishdate>2008-09-10</publishdate>     </php></Computer Books >

The first behavior of the XML declaration, and must appear in the first row, and the first line cannot be a blank line, description of the XML version, encoding and other information.

Comments <!----FOR XML >

XML Elements < tags > data content </tags >


If the label contains special characters such as >< &, there are two ways to solve the problem:

The first uses the entity reference (

< &lt;

> $gt;

' &apos;

"&quot;

& &amp; Note that you must not forget the semicolon at the end

The second type of case that applies especially to special characters

<! [cdata[

Special characters here can normally output < > ' "and other special characters

]]>


4. How do I parse an XML file?

You can use the SimpleXML function of PHP5

<?phpheader ("Content-type:text/html;charset=utf-8"); $xml = Simplexml_load_file ("Book.xml");/* Create the SimpleXML object by loading the XML file as the root node object *///print_r ($xml); foreach ($xml->children () as $layer _one) {print_r ($layer _one);/* $    Layer_one is actually an array, the array element is the node object of the layer here is the three PHP node */echo "<br>";        foreach ($layer _one->children () as $layer _two) {print_r ($layer _two);    echo "<br>"; }}

=============================================

Traverse all properties of a node

header ("Content-type:text/html;charset=utf-8"); $xml  =  Simplexml_load_file ("Book.xml");//print_r ($xml); foreach ($xml->children ()  as  $layer _one) {     foreach ($layer _one->attributes ()  as  $name + $value) {         echo  "$name  ======>  $value";         echo  "<br>";         foreach ($layer _one-> Children ()  as  $layer _two) {             foreach ($layer _two->attributes ()  as  $name 1=> $value 1) {              echo  "$name 1 =======> $value 1";             }        }     }} 

Run results

650) this.width=650; "Src=" Http://s3.51cto.com/wyfs02/M00/88/47/wKiom1fs9BHBN_dLAAAQSjN6WMU116.jpg-wh_500x0-wm_3 -wmp_4-s_2336428594.jpg "title=" 1.jpg "alt=" Wkiom1fs9bhbn_dlaaaqsjn6wmu116.jpg-wh_50 "/>

==========================================

Accessing elements and attributes of a specific node

<?phpheader ("Content-type:text/html;charset=utf-8"); $xml = Simplexml_load_file ("Book.xml"); Echo $xml ["Name"];/ * Access the value of the root node property name */$layer _one = $xml->children (); Echo $layer _one[0]->bookname. " <br> "Echo $layer _one[0]->price."         <br> "; /* Get the value of child node price */echo $layer _one[0]->price["unit"]. " <br> */* Get the value of the child Node price unit property */echo $layer _one[0]->publishdate. " <br> "; Echo count ($layer _one); /* 3 */

650) this.width=650; "Src=" Http://s2.51cto.com/wyfs02/M00/88/43/wKioL1fs-M3DhgJ8AAARWdMeRac596.jpg-wh_500x0-wm_3 -wmp_4-s_3776936335.jpg "title=" 1.jpg "alt=" Wkiol1fs-m3dhgj8aaarwdmerac596.jpg-wh_50 "/>

========================================================

Modify the contents of an XML file

<?phpheader ("Content-type:text/html;charset=utf-8"); $xml = Simplexml_load_file ("Book.xml"); $layer _one = $xml- >children (); $layer _one[0]->bookname = "The Path of PHP master";//print_r ($layer _one); $modify = $xml->asxml (); /* format is an XML object */file_put_contents ("Book.xml", $modify);/* Writes the modification to the XML file */$str = file_get_contents ("Book.xml"); Echo $str;

====================================================

In addition, we can use the DOM class library to traverse an XML document, which iterates through the XML document through a tree-structured pattern. Using the DOM variable document

The advantage is that you don't need a tag to display everything, but the downside is that it consumes memory very much.

So how do you parse an XML document?

<?phpheader ("Content-type:text/html;charset=utf-8"); $dom = new DOMDocument; /* Initialize the DOMDocument object */$dom->load ("Book.xml");/* Load book.xml Note, do not use Loadxml () */$names = $dom getElementsByTagName ("BookName");/* Get Domnode list */foreach ($names as $name) {/* traverse domnode*/print $name by tag name NodeValue. " <br> ";}


This article is from the "thick Product Thin Hair" blog, please make sure to keep this source http://joedlut.blog.51cto.com/6570198/1857896

Getting Started with PHP (+ PHP) and XML

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.