Php SimpleXML Method for reading and writing XML interface file instance Parsing

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.