DOMDocument is a self-contained function in php. next I will introduce how to use DOMDocument to generate xml documents and how to parse xml files. if you need it, refer to it. array real... DOMDocument is a self-contained function in php. next I will introduce how to use DOMDocument to generate xml documents and how to parse xml files. if you need it, refer to it.
The array instance code is as follows:
$row['name'], 'sex' => $row['sex'], 'age' => $row['age']);?>
Generate php with the following code:
FormatOutput = true; $ r = $ doc-> createElement ("root"); $ doc-> appendChild ($ r); foreach ($ arr as $ dat) {$ B = $ doc-> createElement ("data"); $ name = $ doc-> createElement ("name "); $ name-> appendChild ($ doc-> createTextNode ($ dat ['name']); $ B-> appendChild ($ name ); $ sex = $ doc-> createElement ("sex"); $ sex-> appendChild ($ doc-> createTextNode ($ dat ['sex']); $ B-> appendChild ($ sex); $ age = $ doc-> createElement ("age"); $ ag E-> appendChild ($ doc-> createTextNode ($ dat ['age']); $ B-> appendChild ($ age ); $ r-> appendChild ($ B);} echo $ doc-> saveXML ();?>
In this way, an xml file is generated.
Parsing xml files is also very simple. the code is as follows:
Open ($ url); // get xml $ I = 1; while ($ reader-> read () {if ($ reader-> nodeType = XMLReader: TEXT) {// Determine the node type $ m = $ I % 3; if ($ m = 1) $ name = $ reader-> value; // read the node value if ($ m = 2) $ sex = $ reader-> value; if ($ m = 0) {$ age = $ reader-> value; $ arr [] = array ('name' => $ name, 'sex' => $ sex, 'age' => $ age) ;}$ I ++ ;}} // print_r ($ arr);?>
Article address:
Reprint at will ^ please include the address of this article!