XML document access technology (DOM, SAX) (transfer)

xiaoxiao2021-03-06  54

Why is SAX | DOM exists? ------------------------------------------- -------------------------------------

SAX (Simple API for XML) and DOM (Document Object Model) are to access their information information without writing a parser. You can use any parser by using XML 1.0 format and you can use any parser using SAX or DOM APIS. This is because developers who use their favorite language development parsers must implement SAX and DOM APIs. SAX and DOM APIs can be implemented in multiple languages ​​(Java, C , Perl, Python, others ...).

So SAX and DOM are existing for the same purpose, which is to enable users to access information in the XML document using any programming language (to have a parser of the programming language). Although they are not very different from the way to access information. What is DOM?

-------------------------------------------------- ------------------------------

DOM allows you to access information stored in an XML document with a hierarchical object model. DOM generates a node tree (based on the structure and information of XML documents). You can access your information through this tree. The text information in the XML document is converted into a node of a set of trees. Regardless of the type of information in your XML document (whether it is a table data, or a column Items, or just a document), DOM creates a node tree when you create an XML document. Dom forced you to use a tree model (like Swing Treemodel) to access the information in your XML document. This model is really good because XML is originally a hierarchy. This is also why DOM can put your information in a tree (even if the information is a table format or simple list ???? Here I don't know how to turn into the original: Even if the information is actually Tabular OR A SIMPLE LIST ??????).

Because in the DOM, each element node actually has a series of other nodes as its child. These children's nodes can contain text values ​​or other element nodes. At first glance, the value of accessing this node is not necessary by traversing all the children who visit an element (for example: node " nazmul ", nazmul is the value). This is indeed necessary if each element is only values. However, elements may contain text data or other elements; this is why you want to make additional work in the DOM to get the element node value. Usually when only pure data in your document, press all the data into a "block" to put on the string and let the DOM return to the value of the string as a particular element node is appropriate. This approach is not suitable if the data in your XML document is a document (such as a Word document or the Framemaker document) in the document, the order of the element is very important. The order of pure data (like a database table) element is not tight. The DOM remains in the order of the elements read from the XML document, as it handles all things as documents. The name of the document object is hereby here.

If you plan to use DOM as a Java object model for information you are stored in an XML document, then you don't need to consider SAX. But if you find that DOM is not a good object pattern that can be used to process XML document information, you may want to see SAX. It is very common in some cases that must use a custom object model. Say that let things seem to have a confused, you can also create your own object mode on the ground. Object-oriented is really a good stuff. What is SAX?

-------------------------------------------------- ------------------------------

SAX allows you to access the information stored in the XML document, not through the node tree, but a series of events. You will ask, what is this benefit? Answer Yes, SAX selection is not to create a Java object model on the XML document (like DOM). This makes SAX faster, simultaneously becomes necessary: ​​Create your own custom object model Create a class of listening to SAX events, and create your own object model. Note that these steps are unnecessary for DOM, because The DOM has created an object model for you (indicating your information with a node tree).

In the case of using the DOM, the parser has done most things, reads the XML document, and creates a Java object model on this basis, then gives you a reference to this object (a document object), so you can operate use it. SAX is called Simple API for XML is not without reason, she is really simple. SAX does not expect the parser to do so much work, all SAX requirements is that the parser should read into the XML document while emit a series of events based on the tag of the XML document encountered. You have to write an XML document processor class (XML Document Handler Class) to handle these events, which means that all tag events make sense to create objects with your own object model. So you have to complete:

Control all XML document information (or the source document here is never seen here, or the source document has a typographic error here, first turn it first) a monitable SAX event (Event is read by the SAX parser The document processor generated when your XML document is created, and the objects you have created in your custom object model. If your object model is simple, SAX will run very fast at runtime. In this case, it will be faster than DOM because it ignores the process of creating a tree object model for your information. On the other hand, you must write an SAX document processor to explain all SAX events (this will be a heavy work).

What type of SAX event is thrown by the SAX parser? These events are actually very simple. SAX will throw events for each start label, which is also true for each end tag. It throws events as part of #pcdata and cdata. Your document processor (listener for these events) To explain these events, you have to create your own custom object model on them. Your document processor must explain these events, while the order of these events is very important. SAX also throws an event on Processing Instructions, DTDS, Comments, but they are conceptually, your parser wants to explain these events (the order of these events) and make them meaningful. When is DOM?

-------------------------------------------------- ------------------------------

If your XML document contains document data (for example, Framemaker Documents Stored In XML Format), then DOM is the most natural choice for your solution. If you want to create some systems similar to document information management, you have to handle a lot of document data. DataChannel Rio products are such an example, which can index and organize information in various types of document resources (such as Word and Excel files). In this case, the DOM is a very suitable program to access information stored in these documents.

However, if you mainly process structured data (serialized Java object in XML, the Equivalent of Serialized Java Objects in XML), DOM is not the best choice. That is where SAX will be more appropriate. When is SAX?

-------------------------------------------------- ------------------------------

If the information in your XML document is easy to read (and machine generated) data, SAX is the appropriate API that allows you to access this information. Machine easy-to-read and generated data types contains the following state: Java object attributes in XML format use some query generated by queries (this Perhaps the data encoded in the relational database table is XML). This seems that the data generated by the machine is you usually generate information and class information in Java. A simple example is an address book containing personal information, shown above. This address book XML file is unlike word processor document, which is an XML document that contains pure data that has been encoded into text.

When your data is this style, you have to create your own data structure and class (object model) to manage operations and continuously save them. SAX allows you to quickly create a processor class that can generate your object model instance. An example is: an SAX document processor. Its work has an XML document that reads I contain my address book, creating a AddressBook class that can access this information. SAX Guide tells you how to do it. This address thin XML document contains the Person element, and there is Name and Email elements in the Person element. My AddressBook object model includes the following classes:

AddressBook class, container of the Person object, Person class, String type Name and Email containers so my "SAX Address Book Processor" can turn the Person element into the Person object and store them into the AddressBook object. This document processor converts Name and Email elements to String objects.

in conclusion

-------------------------------------------------- ------------------------------

The SAX Document Handler you wrote, made the work of mapping the element into objects. If your information is structured to create such a map, you should use the SAX API. On the other hand, if your data is more suitable to use the tree to express it, then you should use the DOM. [020401050] DOM and ASP interface problem?

[Author: cmy8132]

A small problem I did is the knowledge query and troubleshoot system. It is made by ASP and XML, where the query module is implemented, if you use C, C how to implement?

t = "text / html; charSet = GB2312">

Query Results </ Title></p> <p><script language = "</p> <p>JavaScript "Type =" Text /</p> <p>JavaScript "></p> <p><! -</p> <p>Function mm_reeloadpage (init) {// reloads the window if Nav4 Resize</p> <p>IF (init == true) with (Navigator) {IF ((AppName == "Netscape") && (PARSEINT (APPVERSION) == 4) {</p> <p>document.MM_pgW = innerWidth; document.MM_pgH = innerHeight; οnresize = MM_reloadPage;}} else if (!! innerWidth = document.MM_pgW || innerHeight = document.MM_pgH) location.reload ();</p> <p>}</p> <p>MM_RELOADPAGE (TRUE);</p> <p>// -></p> <p></ script></p> <p></ hEAD></p> <p><body bgcolor = "# 000000"> <div align = "center"> <center> <table border = 0 "width =" 611 "cellspacing =" 0 "cellpadding =" 0 "height =" 528 "background =" Images / i-3-1.gif "> <! - DWLAYOTTABLE -> <TR> <TD width =" 609 "Height =" 539 "Valign =" TOP "Background =" Images / T-02.gif " > <Div align = "right"> <table border = "0" width = "91%" cellspacing = "5" cellpadding = "5" height = "477> <! - dwlayouttable -> <tr> < Td width = "1" Height = "80"> </ td> <td width = "100%> <p align =" center "> <font color =" # ff3366 "size =" 4 "face =" imitation Song _GB2312 "> <em> <strong> I have been working hard </ strong> </ em> </ font> </ td> </ tr> <tr bgcolor =" # 006666 "> <td height =" 61 "COLSPAN =" 2 "Valign =" TOP "> <div align =" center "> <font color =" # 00ff00 "size =" 6 "> <strong> question answer is </ strong> </ font> </ Div> </ td> </ tr> <tr> <td height = "349" colspan = "2" valign = "middle" bgcolor = "# 006460"> <div align = "left"> <Form name = "form1" method = "post" action = ""</p> <p>> <P> question = request.form ("std_question" Question = trim (question) length = len (question) Question = left (question, length-4) Keywords = split (Question, "") keyword_num = Ubound (keywords) 1 set doml = server.createObject ("msxml.domdocument") DOMXML.LOAD "D: / knowledge query and troubleshoot system /DS.XML" set root = doml.documentelement set childNodes = root.selectnodes "//" & keywords (0)) 'Find all nodes in accordance with the first keyword if childnodes.length = 0 Then Response.write "<font color =' # ff3366 '>" & "Sorry, I can't find you Question of the problem! "&" </ Font> "</p> <p>Dim Stack () redim stack (100, 1) TOP = 0 for i = 0 to childnodes.length-1 set child = childnodes.Item (i) set stack (top, 0) = Child Stack (TOP, 1) = 0 TOP = TOP 1 Next</p> <p>Answer_Order = 1 While Top> 0 TOP = TOP-1 Set Parent = Stack (TOP, 0) Key_i = Stack (TOP, 1) IF key_i 1 = keyword_num the 'keyword matching Answer = Question answer = cstr (answer_order) "." & & answer answer_order = answer_order 1 set answerNodes = parent.childNodes if answerNodes.length> 1 then answer = answer & "can be divided into:" else answer = answer & "is:" end if for m = 0 to answerNodes.length -2 set child = answerNodes.item (m) if child.nodeType = 1 or child.nodetype = 2 then 'Current node is element node answer = answer & child.nodename & "," elseif child.nodeType = 3 or child.nodeType = 4 THEN ANSWER = Answer & "& child.text &" "End if Next set child = answer Nodes.Item (M) IF m> 0 Then if child.nodetype = 1 THEN ANSWER = Answer & "& Child.NodeName &". <HR> "elseif child.nodetype = 3 or child.NodeType = 4 Then Answer = Answer &" & Child.Text & ". <HR> "end if else if child.nodetype = 1 Then Answer = Answer & Child.NodeName &".</p> <p><HR> "elseif child.nodetype = 3 or child.nodetype = 4 THEN ANSWER = Answer & Child.Text &". <HR> "end if end if response.write" <font color = '# ff3366'> "& answer &" <</ font> "else set childset = parent.childnodes finded = false for j = 0 to childset.length-1 set Child = childset.item (j) childname = child.nodeename if strass (childname, keywords (key_i 1)) = 0 THEN SET Stack (Top, 0) = Child Stack (TOP, 1) = key_i 1 TOP = TOP 1 Finded = true end if next if Finded = false kil = 0 to childset.Length-1 set child = childset.item (k) set stack (top, 0) = child stack (top, 1) = key_i TOP = TOP 1 Next End if End if Wend%></p> <p></ p> </ form> <p> <object classid = "CLSID: D27CDB6E-AE6D-11CF-96B8-444553540000" CodeBase = "http://download.macromedia.com/pub/shockwave/cabs/flash/swflash .cab # version = 5, 0, 0, 0 "width =" 144 "height =" 39> <param name = "movie" value = "button3.swf"> <param name = "Quality" value = "high "> <Param name =" base "value ="> <param name = "bgcolor" value = "# 006666> <Embed src =" button3.swf "width =" 144 "height =" 39 "base = "." quality = "high" pluginspage = "http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" type = "application / x-shockwave-flash" bgcolor = "# 006666" > </ Embed> </ Object> </ p> <TR> <TD Height = "36" Colspan = "2" VALIGN = "TOP"> <Table Width = "100%" border = "0" Cellpadding = "0" cellspacing = "0"> <! - dwlayouttable -> <tr> <td width = "383" height = "26" VALIGN = "TOP"> <font color = "# 66fff" size = "4"> </ font> </ td > <Td width = "151"</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-82120.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="82120" 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.042</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 = 'a_2BRtJ6wZKHacT87VArovi_2BiA2DL8PD_2F53b5_2FCfTvrc6qwKvYiqFJkox14LYODjyT4JsThcIx2c6Fb9JSZH3GXw_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>