XML DOM Beginner Guide (including verification DTD)

zhaozj2021-02-08  281

XML DOM Beginner's Guide Author: Dongsheng Tao Overview: This paper discusses how to access and maintain XMLDOM XML document, the parser XMLDOM implemented by Microsoft. Directory: Introduction DOM is how to use DOM how to process an document How to get information from the XML document How to traverse the XML document Next to do this: As a VB developer you may contact the Extensible Markup Language (XML) document. You now want to handle the XML document and integrate it into your solution. You can program the resolution, handle it as a normal text document, but so too efficient, no power of XML: it can be structured. The best way to get information from the XML file is to use the XML parser. The parser, simple, is a software that can make the data in the XML file easy to use. As a developer of VB, you may want to get a parser that supports Document Object Model (DOM). The DOM describes a range of methods for accessing XML and HTML documents, which should be implemented. A parser that supports DOM should turn data in XML into a series of objects so that these objects can be programmed. In this article, you will learn how to use the DOM structure implemented by the Microsoft parser (MSXml.dll) to access and maintain an XML document. When we continue to advance, let's take a look at a paragraph XML code to understand how this parser makes our life easier. Next, this document called CDS.xml is used to represent each item of a record, and each item contains information such as main singing, title, and track.

Frank sinatra in The Wee Small Hours </ Title> <TRACKS> <Track> in The Wee Small Hours </ track> <track> Mood Indigo </ track> <track> Glad to be unhappy </ track> <track> i Get Along Without You Very Well </ track> </ tracks> <price> $ 12.99 <compactdisc> <artist type = "band"> the offspring </ artist> <title numberoftracks = "5"> Americana < / Title> <tracks> <track> Welcome </ track> <track> have you ever </ track> <track> staring at the sun </ track> <track> pretty fly (for a white guy) </ track> </ tracks> <price> $ 12.99 </ price> </ compactdisc> </ compactdiscs> The second row of the document above references an external DTD (Document Type Description), DTD describes a specific type of XML hierarchy and The content that can be included. The XML parser uses DTD to verify the correctness of the XML document. DTD is only one of the way you use to verify that the XML document is legal. Another more popular approach is XML Schemas, which describes Schemas instead of DTD with XML instead of DTD. Unlike DTD, schema is described in XML, which is to use its own "interesting" syntax. The following document is CDS.DTD used by cds.xml.</p> <p><! Element CompactDiscs> <! Element CompactDisc (Artist, Title, TRACKS, Price)> <! Entity% Type "Individual | Band"> <! Element Artist (#pcdata)> <! Attlist artist type (% Type;) #Required> <! Attlist title Numberoftracks cdata #Required> <! Element tracks (track *)> <! Element price (#pcdata)> <! Element track (#pcdata) > This article does not discuss DTD and XML Schemas, and XML-DATA-based XML Schema Reference has been submitted to W3C. What DOM is: XML DOM structure implements the content of the XML document as an object model. W3C Dom Level 1 illustrates how DOM structures implement properties, methods, events, and more. Microsoft's DOM implementation fully supports W3C standards, and there are many new features that make programs easier to access XML files. How to use DOM Use DOM, you need to create an instance of an XML parser. Microsoft created a series of standard COM interfaces in MSXml.dll to make the creation instance possible. Msxml.dll contains type libraries and applicable code, which you can use to process XML files. If you use clients that implement scripts, such as VBScript and IE, you can use the CreateObject method to get an instance of the parser. Set objParser = CreateObject ("Microsoft.xmldom") If you use the ASP (Active Server Page), you use the Server.createObject method. Set objParser = Server.createObject ("Microsoft.xmLDom") If you are using VB, you can create a reference to the MSXML type library so you can access the DOM. To use MSXML in VB6.0, the operation is as follows: Open the Project References item Select Microsoft XML from the COM object, Version 2.0, if you can't find this, you need to get it. You can create an instance of a parser. Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument You can get MSXml.dll through two ways.</p> <p>You can install IE5.0, and the MSXML parser is in which components are integrated. Or you can download it on related websites Once you have established a reference to the type library, you can perform resolution, transfer to document, in short, you can handle the XML document. You may have some confusion, what should I do? If you open the MSXML library, use the Visual Basic 6.0 object to view the object model in the viewer, you will find it very rich. This article will tell you how to use the DomDocument class and the IxmLDomnode interface to access the XML document. How to load a document: To transfer an XML document, you must first create an instance of DomDocument. Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument When you get a legal reference, you can use the LOAD method to transfer a document. The parser can be transferred from the local hard drive or transferred from the network through UNC and URL. From the hard disk to the following: if xdoc.load ("c: / my documents / cds.xml") THEN 'Document Tune Success' Do something we like Else' document transferred failures End if you finish your work, you Need to release this reference, MSXML does not directly implement a Close method, you'd better set it directly to Nothing to close it. Set xdoc = Nothing When you call a document, the default is asynchronous, you can change it by modifying the Async property. If you want to operate a document, you must first check the ReadyState property to confirm the status of the document, which will return five possible results. The status attribute value is not initialized: Turn into the document does not start 0 to transfer: LOAD method is executing 1 Transfer completion: The LOAD method has completed 2 interaction phase: DOM can perform a read-only test, data part resolution 3 complete: Data is fully resolved, Read / write operation. 4</p> <p>The MSXML parser implements some useful methods where you can use these methods to track the status of the transfer process when you transfer a large document. These methods are also very helpful to transfer from the Internet. To open a document on an Internet, you need to provide an absolute URL and must add an HTTP: // prefix. Below is an example. XDoc.async = false if xdoc.load ("http://www.develop.com/hp/brianr/cds.xml") The 'Document Tune Success' Do what we like ELSE' document transfer failback If set the Async property to false so that the parser will not give the control to your code before the document is completed. If you save Async as True, you must check the readyState property when you access the document or use DomDocument's event to prompt your code when you can access the document. Processing error: Your document may be transferred in a variety of reasons, the most common reason is that the document name provided to the parser is incorrect, and another common reason is that the XML document is not legal. The default parser will verify that your document meets a DTD or Schema, you can do not allow the parser to perform verification, before executing the Load method, the DomDocument's properties ValidateonParse is false. Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument xdoc.validateonparse = false if xdoc.load ("c: / my documents / cds.xml") Then 'document transfer success' as we like things ELSE' Document Tune failure END IF is not a good idea in advance, it will bring a lot of problems, at least it will be the user you provide for you. You can get information about the error type from the parser by accessing the ParseError object. Create a reference to the IXMLDompivalSeError interface and point it to the document itself ParseError object. The IxmlDompivalSeError interface implements seven properties to make you get the wrong reason. The following example shows a message box listing all error messages in the ParseError object.</p> <p>Dim xdoc as msxml.domdocument set xdoc = new msxml.domdocument if xdoc.load ("c: / my documents / cds.xml") The 'document is successful "Do what we like ELSE' document transfer DIM DIM STRERRTEXT AS STRING DIM XPE AS MSXML.IXMLDompivalError 'Get ParseError object set xpe = xdoc.parseerror with xpe strerrtext = "Your XML Document cannot be transferred to" & VBCRLF & _ "error #:" Errorcode & ":" & xpe.reason & _ "line #:" & .line & vbcrf & _ "line position:" & .linepos & vbcrlf & _ "Position in file:" & .filepos & vbcrf & _ "Source Text: "& .SrcText & Vbrlf & _" Document URL: "& .url End with Msgbox Strerrtext, vbexclamation end if set xpe = Nothing You can use the ParseError object to report an error message to your user, or write it to you In a log, you can try yourself to solve the problem. How to get information from the XML document: Once you have successfully transferred the document, the next step is how to get information. When you operate a document, you often use the Ixmldomnode interface, you use it to read / write each separated node element. Before using it, you must first understand the 13 types of node elements supported by MSXML, and below is the most commonly used. DOM Node type example NODE_ELEMENT <artist type = "band"> The Offspring </ artist> NODE_ATTRIBUTE type = "band"> The Offspring NODE_TEXT The Offspring NODE_PROCESSING_INSTRUCTION <? Xml version = "1.0"?> NODE_DOCUMENT_TYPE <! DOCTYPE compactdiscs SYSTEM "cds .dtd "></p> <p>You access the type of node through the two properties implemented by the IXmldomnode interface. The NodeType property lists all items of DomnodeType (some items are listed on the table above). In addition, you can use the NodeTypeString property to get a string of the node type. Once you have a DOM reference for the document, you can traverse the hierarchy of the node. With documentation, you can access the ChildNodes property that gives a directory that contains all nodes from top. The ChildNodes property implements ixmldomnodelist, which supports Visual Basic's For / Each structure, so you can list all nodes in ChildNodes. In addition, the ChildNodes property also implements the Level property, which can return all child nodes. Not just a document object has a ChildNodes property, each node has a ChildNodes property. Because of this, the ChildNodes property and the IXmLDomnode's HaschildNodes property match make it very convenient to traverse documents, access elements, properties, and values. It is worth mentioning that the element and element values ​​are parent and child relationships. For example, in the CDS XML document, the element <title> represents the name of the song, know the value of <title>, you need to access the node's property node_text. If you find a node with data you are interested, you can access its properties, or you can access their parent nods through the ParentNode property. How to traverse the XML document: You can traverse the node that uses the document object, because the XML itself is a hierarchy, so it is easy to write recursive code to access the entire document. The LoadDocument program opens the XML document and then calls another program DisplayNode that is used to display the structure of the document. LoadDocument passes a reference to the XML document of the XML document as a reference as its parameters, and transmits an integer to indicate the level level of the start display. The code utilizes parameters to format the text in the Visual Basic document structure display window. The function of the DisplayNode property will traverse the document to find the Node_text node type string, once the code finds a node_text node, which uses the NodeValue property to obtain the corresponding text string. In addition, the ParentNode property of the current node points to a node of an element type. The element node implements a NodeName property, the NodeName and NodeValue properties are displayed. If there is a child node, confirm by detecting the HaschildNodes property, DisplayNode calls yourself all of the traversal documents. The following DISPLAYNODE program is written to the Visual Basic window with debug.print.</p></div><div class="text-center mt-3 text-grey"> 转载请注明原文地址:https://www.9cbs.com/read-1828.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="1828" 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 = 'hB6q7bECB7PsZ1Udv6y32fcJUW_2FAB_2Ftoy03ZKLvzS0zpMcvJKiv_2B0gPsnNwwvCWbIAL0CtIoD2ybJ67YYLgARw_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>