How to use PHP to insert RDF in the Web site (2)

zhaozj2021-02-16  55

Fresh meat

Since it is technically, RSS is a well-structured XML document, so it can be handled with standard XML programming technology. There are two main technologies: SAX (The Simple API for XML) and DOM (The Document Object Model).

The SAX analyzer traverses the entire XML document while encountering a specific function when you encounter a tag that does not have the type. For example, call a specific function to process a start tag, call another function to process an end tag, then call a function to process data between the two. The duties of the analyzer are just the order traversing this document. The function it calls is responsible for processing the discovery mark. Once a tag is processed, the analyzer continues to analyze the next element in the document, which is constantly repeating.

On the other hand, the DOM analyzer works to read the entire XML document into memory and convert it into a hierarchical tree structure. Moreover, the API is provided for accessing different tree nodes (and the content attached to the node). The recursive processing method plus the API function allows developers to distinguish between different types of nodes (elements, attributes, character data, annotations, etc.), while performing different operations based on the node type and node depth of the document tree.

SAX and DOM analyzers support every language, including your favorite - php. I will use PHP's SAX analyzer to process RDF examples in this article. Of course, it is also easy to use the DOM analyzer.

Let us look at this simple example and remember it in your mind. Below is an RDF file I will use, this file is directly selected from http://www.freshmeat.net/:

XMLns = "http://purl.org/rss/1.0/"

XMLns: DC = "http://purl.org/dc/elements/1.1/"

>

Freshmeat.net </ Title></p> <p><link> http://freshmeat.net/ </ link></p> <p><Description> FreshMeat.Net Maintains The Web's Largest Index of Unix</p> <p>And Cross-Platform Open Source Software. THOUSANDS OF Applications Are</p> <p>Meticulously Catalog in the Freshmeat.Net Database, And Links to New</p> <p>Code Are Added Daily. </ description></p> <p><DC: Language> EN-US </ dc: Language></p> <p><DC: Subject> Technology </ dc: Subject></p> <p><DC: Publisher> Freshmeat.net </ dc: publisher></p> <p><dc: creator> Freshmeat.Net Contributors </ dc: creator></p> <p><DC: Rights> Copyright (C) 1997-2002 OSDN </ dc: Rights></p> <p><DC: DATE> 2002-02-11T10: 20 00: 00 </ dc: Date> <items></p> <p><RDF: SEQ></p> <p><rdf: li rdf: resource = "http://freshmeat.net/releases/69583/" /></p> <p><rdf: li rdf: resource = "http://freshmeat.net/releases/69581/" /></p> <p><! - and so -></p> <p></ rdf: SEQ></p> <p></ items></p> <p><image rdf: resource = "http://freshmeat.net/img/fmii-button.gif" /></p> <p><textinput rdf: resource = "http://freshmeat.net/search/" /></p> <p></ CHANNEL></p> <p><image rdf: About = "http://freshmeat.net/img/fmii-button.gif"></p> <p><title> Freshmeat.net </ Title></p> <p><URL> http://freshmeat.net/img/fmii-button.gif </ url></p> <p><link> http://freshmeat.net/ </ link></p> <p></ iMAGE></p> <p><item rdf: About = "http://freshmeat.net/releases/69583/>"></p> <p><title> sloop.splitter 0.2.1 </ title></p> <p><link> http://freshmeat.net/releases/69583/ </ link></p> <p><Description> A Real Time Sound Effects Program. </ description></p> <p><DC: DATE> 2002-02-11T04: 52-06: 00 </ dc: DATE></p> <p></ item></p> <p><item rdf: About = "http://freshmeat.net/releases/69581/>"></p> <p><title> Apacompile 1.9.9 </ title></p> <p><link> http://freshmeat.net/releases/69581/ </ link></p> <p><Description> a full-featured apache compiration howto. </ description></p> <p><DC: DATE> 2002-02-11T04: 52-06: 00 </ dc: DATE></p> <p></ item></p> <p><! - and so -></p> <p></ rdf: rdf></p> <p>Below is a PHP script that analyzing this document and displays data in it:</p> <p><? PHP</p> <p>// xml file</p> <p>$ FILE = "FM-Releases.rdf";</p> <p>// SET UP Some Variables for Use by the Parser</p> <p>$ CurrentTag = ""</p> <p>$ FLAG = "";</p> <p>// Create Parser</p> <p>$ XP = XML_PARSER_CREATE ();</p> <p>// set Element Handler</p> <p>XML_SET_ELEMENT_HANDLER ($ XP, "ElementBegin", "Elementend"); XML_SET_CHARACTER_DATA_HANDLER ($ XP, "CharacterData");</p> <p>XML_PARSER_SET_OPTION ($ XP, XML_OPTION_CASE_FOLDING, TRUE);</p> <p>// read XML File</p> <p>IF ($ fp = fopen ($ file, "r"))))))))</p> <p>{</p> <p>DIE ("Could Not Read $ File");</p> <p>}</p> <p>// Parse Data</p> <p>While ($ XML = FREAD ($ FP, 4096))</p> <p>{</p> <p>IF (! XML_PARSE ($ XP, $ XML, Feof ($ FP)))))</p> <p>{</p> <p>DIE ("XML Parser Error:".</p> <p>XML_ERROR_STRING (XML_GET_ERROR_CODE ($ XP)));</p> <p>}</p> <p>}</p> <p>// Destroy Parser</p> <p>XML_PARSER_FREE ($ XP);</p> <p>// Opening Tag Handler</p> <p>Function ElementBegin ($ PARSER, $ Name, $ Attributes)</p> <p>{</p> <p>Global $ CURRENTTAG, $ FLAG;</p> <p>// export the name of the current tag to the global scpe</p> <p>$ currenttag = $ name;</p> <p>// if Withn Item Block, Set A Flag</p> <p>IF ($ Name == "Item")</p> <p>{</p> <p>$ FLAG = 1;</p> <p>}</p> <p>}</p> <p>// Closing Tag Handler</p> <p>Function Elementend ($ Parser, $ Name)</p> <p>{</p> <p>Global $ CURRENTTAG, $ FLAG;</p> <p>$ CurrentTag = ""</p> <p>// if Exitation An Item Block, Print A Line and Reset The Flag</p> <p>IF ($ Name == "Item")</p> <p>{</p> <p>ECHO "<hr>";</p> <p>$ FLAG = 0;</p> <p>}</p> <p>}</p> <p>// Character Data Handler</p> <p>Function CharacterData ($ Parser, $ DATA)</p> <p>{</p> <p>Global $ CURRENTTAG, $ FLAG;</p> <p>// if Withn iTem Block, Print Item Data</p> <p>IF ($ currenttag == "title" || $ currenttag == "link" ||</p> <p>$ currentTAG ==</p> <p>"Description") && $ flag == 1)</p> <p>{</p> <p>Echo "$ CURRENTTAG: $ DATA <br>";</p> <p>}</p> <p>}</p> <p>?></p> <p>Do not understand? Don't worry, will be explained later.</p> <p>Capture flag</p> <p>This script must first do to set some global variables:</p> <p>// xml file</p> <p>$ FILE = "FM-Releases.rdf";</p> <p>// SET UP Some Variables for Use by the Parser</p> <p>$ CurrentTag = ""</p> <p>$ FLAG = "";</p> <p>$ CURRENTTAG Variable Save is the name of the elements of the analyzer. You will soon see why you need it.</p> <p>Because my ultimate goal is to display each individual entry (Item) in the channel and have a link. Also know when the analyzer exits the <channel> </ channel> block, and when I entered the <Item> </ ITEM> section of the document. Besides, I use the SAX analyzer, it works in order, without any analyzer API, can not be used to know the depth and location in the document tree. So, I have to invent a mechanism to do this - this is the reason for introducing a $ FLAG variable. The $ FLAG variable will be used to determine the analyzer in the <channel> block or in the <Item> block.</p> <p>The next step is to initialize the SAX analyzer and start analyzing the RSS document.</p> <p>// Create Parser</p> <p>$ XP = XML_PARSER_CREATE ();</p> <p>// set Element Handler</p> <p>XML_SET_ELEMENT_HANDLER ($ XP, "ElementBegin", "Elementend");</p> <p>XML_SET_CHARACTER_DATA_HANDLER ($ XP, "CharacterData");</p> <p>XML_PARSER_SET_OPTION ($ XP, XML_OPTION_CASE_FOLDING, TRUE);</p> <p>// read XML File</p> <p>IF ($ fp = fopen ($ file, "r"))))))))</p> <p>{</p> <p>DIE ("Could Not Read $ File");</p> <p>}</p> <p>// Parse Data</p> <p>While ($ XML = FREAD ($ FP, 4096))</p> <p>{</p> <p>IF (! XML_PARSE ($ XP, $ XML, Feof ($ FP)))))</p> <p>{</p> <p>DIE ("XML Parser Error:".</p> <p>XML_ERROR_STRING (XML_GET_ERROR_CODE ($ XP)));</p> <p>}</p> <p>}</p> <p>// Destroy Parser</p> <p>XML_PARSER_FREE ($ XP);</p> <p>This code is simple, and the comments have been explained enough. The XML_PARSER_CREATE () function creates an analyzer instance and assigns it to the handle $ XP. Then create a backup function to process the on-tag and closed mark, and the character data between the two. Finally, the XML_PARSE () function combines the FREAD () call to read the RDF file and analyze it.</p> <p>In the documentation, each time you encounter a bilus, you will be called by ELEMENTBEGIN ().</p> <p>// Opening Tag Handler</p> <p>Function ElementBegin ($ PARSER, $ Name, $ Attributes)</p> <p>{</p> <p>Global $ CURRENTTAG, $ FLAG;</p> <p>// export the name of the current tag to the global scpe</p> <p>$ currenttag = $ name;</p> <p>// if Withn Item Block, Set A Flag</p> <p>IF ($ Name == "Item")</p> <p>{</p> <p>$ FLAG = 1;</p> <p>}</p> <p>}</p> <p>This function takes parameters as the name and attribute of the current tag. The tag name is assigned to the global variable $ CURRENTTAG. If this is called <item>, then the $ FLAG variable is set.</p> <p>Similarly, if you encounter a closed mark, the closed mark processor ELEMENTENTENTEND () will be called.</p> <p>// Closing Tag Handler</p> <p>Function Elementend ($ Parser, $ Name)</p> <p>{</p> <p>Global $ CURRENTTAG, $ FLAG;</p> <p>$ currenttag = ""; // if exitation an item block, print a line and reset the flag</p> <p>IF ($ Name == "Item")</p> <p>{</p> <p>ECHO "<hr>";</p> <p>$ FLAG = 0;</p> <p>}</p> <p>}</p> <p>The closed tag handler is also used as its parameters with the marker name. If it is a closed mark for </ item>, the value of the variable $ FLAG is reset to 0 and the value of the variable $ CURRENTTAG is empty.</p> <p>So how do you handle character data between tags? This is our interest. Let's greessing the character data processor CharacterData () first.</p> <p>// Character Data Handler</p> <p>Function CharacterData ($ Parser, $ DATA)</p> <p>{</p> <p>Global $ CURRENTTAG, $ FLAG;</p> <p>// if Withn iTem Block, Print Item Data</p> <p>IF ($ currenttag == "title" || $ currenttag == "link" ||</p> <p>$ currentTAG ==</p> <p>"Description") && $ flag == 1)</p> <p>{</p> <p>Echo "$ CURRENTTAG: $ DATA <br>";</p> <p>}</p> <p>}</p> <p>Now you can see the parameters passing to this function, you will find that it only receives the number between the tag and the closed mark, and it does not know that the analyzer is currently being processed "tag. And this is the reason why we introduce global variable $ CURRENTTAG at first.</p> <p>If the value of the $ FLAG variable is 1, that is, if the analyzer is currently between the <Item> </ ITME> block, the currently processed element, regardless of <title>, <link> or <description>, The data is printed onto the output device (here, the output device is a web browser) and adds a newline character after the output of each element.</p> <p>The entire RDF document is handled in this order, and a certain output is displayed for each <Item> tag. You can take a look at the results below:</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-27960.html</div><div class="plugin d-flex justify-content-center mt-3"></div><hr><div class="row"><div class="col-lg-12 text-muted mt-2"><i class="icon-tags mr-2"></i><span class="badge border border-secondary mr-2"><h2 class="h6 mb-0 small"><a class="text-secondary" href="tag-2.html">9cbs</a></h2></span></div></div></div></div><div class="card card-postlist border-white shadow"><div class="card-body"><div class="card-title"><div class="d-flex justify-content-between"><div><b>New Post</b>(<span class="posts">0</span>) </div><div></div></div></div><ul class="postlist list-unstyled"> </ul></div></div><div class="d-none threadlist"><input type="checkbox" name="modtid" value="27960" checked /></div></div></div></div></div><footer class="text-muted small bg-dark py-4 mt-3" id="footer"><div class="container"><div class="row"><div class="col">CopyRight © 2020 All Rights Reserved </div><div class="col text-right">Processed: <b>0.037</b>, SQL: <b>9</b></div></div></div></footer><script src="./lang/en-us/lang.js?2.2.0"></script><script src="view/js/jquery.min.js?2.2.0"></script><script src="view/js/popper.min.js?2.2.0"></script><script src="view/js/bootstrap.min.js?2.2.0"></script><script src="view/js/xiuno.js?2.2.0"></script><script src="view/js/bootstrap-plugin.js?2.2.0"></script><script src="view/js/async.min.js?2.2.0"></script><script src="view/js/form.js?2.2.0"></script><script> var debug = DEBUG = 0; var url_rewrite_on = 1; var url_path = './'; var forumarr = {"1":"Tech"}; var fid = 1; var uid = 0; var gid = 0; xn.options.water_image_url = 'view/img/water-small.png'; </script><script src="view/js/wellcms.js?2.2.0"></script><a class="scroll-to-top rounded" href="javascript:void(0);"><i class="icon-angle-up"></i></a><a class="scroll-to-bottom rounded" href="javascript:void(0);" style="display: inline;"><i class="icon-angle-down"></i></a></body></html><script> var forum_url = 'list-1.html'; var safe_token = 'MnIyJxxxfzhC0Gj_2FuD2DSPedDCE_2ByldSPtXB4W9DNWN91w4Iy20seA1DrM_2BmfUZJ0_2FohugrzMrALaMhLoJXLyw_3D_3D'; var body = $('body'); body.on('submit', '#form', function() { var jthis = $(this); var jsubmit = jthis.find('#submit'); jthis.reset(); jsubmit.button('loading'); var postdata = jthis.serializeObject(); $.xpost(jthis.attr('action'), postdata, function(code, message) { if(code == 0) { location.reload(); } else { $.alert(message); jsubmit.button('reset'); } }); return false; }); function resize_image() { var jmessagelist = $('div.message'); var first_width = jmessagelist.width(); jmessagelist.each(function() { var jdiv = $(this); var maxwidth = jdiv.attr('isfirst') ? first_width : jdiv.width(); var jmessage_width = Math.min(jdiv.width(), maxwidth); jdiv.find('img, embed, iframe, video').each(function() { var jimg = $(this); var img_width = this.org_width; var img_height = this.org_height; if(!img_width) { var img_width = jimg.attr('width'); var img_height = jimg.attr('height'); this.org_width = img_width; this.org_height = img_height; } if(img_width > jmessage_width) { if(this.tagName == 'IMG') { jimg.width(jmessage_width); jimg.css('height', 'auto'); jimg.css('cursor', 'pointer'); jimg.on('click', function() { }); } else { jimg.width(jmessage_width); var height = (img_height / img_width) * jimg.width(); jimg.height(height); } } }); }); } function resize_table() { $('div.message').each(function() { var jdiv = $(this); jdiv.find('table').addClass('table').wrap('<div class="table-responsive"></div>'); }); } $(function() { resize_image(); resize_table(); $(window).on('resize', resize_image); }); var jmessage = $('#message'); jmessage.on('focus', function() {if(jmessage.t) { clearTimeout(jmessage.t); jmessage.t = null; } jmessage.css('height', '6rem'); }); jmessage.on('blur', function() {jmessage.t = setTimeout(function() { jmessage.css('height', '2.5rem');}, 1000); }); $('#nav li[data-active="fid-1"]').addClass('active'); </script>