Although PHP has provided XML support in earlier versions, this support has been greatly enhanced with the emergence of PhP5. PhP4 has limited support for XML. For example, only the sax-based parser and PhP4 Dom are provided by default, and the W3C standard is not implemented for PhP5, it can be said that php xml developers re-invent the wheel and comply with common standards.
PHP 5 adds XML features
PhP5 includes thoroughly rewritten and new extensions, such as the SAX Parser, Dom, simplexml, xmlreader, xmlwriter, and XSLT processing programs. All these extensions are based on libxml2.
In addition to the improved sax support from PhP4, PhP5 also supports Dom and simplexml extensions that comply with W3C standards. By default, both sax, Dom, and simplexml are supported. If you are familiar with Dom in other languages, it is easier to use PHP to implement similar functions.
PHP has many ways to read XML files. The following is a brief introduction.
First: Use dom
Read:
First, create an XML document test. XML for testing. The content is as follows:
<? XML version = "1.0"?>
<Books>
<Book>
<Title> PHP advanced programming </title>
<Price> 65.32 </price>
</Book>
<Book>
<Title> PHP and MySQL advanced programming </title>
<Price> 59.6 </price>
</Book>
</Books>
Create the PHP file test. php to read the XML file. The content is as follows:
$ Doc = new domdocument ();
$ Doc-> load ('/document path/books. xml ');
$ Books = $ doc-> getelementsbytagname ('book ');
Foreach ($ books as $ book ){
$ Titles = $ book-> getelementsbytagname ('title ');
$ Title = $ titles-> item (0)-> nodevalue;
$ Prices = $ book-> getelementsbytagname ('price ');
$ Price = $ prices-> item (0)-> nodevalue;
Echo $ title. "-----". $ price. "/N ";
}
In this way, PHP uses Dom to read XML documents and output results.
Create:
Create the array data in PHP as the XML document format. The Code is as follows:
<? PHP
$ Books = array ();
$ Books [] = array (
'Title' => 'php hacks ',
'Author' => 'Jack herrington ',
'Her her' => "O 'Reilly"
);
$ Books [] = array (
'Title' => 'podcasting hacks ',
'Author' => 'Jack herrington ',
'Her her' => "O 'Reilly"
);
$ Doc = new domdocument ();
$ Doc-> formatoutput = true;
$ R = $ doc-> createelement ("books ");
$ Doc-> appendchild ($ R );
Foreach ($ books as $ book)
{
$ B = $ doc-> createelement ("book ");
$ Author = $ doc-> createelement ("author ");
$ Author-> appendchild (
$ Doc-> createtextnode ($ book ['author'])
);
$ B-> appendchild ($ author );
$ Title = $ doc-> createelement ("title ");
$ Title-> appendchild (
$ Doc-> createtextnode ($ book ['title'])
);
$ B-> appendchild ($ title );
$ Publisher = $ doc-> createelement ("publisher ");
$ Publisher-> appendchild (
$ Doc-> createtextnode ($ book ['her her'])
);
$ B-> appendchild ($ publisher );
$ R-> appendchild ($ B );
}
$ Doc-> Save ('createxml. xml ');
The XML document content is stored in the createxml. xml file.
Method 2: Use simplexml
We chose simplexml extension to parse the XML document. Simplexml extensions include the interoperability with Dom for compiling XML files and built-in XPath support. Simplexml is easier to use than Dom, as the name implies
Read:
In the first example, we changed the content of the test. php file:
$ Doc = new domdocument ();
$ Doc-> load ('books. xml ');
If (! $ DOC ){
Echo "error ";
Exit (0 );
}
$ Simplexml = simplexml_import_dom ($ DOC );
Echo $ simplexml-> book [1]-> title;
Use simplexml to read the Dom and then output the result.
To be continued ......