Php implementation code for operating XML, reading and writing data, phpxml
Xml file
<? Xml version = "1.0" encoding = "UTF-8"?> <Vip> <id> 23 </id> <username> happy lufei </username> <sex> male </sex> <face> face/43.jpg</face> <email> 123@qq.com </email> <qq> 1212121212 </qq> </vip>
Php parses XML to get values in tags
/** _ Get_xml: The obtained XML file * @ access public indicates that the function is made public * @ param $ _ xmlfile: xml file * $ _ the data array retrieved from XML by html **/function _ get_xml ($ _ xmlfile) {$ _ html = array (); if (file_exists ($ _ xmlfile) {$ _ xml = file_get_contents ($ _ xmlfile); preg_match_all ('/<vip> (. *) <\/vip>/', $ _ xml, $ _ dom); foreach ($ _ dom [1] as $ _ value) {preg_match_all ('/<id> (. *) <\/id>/', $ _ value, $ _ id); preg_match_all ('/<username> (. *) <\/username>/', $ _ value, $ _ username); preg_match_all ('/<sex> (. *) <\/sex>/', $ _ value, $ _ sex); preg_match_all ('/<face> (. *) <\/face>/', $ _ value, $ _ face); preg_match_all ('/<email> (. *) <\/email>/', $ _ value, $ _ email); preg_match_all ('/<qq> (. *) <\/qq>/', $ _ value, $ _ qq); $ _ html ['id'] = $ _ id [1] [0]; $ _ html ['username'] = $ _ username [1] [0]; $ _ html ['sex'] = $ _ sex [1] [0]; $ _ html ['face'] =_ _ face [1] [0]; $ _ html ['email '] =$ _ email [1] [0]; $ _ html ['qq'] =$ _ qq [1] [0] ;}} else {_ alert_back ("file does not exist") ;}return $ _ html ;}
Php writes data to XML files
/** _ Set_xml write information to the XML file * @ access public indicates that the function is made public * @ param $ _ xmlfile xml file * @ param $ _ clean indicates the array of information to be written ** /function _ set_xml ($ _ xmlfile, $ _ clean) {$ _ fp = @ fopen ('newuser. xml', 'w'); if (! $ _ Fp) {exit ('System error, file does not exist! ');} Flock ($ _ fp, LOCK_EX); $ _ string = "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?> \ R \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string =" <vip> \ r \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string )); $ _ string = "\ t <id >{$ _ clean ['id']} </id> \ r \ t"; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string = "\ t <username >{$ _ clean ['username']} </username> \ r \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string )); $ _ string = "\ t <sex >{$ _ clean ['sex ']} </sex> \ r \ t"; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string = "\ t <face >{$ _ clean ['face']} </face> \ r \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string )); $ _ string = "\ t <email >{$ _ clean ['email ']} </email> \ r \ t"; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string = "\ t <qq >{$ _ clean ['url']} </qq> \ r \ t "; fwrite ($ _ fp, $ _ string, strlen ($ _ string); $ _ string = "</vip>"; fwrite ($ _ fp, $ _ string, strlen ($ _ string); flock ($ _ fp, LOCK_UN); fclose ($ _ fp );}
Use php to operate xml, read data, and insert data
I am looking for some information on the Internet, hoping to help you introduce the functions related to xml reading in php: Reference: ---------------------------------------------------------------------- description of object XML parsing functions.
Start and end of element xml_set_element_handler ()
Character data xml_set_character_data_handler () Start of character data
External entity xml_set_external_entity_ref_handler () external entity appears
External entity xml_set_unparsed_entity_decl_handler () not resolved external entity appears
Processing Command xml_set_processing_instruction_handler () Processing Command appears
The emergence of the xml_set_notation_decl_handler () method declaration
By default, xml_set_default_handler () is used to read xml data by using the parser function? Php
$ Parser = xml_parser_create (); // create a parser Editor
Xml_set_element_handler ($ parser, "startElement", "endElement"); // corresponding functions for tag setting triggering are startElement and endElenment respectively.
Xml_set_character_data_handler ($ parser, "characterData"); // you can specify a function for reading data.
$ Xml_file = "1.xml"; // specify the xml file to be read, which can be a url
$ Filehandler = fopen ($ xml_file, "r"); // open the file
While ($ data = fread ($ filehandler, 4096 ))
{
Xml_parse ($ parser, $ data, feof ($ filehandler ));
} // Extract 4096 bytes each time for fclose ($ filehandler );
Xml_parser_free ($ parser); // close and release the parser
$ Name = false;
$ Position = false;
Function startElement ($ parser_instance, $ element_name, $ attrs) // function of the start tag event
{
Global $ name, $ position;
If ($ element_name = "NAME ")
{
$ Name = true;
$ Position = false;
Echo "name :";
}
If ($ element_name = "POSITION ")
{$ Name = false;
$ Position = true;
Echo ...... remaining full text>
How to Use php to read data from the database and generate xml files
My idea is to directly use dynamic xml to allow flash to read this document, so that you do not need to generate xml files in real time. Of course, this xml file is. php format, so you must change the file address read in flash to php, just like writing a php page, the difference is that the output content of this PHP file is in xml format.
For example, if you create an xml. php file
<? Php
Echo "<? Xml version = \ "1.0 \" encoding = \ "UTF-8 \"?>
<Gallery>
<Settings> ";
// Call as needed if dynamic information is also provided here
Echo "</settings>
<Items> ";
// Loop your image data here
$ Data = ??
While ($ data ){
Echo "<item source = \"". $ data ['source']. "\" description = \"". $ data ['description']. "\"/> ";
}
Echo '</items> ';
?>