Nesting time (Nesting Time)
The previous example is only used to explain the problem. If you really want to insert the RDF content into the Web site, you need to make things better. Therefore, the previous script has been improved, and some things have been added to simplify the task of formatting RDF data.
hEAD>
New Releases on FreshMeat.net Today: b> td>
TR> PHP // xml file $ file = "http://www.freshmeat.net/backend/fm-releases.rdf"; // SET UP Some Variables for Use by the Parser $ CurrentTag = "" $ FLAG = ""; $ count = 0; // this is an associative array of channel data with keys ("title", "LINK", "description") $ CHANNEL = array (); // this is an array of arrays, with each array element representing an // with keys ("Title", "LINK", "Description") $ items = array (); // Opening Tag Handler Function ElementBegin ($ PARSER, $ Name, $ Attributes) { Global $ CURRENTTAG, $ FLAG; $ currenttag = $ name; // set flag if entering IF ($ Name == "Item") { $ FLAG = 1; } Else IF ($ name == "channel") { $ FLAG = 2; } } // Closing Tag Handler Function Elementend ($ Parser, $ Name) { Global $ CURRENTTAG, $ FLAG, $ COUNT $ CurrentTag = "" // set flag if exitation IF ($ Name == "Item") { $ count ; $ FLAG = 0; } Else IF ($ name == "channel") { $ FLAG = 0;} } // Character Data Handler Function CharacterData ($ Parser, $ DATA) { GLOBAL $ Currenttag, $ FLAG, $ Items, $ COUNT, $ CHANNEL $ DATA = Trim (HTMLSpecialchars ($ data)); IF ($ currittag == "title" || $ currenttag == "link" || $ currentTAG == "Description") { // add data to $ channels [] or $ items [] array IF ($ FLAG == 1) { $ Items [$ COUNT] [STRTOLOWER ($ CurrentTag)]. = $ DATA; } ELSE IF ($ FLAG == 2) { $ CHANNEL [STRTOLOWER ($ currentTAG)]. = $ data; } } } // Create Parser $ XP = XML_PARSER_CREATE (); // set Element Handler XML_SET_ELEMENT_HANDLER ($ XP, "ElementBegin", "Elementend"); XML_SET_CHARACTER_DATA_HANDLER ($ XP, "CharacterData"); XML_PARSER_SET_OPTION ($ XP, XML_OPTION_CASE_FOLDING, TRUE); XML_Parser_Set_Option ($ XP, XML_OPTION_SKIP_WHITE, TRUE); // read XML File IF ($ fp = fopen ($ file, "r")))))))) { DIE ("Could Not Read $ File"); } // Parse Data While ($ XML = FREAD ($ FP, 4096)) { IF (! XML_PARSE ($ XP, $ XML, Feof ($ FP))))) { DIE ("XML Parser Error:". XML_ERROR_STRING (XML_GET_ERROR_CODE ($ XP))); } } // Destroy Parser XML_PARSER_FREE ($ XP); // Now Iterate Through $ items [] Array // and print each item as a Table Row Foreach ($ Items as $ Item) { Echo " |
". $ item ["title"].
" ". $ item ["description"]. " td> tr>"; ?> TABLE> body> html> The main difference from the previous paragraph is that this script creates two arrays to save the information extracted during the analysis process. Among them, $ channel is Associative Array, which stores basic description information of the processed channel, and $ Items are a two-dimensional array containing information about a separate channel entry (Channel Intems). Each element in the $ items array itself is a combined array, contains Title, URL, and Description keywords. The total number of elements in the items array is the same as the total number of Once the document is analyzed, things are simple - traversing the $ items array, print each of the items (Item) in the form. The travel results are as follows:
转载请注明原文地址:https://www.9cbs.com/read-27959.html New Post(0)
|