In php5, It is very convenient to read and write xml documents. You can directly use the SimpleXML method of php to quickly parse and generate files in xml format. The following is an example:
There are three methods to create a SimpleXML object:
1.Create with the new Keyword
Copy codeThe Code is as follows:
$ Xml = "<personinfo> <item> <id> 1 </id> <name> aaa </name> <age> 16 </age> </item>
<Item> <id> 2 </id> <name> bbb </name> <age> 26 </age> </item> </personinfo> ";
$ Rss = new SimpleXMLElement ($ xml );
2.Create with simplexml_load_string ()
Copy codeThe Code is as follows:
$ Xml = "<personinfo> <item> <id> 1 </id> <name> aaa </name> <age> 16 </age> </item>
<Item> <id> 2 </id> <name> bbb </name> <age> 26 </age> </item> </personinfo> ";
$ Rss = simplexml_load_string ($ xml );
3.Use simplexml_load_file () to create a URL
Copy codeThe Code is as follows:
$ Rss = simplexml_load_file ("rss. xml ");
// Or:
$ Rss = simplexml_load_file ("/rss. xml"); // remote documentation
The specific example is as follows:
Copy codeThe Code is as follows:
<? Php
$ Xml = "<personinfo> <item> <id> 1 </id> <name> aaa </name> <age> 16 </age> </item> <item> <id> 2 </id> <name> bbb </name> <age> 26 </age> </item> </personinfo> ";
$ Rss = new SimpleXMLElement ($ xml );
Foreach ($ rss-> item as $ v ){
Echo $ v-> name, '<br/> ';
}
Echo $ rss-> item [1]-> age; // read data
Echo '$ Rss-> item [1]-> name = 'ccc '; // modify data
Foreach ($ rss-> item as $ v ){
Echo $ v-> name, '<br/>'; // aaa <br/> ccc <br/>
}
Echo 'Unset ($ rss-> item [1]); // output data
Foreach ($ rss-> item as $ k => $ v ){
Echo $ v-> name, '<br/>'; // aaa <br/>
}
Echo '// Add data
$ Item = $ rss-> addChild ('item ');
$ Item-> addChild ('id', '3 ');
$ Item-> addChild ('name', 'ccc _ new ');
$ Item-> addChild ('age', '40 ');
Foreach ($ rss-> item as $ k => $ v ){
Echo $ v-> name, '<br/>'; // aaa <br/> ccc_new <br/>
}
$ Rss-> asXML ('personinfo. xml ');
?>
The above example is further analyzed as follows:
Copy codeThe Code is as follows:
// Read xml data
// You can directly access a specific element through the element name. All elements in the document are considered as attributes of the object.
Foreach ($ rss-> item as $ v ){
Echo $ v-> name, '<br/>'; // aaa <br/> bbb <br/>
}
Echo $ rss-> item [1]-> age; // 26
// Modify xml data. You can directly use the object attribute Assignment Method to directly edit the content of an element.
$ Rss-> item [1]-> name = 'ccc '; // modify data
Foreach ($ rss-> item as $ v ){
Echo $ v-> name, '<br/>'; // aaa <br/> ccc <br/>
}
// You can use the php Content Function unset to delete an element from the tree.
Unset ($ rss-> item [1]);
Foreach ($ rss-> item as $ v ){
Echo $ v-> name, '<br/>'; // a www.jb51.net aa <br/>
}
// Add element data in xml, which can be implemented through the addChild method of the object
$ Item = $ rss-> addChild ('item ');
$ Item-> addChild ('id', '3 ');
$ Item-> addChild ('name', 'ccc _ new ');
$ Item-> addChild ('age', '40 ');
Foreach ($ rss-> item as $ k => $ v ){
Echo $ v-> name, '<br/>'; // aaa <br/> ccc_new <br/>
}
// Store xml data
// Use the asXML () method of the object
$ Rss-> asXML ('personinfo. xml'); // store xml data in the personinfo. xml file.