What we want to discuss with you today is about
Xml has become the trend of data storage and transmission. From the popular Ajax last year, it has a method (ResponseXML) to return Xml objects, then Javascript is used to process the Xml object.
Obviously, PHP, the powerful programming language on the server, needs more support from this technology. Compared with popular ones, Xml Parse, XmlReader, Xml Rpc... This is not the case here, but a PHP simplexml already integrated. It is easy to use and does not need to load other files or related configurations. Of course, this is what I use most. I know more about it than other php xml parsing classes.
Suppose we have a file like this,
The XML code for php xml processing is as follows:
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Phplamp>
- <Post>
- <Title id = "1"> php xml processing Overview 1 </title>
- <Details> details 1 </details>
- </Post>
- <Post>
- <Title id = "2"> php xml processing Introduction 2 </title>
- <Details> details 2 </details>
- </Post>
- <Post>
- <Title id = "3"> php xml processing Introduction 3 </title>
- <Details> details 3 </details>
- </Post>
- </Phplamp>
Now we need to use the PHP simplexml method to parse all its data.
Php xml processing PHP code is as follows
- <? Php
- /**
- * Load Xml files
- */
- $ Xml = simplexml_load_file ("me. xml ");
- /**
- * If Xml is a string, you can use the following
Method.
- * $ Xml = simplexml_load_string
- */
- /**
- * Traverse $ xml objects
- */
- Foreach ($ xml as $ key => $ value ){
- // Get attributes
- $ Attr = $ value-> title-> attributes ();
- Echo "Id:". $ attr ['id']. "<br/> ";
- Echo "Title:". $ value-> title. "<br/> ";
- Echo "Details:". $ value-> details
. "<Br/> ";
- }
- ?>
The preceding section describes how to use PHP simplexml.