First, the concept of XML
XML Extensible Markup Language (extensible Markup Language) is a platform-independent way to represent data
Markup Language: A computer text encoding that combines text and other text-related information to show the structure of the document and the details of the data processing.
Ii. The role and rules of XML
How easy it is to exchange data between different business systems, so developers take pains. There are two common ways of storing data: databases and files. If you use database operations then you need a consolidated database, the process is cumbersome.
All can not use a file to do, can not find a data structure to maintain a file to hold the information, the advent ofXML so that we have a good solution.
Compare and analyze XML,HTML ,HTTP Concepts
Iii. XML role: Storing and transmitting data
<?xml version= "1.0"?>
<books>
<book no= "1" >
<author> serwas </author>
<title>php WEB 2.0 Development Combat </title>
<introduce> Main Introduction zendframework, author code worth learning </introduce>
</book>
<book no= "2" >
<author> MacArthur </author>
<title>php Advanced Programming: Patterns, frameworks and testing </title>
<introduce> in- depth design mode,PHP Standard library and json</introduce>
</book>
</books>
Role:
Sharing and interacting data across different systems
Data is structured and readable
Iv. reading data from XML
1. DOM Way to read XML documents
Xml File:
Stu.xml
<?xml version= "1.0" encoding= "UTF-8"?>
<students>
<student no= "1" >
<name>zhangsan</name>
<age>20</age>
</student>
<student no= "2" >
<name>lisi</name>
<age>21</age>
</student>
</students>
PHP Program
<?php
creating dom objects
$dom =new DOMDocument ();
loading an XML file
$dom->load ("Stu.xml");
reads the outermost node into the
$students = $dom->getelementsbytagname ("students");
Traverse
foreach ($students as $key = = $val) {
read the next layer of nodes
$stu = $val->getelementsbytagname ("student");
traverse the next layer of nodes
foreach ($stu as $k = = $v) {
get name Node Object
$name = $v->getelementsbytagname ("name");
gets the value of the node object
$na = $name->item (0)->nodevalue;
get The age node
$age = $v->getelementsbytagname ("Age");
gets the value of the node
$ag = $age->item (0)->nodevalue;
}
}
?>
V.PHP Create XML file
Create XML:
Declaring DOM objects:new DOMDocument ()
Create node:createelement ()
Assigning values to child nodes
Create text node:createtextnode ()
Add child node:appendchild ()
Create node Properties : CreateAttribute ()
Assigning values to attribute nodes
Create a <![containing cdata[ node for data ]]> :createcdatasection
Example:
1. Querying data from a database to get an array of data
2.iterate through the data in the array
3.Create a node during the loop and store the data data
4. Save the XML as a file
Attention:
Generating an XML file using DOMDocument
Create nodes using the CreateElement method,
Create text content using the createTextNode method,
Add child nodes using the AppendChild method,
To create an attribute using the CreateAttribute method
Example:
<? Php
$data _array = Array (
Array
' Title ' = ' Title1 ',
' Content ' = ' content1 ',
' pubdate ' = ' 2014/9/2 ',
),
Array
' Title ' = ' Title2 ',
' Content ' = ' content2 ',
' pubdate ' = ' 2014/9/2 ',
)
);
Property Array
$attribute _array = Array (
' title ' = = Array (
' Size ' = 1
)
);
Create an XML document and set the XML version and encoding.
$dom =new DomDocument (' 1.0 ', ' utf-8 ');
Creating the root node
$article = $dom->createelement (' article ');
$dom->appendchild ($article);
foreach ($data _array as $data) {
$item = $dom->createelement (' item ');
$article->appendchild ($item);
Create_item ($dom, $item, $data, $attribute _array);
}
output the generated XML file
echo $dom->savexml ();
Save on disk
$dom->save ("Order.xml");
function Create_item ($dom, $item, $data, $attribute) {
if (Is_array ($data)) {
foreach ($data as $key = = $val) {
Creating Elements
$ $key = $dom->createelement ($key);
$item->appendchild ($ $key);
Creating element Values
$text = $dom->createtextnode ($val);
$ $key->appendchild ($text);
if (Isset ($attribute [$key])) {
if there are related attributes in this field, you need to set
foreach ($attribute [$key] as $akey = = $row) {
To Create an attribute node
$ $akey = $dom->createattribute ($akey);
$ $key->appendchild ($ $akey);
Creating attribute value nodes
$aval = $dom->createtextnode ($row);
$ $akey->appendchild ($aval);
}
}//End If
}
}//End If
}//End Function
?>
The concept, role, and trial of XML