XML application in PHP (2)

xiaoxiao2021-03-06  76

XML application in PHP (2)

Source: How to parse the document? After completing all preparations, the script is now able to resolve the XML document:                                                                                               Analysis XML_PARSE (), like XML_PARSE_FROM_FILE (), when an error occurs, the format of the XML document is not complete, will return false. We can use the XML_GET_ERROR_CODE () function to get the last error number code. Pass this numeric code to the XML_ERROR_STRING () function to get the wrong text information. Output XML's current number of rows, making debugging easier. When parsing the document, it is necessary to emphasize the problem for Exppat: How to maintain the basic description of the document structure? As mentioned earlier, an event-based parser itself does not produce any structural information. However, the tag (TAG) structure is an important feature of XML. For example, elemental sequence means it means unlike <Figure> <title>. The book name and picture are there is no relationship, although they all use / "Title /". Therefore, in order to more effectively use an event-based parser processing XML, you must use your own stacks or list (LISTS) to maintain the structure information of the document. In order to generate a mirror of a document structure, the script needs to know the parent element of the current element. The API with Exapt is unable to report only the current elements, without any information on the front-rear relationship. Therefore, you need to build your own stack structure. The script example uses the stack structure of advanced subsequent (Filo). Through an array, the stack will save all the start elements. For start element processing functions, the current element will be pushed to the top of the stack by the array_push () function. Correspondingly, the end element processing function removes the top element by array_pop (). For sequence <book> <title> </ title> </ book>, the stack is filled as follows:   Start Element Book: Pass / "BOOK /" to give the first element ($ stack [0]). Start Elements Title: Assign / "Title /" to the top of the stack ($ stack [1]). End Elements Title: From the stack to remove the top elements ($ stack [1]). End Elements Title: From the stack to remove the top element ($ stack [0]). PHP3.0 manually controls the nested nested nested by a $ depth variable, which makes the script look complicated. PHP4.0 makes scripts more concise via Array_POP () and array_push (). How to collect element information in XML documentation? In order to collect information of each element, the script needs to remember the event of each element. Save all different elements in the document by using a global array variable $ Elements. Array items are an example of element classes, 4 attributes (variables of class)   $ count - This element is discovered in the document   $ Chars - The number of character events in the element is the number of character events in the element   $ PARENTS - Parent Elements  $ childs - Child Elements Note: One feature of PHP is you can pass through while (list () = each ()) loop throughout the entire class structure, just like you travers the entire array. All class variables (when you use php3.0, however, however, the method is output in a string. When an element is found, we need to increase its corresponding record to track how many times it appears in the document. In the corresponding $ Elements item, you should add one.</p> <p>We also want to let the parent elements know the current element is its child elements. Therefore, the name of the current element will be added to the $ childs array of the parent element. Finally, the current element should remember who is its parent element. Therefore, the parent element is added to the current element $ PARENTS array project. Display the remaining code remaining in the $ Elements array and its statistical results. This is the simplest nesting cycle, although the correct result is output, but the code is neither concise and no special skills, it is just a loop you might use in him every day. The script example is designed to call the command line of the PHP's CGI mode. Therefore, the format of the statistical result is the text format. If you want to use the script to the internet, you need to modify the output function to generate an HTML format. How to prepare a mini search engine instance with PHP & XML? Let us first familiarize with the XML used in our program (saved as XYZ.xml). <? XML Version = / "1.0 /" Encoding = / "GB2312 /"?> <LINKS> Search Engine built by php and XML technology <web memo = / "MEMO1 /" URL = / "/"> Name1 </ Web> <sub> computer network <web memo = / "nemo2 /"> Name2 </ web> <sub> program <web memo = / "memo3 /"> name3 </ web> <sub> php <web url = / "Http://www.phpbuilder.com// "Memo = /" [English] PHP Development Resources ./ "Www.phpbuilder.com </ Web> <web url = /" http://www.fokus .GMD.DE / "MEMO = /" [English] PHP Development Manual. / "> PHP MANUAL </ Web> </ sub> </ sub> </ sub> </ links> It is quite simple, The root element is links, Sub represents a category, the web is a website information, which contains attributes, the URL represents the joint connection, MEMO is a note information, <web>? ? </ web>, <sub>? ? The data contained in </ sub> is here is the name of the category and website, which is in line with the above specified. Now let's answer the questions raised above: Why use XML to program a search engine? The first reason is sometimes due to various reasons, we may not be able to use the database (mysql or other); second, for small data search engines, its data is small, if you use a database Do it, efficiency is not necessarily high; the most important point is that this search engine maintenance is quite simple and does not have to write a programs for the maintenance of a cumbersome database.</p> <p>For example, we have to add a category or web page, just edit the text file, plus a blessing EB> ??? </ web> or <sub> ???? </ sub> is OK, and if If you want to move a category to another, we just need to copy this part of the SUB. The following is the simplest use of PHP display XML examples. The following program is to parse XML and output to the browser according to the tree structure and display the total number of elements per layer. <? php $ file = /"demo.xml/";// xml file function XML_PARSE_FROM_FILE ($ PARSER, $ file) {// parsing XML file function} Function Start_element ($ Parser, $ Name, $ Attrs) {/ / I encountered the open element marker, such as <a href=/"link/"> to execute this paragraph} Function Stop_Element ($ PARSER, $ NAME) {// encountered this paragraph, such as </ body> to execute this paragraph} Function Data ($ PARSER, $ DATA) {...} Function showcount () {// The total number of elements of each layer} Global $ Level, $ LevelCount, $ MAXLEVEL; $ level = -1; $ PARSER = XML_Parser_create (); // generated parser instance xml_set_element_handler ($ parser, / "start_element /", / "stop_element /"); // set handler xml_set_character_data_handler ($ parser, / "data /"); xml_parser_set_option ($ parser, XML_OPTION_CASE_FOLDING, 0 $ RET = XML_PARSE_FROM_FILE ($ PARSER, $ file); // Resolution file if (! $ RET) {Die (sprintf (/ "xml error:% s at line% d /", XML_ERROR_STRING (XML_GET_ERROR_CODE ($ PARSER) ), XML_GET_CURRENT_LINE_NUMBER ($ PARSER)));} XML_Parser_Free ($ PARSER); // Release the parser showcount ();> On the basis of the above program, a piece of sub-tree can be displayed, we can in accordance with the number of elements And his order in this layer, you will be able to position him, for example: LLINKS (0,1)   ---- Web (1, 1)   - --Sub (1, 2) | ---- Web (2,1)   ---- SUB (2, 2)   | | ---- Web (3, 1)   | ---- SUB (3, 2)    :                                 It is the foundation of our search engine. Because you want to display a subcategory (such as program design -> php->) to use him.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-104911.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="104911" 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.059</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 = '7jTX_2Fi6IXnmxTWCexY1ajlPtaUFyZTEqE65qmvln8D8CJ21JsAUNF2H0nHkfVG0fcrbBHEu93_2F5dW6CeGe8iVA_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>