Use SimpleXML to process XML
When developing Web applications, in order to reduce the I/O overhead of reading and writing databases, some configuration information is often stored in a flat file such as XML and json, and then through the script language such as PHP, javascript to operate these files. PHP has two main methods to operate XML: DOM and SimpleXML. The two methods parse XML by analyzing the entire XML document and providing APIs to access elements in the tree. SimpleXML is a new feature of PHP 5, designed to easily complete some common XML processing tasks. The following is a simple example of how to format a group of XML data through SimpleXML.
-
-
-
-
- Belgian Waffles
- $5.95
- Two of our famous Belgian Waffles with plenty of real maple syrup
- 650
-
- Strawberry Belgian Waffles
- $7.95
- Light Belgian waffles covered with strawberries and whipped cream
- 900
-
- Berry-Berry Belgian Waffles
- $8.95
- Light Belgian waffles covered with an assortment of fresh berries and whipped cream
- 900
-
- Use simpleXML to process XML
- // Use simpleXML to process XML
- Foreach ($ child-> children () as $ item)
- }
- }
- $ Xml = simplexml_load_file ('./simple. XML ');
- // Var_dump ($ xml );
- // Echo $ xml-> getName ();
- // Var_dump ($ xml-> children ());
- $ Record = '';
- Foreach ($ xml-> children () as $ child)
- {
- $ Record. ='
- {
- // Var_dump ($ child );
- $ Record. ='
- $ Record. ='
- Echo $ record;
- ?>
|
Name |
Price |
Description |
Calories |
'. $ Child-> attributes ().' | ';
'. $ Item .' | ';
';
|