Introduction to XML
XML is a popular semi-structured file format that stores data in a database-like format. In practical applications, some simple, low-security data is often stored in the format of an XML file. The benefits of this can be achieved by reducing the interaction with the database to improve the reading efficiency, on the other hand can effectively use the advantages of XML to reduce the programming difficulty.
PHP provides a complete set of methods for reading XML files, and it is easy to write an XML-based script program. This chapter will introduce the operation methods of PHP and XML, and make a brief introduction to several commonly used XML class libraries.
1 Introduction to XML
XML is an abbreviation for "Extensible Identity Language (extensible Markup Language)" and is a markup language similar to HTML. However, unlike HTML, XML is primarily used to describe data and hold data, while HTML is primarily used to display data.
PHP Operation XML
1 <?XML version= "1.0" encoding= "Utf-8"?>2 <!--edited with XML Spy v2007 (http://www.altova.com) -3 <Breakfast_menu>4 < Food>5 <name>Belgian Waffles</name>6 < Price>$5.95</ Price>7 <Description>Famous Belgian Waffles with plenty of real maple syrup</Description>8 <calories>650</calories>9 </ Food>Ten < Food> One <name>Strawberry Belgian Waffles</name> A < Price>$7.95</ Price> - <Description>Light Belgian waffles covered with strawberries and whipped cream</Description> - <calories>900</calories> the </ Food> - < Food> - <name>Berry-berry Belgian Waffles</name> - < Price>$8.95</ Price> + <Description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</Description> - <calories>900</calories> + </ Food> A < Food> at <name>French Toast</name> - < Price>$4.50</ Price> - <Description>Thick slices made from our homemade sourdough bread</Description> - <calories>600</calories> - </ Food> - < Food> in <name>Homestyle Breakfast</name> - < Price>$6.95</ Price> to <Description>Eggs, bacon or sausage, toast, and our ever-popular hash browns</Description> + <calories>950</calories> - </ Food> the </Breakfast_menu>
Simple.xml file
1. Create a SimpleXML object
$xml=simplexml_load_file(' simple.xml ', ' simplexmlelement ');
2. Read the XML content
foreach ($xmlas$v) { echo$v->name. ---". $v $v->price. " <br/> ";}
3. Adding nodes
1 $xml->addchild ("name", "value");
4. Add Node Properties
$son=$xnl->addchild ("name", "value"); $son->addattibute ("name", "value"); // Name denotes property name value represents the property value
5. Updating the XML file
$content=$xml-SaveXML (); $fp=fopen(' simple.xml ', ' wb+ '); fwrite ($fp,$content); fclose ($fp);
PHP simplexml Operation XML Usage