Using php to dynamically generate xml files and extract data from xml files into html applets _ PHP Tutorial

Source: Internet
Author: User
Small programs that use php to dynamically generate xml files and extract data from xml files into html. This stuff took me a day... However, at the end, I found that I could not achieve the effect I imagined... It's a pity... It seems that php (as the mainstream development language) has been around for me for a day... However, at the end, I found that I could not achieve the effect I imagined... It's a pity... It seems that php (as the mainstream development language) is more and more important to xml (standardization is getting closer and closer... If anyone has research on this. Thank you for writing to us...

Create a table first:
Create table books (
Bookid int (4) not null auto_increment,
Bookname varchar (100) not null,
Bookauth varchar (50) not null,
Bookpublisher varchar (50) not null,
Bookpubdate datetime not null,
Bookurl varchar (50) not null,
KEY bookid (bookid ),
);

Then... This section is the source code for converting data from MySQL (the best combination with PHP) to xml (standardization is getting closer and closer:
$ Connect_id = MySQL (the best combination with PHP) _ connect ("localhost", "root ","");
MySQL (best combination with PHP) _ select_db ("bbs", $ connect_id );
$ Query = "select * from books order by bookid ";
$ Rs = MySQL (the best combination with PHP) _ query ($ query, $ connect_id );
$ Numfields = MySQL (the best combination with PHP) _ num_fields ($ rs );
$ Xml (standardization is getting closer and closer) file =" N ";
$ Xml (standardization is getting closer and closer) file. =" N ";

While ($ row = MySQL (the best combination with PHP) _ fetch_array ($ rs )){
For ($ I = 0; $ I <$ numfields; $ I ){
$ Fieldname = MySQL (the best combination with PHP) _ field_name ($ rs, $ I );
$ Xml (standardization is getting closer and closer) file. = "<". $ fieldname. ">". $ row [$ I]." N ";
}
}
MySQL (the best combination with PHP) _ free_result ($ rs );
MySQL (the best combination with PHP) _ close ($ connect_id );
$ Xml (standardization is getting closer and closer) file. ="N ";
$ Fp = fopen ("xml (standardization is getting closer and closer) doc/xml (standardization is getting closer and closer) doc. xml (standardization is getting closer and closer)", "w ");
If (fwrite ($ fp, $ xml (standardization is getting closer and closer) file )){
Echo "file written! ";
}
Else {
Echo "failed to write file! ";
}
?>

This section is the source code for converting data from xml (standardization is getting closer and closer) to HTML...
Class xml (standardization is getting closer and closer ){
Var $ parser;

Function xml (standardization is getting closer and closer )(){
$ This-> parser = xml (standardization is getting closer and closer) _ parser_create ();
Xml (standardization is getting closer and closer) _ set_object ($ this-> parser, & $ this );
Xml (standardization is getting closer and closer) _ set_element_handler ($ this-> parser, "tag_on", "tag_off ");
Xml (standardization is getting closer and closer) _ set_character_data_handler ($ this-> parser, "cdata ");
}

Function parse ($ data ){
Xml (standardization is getting closer and closer) _ parse ($ this-> parser, $ data );
}

Function tag_on ($ parser, $ tag, $ attributes ){
If (xml (standardization is getting closer and closer) _ get_current_line_number ($ parser) = 2 ){
Echo"". $ Tag ."";
}
Else {
Switch (xml (standardization is getting closer and closer) _ get_current_line_number ($ parser)-2) % 6 ){
Case 0:
Echo"Download";
Break;
Case 1:
Echo"ID number";
Break;
Case 2:
Echo"Title";
Break;
Case 3:
Echo"Author";
Break;
Case 4:
Echo"Press";
Break;
Case 5:
Echo"Publication date";
Break;
}
}
}

Function cdata ($ parser, $ cdata ){
Echo $ cdata;
}

Function tag_off ($ parser, $ tag ){
Echo "n ";
}
}

$ Xml (standardization is getting closer and closer) _ parser = new xml (standardization is getting closer and closer )();
$ Xml (standardization is getting closer and closer) filename = "xml (standardization is getting closer and closer) doc/xml (standardization is getting closer and closer) doc. xml (standardization is getting closer and closer )";
$ Fp = fopen ($ xml (standardization is getting closer and closer) filename, "r ");
$ Xml (standardization is getting closer and closer) data = fread ($ fp, filesize ($ xml (standardization is getting closer and closer) filename ));
?>
Book Information




$ Xml (standardization is getting closer and closer) _ parser-> parse ($ xml (standardization is getting closer and closer) data );?>

Actually, you can see that .. This example is a failure... Because it is not very effective .. You cannot get what you want... Alas... Bitter ....

Bytes... However, at the end, I found that I could not achieve the effect I imagined... It's a pity... It seems that php (as the mainstream development language) is...

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.