利用vb操作xml数据(Using VB to operate XML data).doc

上传人:rrsccc 文档编号:9186671 上传时间:2021-02-06 格式:DOC 页数:22 大小:28.47KB
返回 下载 相关 举报
利用vb操作xml数据(Using VB to operate XML data).doc_第1页
第1页 / 共22页
利用vb操作xml数据(Using VB to operate XML data).doc_第2页
第2页 / 共22页
利用vb操作xml数据(Using VB to operate XML data).doc_第3页
第3页 / 共22页
利用vb操作xml数据(Using VB to operate XML data).doc_第4页
第4页 / 共22页
利用vb操作xml数据(Using VB to operate XML data).doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

《利用vb操作xml数据(Using VB to operate XML data).doc》由会员分享,可在线阅读,更多相关《利用vb操作xml数据(Using VB to operate XML data).doc(22页珍藏版)》请在三一文库上搜索。

1、利用vb操作xml数据(Using VB to operate XML data)What is XML?The extensible markup language XML is a simple data storage language, using a series of simple tags to describe data, and these markers can be used to establish a convenient way, although the XML space to occupy more space than the binary data, bu

2、t XML is extremely simple and easy to master and use.XML and Access, Oracle and SQL Server database, the database provides the data storage and analysis capabilities, more powerful such as data indexing, sorting, searching, consistency, XML is only the display of data. In fact, the biggest differenc

3、e between XML and other forms of data is that he is extremely simple. This is a look a little petty advantages, but it is in this respect that XML out of the ordinary.Simple XML to make it easy to read and write data in any application, the XML soon became the only common language of data exchange,

4、although different applications also support other data exchange format, but soon they will support XML, it means that the program can be more easily with Windows, Mac OS Linux, and other platforms combined with the information, and then can easily load XML data into the program and his analysis, an

5、d XML format output results.Advantages of XMLWe talked about XML longer than the exchange of data between different applications, the XML file is to facilitate the construction of a small database, not long ago, the use of INI file storage software configuration information, user preferences and oth

6、er information, then Microsoft introduced the system registry, then Microsoft told we should not use the INI file, since then Visual Basic of the INI file support weakened. Unfortunately, the registry has several fatal drawbacks: it is not a simple text file to read and write, may become large and s

7、low, if the registry is somehow there is a problem, it may cause system crashes.The configuration information in the XML file to avoid these problems, even the XML file can be set to a file sharing in different computer users can share data, which is incomparable to the registry.In the ASP.NET that

8、is called the next generation ASP, you can use XML directly in the WEB page. You can bind data directly and automatically display using the data bound controls.Of course, you can not choose XML, the use of text files, registry, database, you can complete the task XML can complete, XML is just anothe

9、r tool you store and restore data.A brief introduction to XML grammarThe XML syntax is very simple, XML document consists of nodes, the use of open and close nodes in the format and description tags, HTML tags are very similar, the biggest difference between them is free to define the tag name XML.

10、For example, the following tag describes a telephone number: 987-654-3210 But no statement can use the tag name.The start and end tags must be the same, and the XML is case sensitive, so the markup must be the same. For example, in the above example, the beginning of mark must end with mark, but not

11、 or Node tags can contain attributes, such as the Phone node in the code below contains the attribute Type, whose value is WorkFax: 987-654-3210 If you do not want to contain a value in the node, then do not need to mark the end, with a slash in the back to mark the beginning of the end node, in the

12、 following example, the Number attribute of the Phone tag is stored in a telephone number, there is no need for an end tag:The structure of a XML document is a tree hierarchy. The document must have a unique root node,The root node contains all the other nodes. Lets give you a more complete example:

13、 Andy Fickle 1234 Programmer Place Bugsville CO 82379 354-493-9489 Betty Masterson 937-878-4958 937-878-4900 .Note that similar nodes do not need to contain the same information, such as the first Entry node contains address information and home telephone number, the second Entry node contains the W

14、ork and WorkFax phone number, but does not include the first Entry node contains information.XML toolsAs in the previous example, the XML syntax is you can make a XML parser in a very short time so simple that, fortunately you do not have to do so, because the XML tool can be run on various platform

15、s, including the Visual Basic Windows can be installed.It is these L tools, not the XML itself, that make XML more powerful and complex. Different parsers allow you to load the entire XML document at any point or just load a node. On the contrary, XML Writer can create an XML document and a node at

16、the same time.The DOM parser enables us to easily load, copy, sort, modify, and store XML files, traverse the nodes to obtain names or attributes, and sort the results. Although their functions are not really relational, databases are powerful, but these features of DOM are still very useful.XSD def

17、ines the format of XML documents, and the XSL extended style sheet defines how to convert XML documents into other file formats that can be browsed in WEB browsers, such as HTML files.These tools are actually more complex than XML itself, so all of the books explaining XML have taken a lot of space

18、to explain these XML tools. But this is beyond the scope of this article, interested readers can refer to the relevant information.Visual Basic.Net provides a complete tool for using XML, XSL, and other XML tools. But without waiting for VB.NET, Microsoft XML core services (MSXML) version 4 provides

19、 tools to load and store XML documents from Visual Basic6.0.Download the latest version of MSXML in and install it on the computer. In Visual Basic 6 Microsoft XML V4.0 used as references to other objects, first select the reference menu item in the Project menu, select Microsoft V4.0, click OK, af

20、ter the completion of all you can now add the XML object in the VB application.Class DOMDocumentThe document object model (DOM) uses a series of corresponding objects to describe the hierarchical state of the XML document, and the DOMDocument class is a MSXML class that describes the DOM structure o

21、f the XML document.The DOMDocument class provides only a few useful properties and methods. The Load method loads a XML file, and the loadxml method adds the string as the XML data to the object. For example, the following code adds a small XML file to a document called xml_document.Dim xml_document

22、 As New DOMDocumentXml_document.loadXML _“人”vbcrlf和_“FirstName杆/名”vbcrlf和_“姓斯蒂芬斯/姓”vbcrlf和_“/人”属性返回文档的DOMDocument的XML XML描述,可以显示这些返回值看看这些文档究竟是什么样子,也可以将它存储为一个文件,但这完全不必要,因为DOMDocument对象的救方法已经自动将他们存储了。DOMDocument对象的属性表示文档数据的根结点通常情况下操作XML文档元素,文档都从这里开始。提供了几种创建新节点的方法DOMDocument。createElement方法为文档创建一个新的元素节

23、点,其他创建节点的方法有CreateAttribute,createprocessinginstruction,和createTextNode,在这里就不一一介绍了。ixmldomnode类ixmldomnode类描述了一个节点,该类提供了一系列用于搜索和操纵XML文档的属性和方法。选择第二个dog节点方法用于搜索指定节点的后代,用于搜索指定节点路径的语言称为XPath,XPath非常棘手,本文就不详细说明其规范了。下面我们将介绍两个对搜索子节点有特别有用并且简单的方法。在给选择第二个dog节点方法中输入子节点的名字,该方法将在节点的子节点进行精确匹配搜索。如果在输入的字符串前面加上”。/”,

24、那么将搜索节点的全部后代。“一个孩子节点命名为“LastName搜索。”集last_name_node = address_node。selectSingleNode(“姓”)“任何后代命名为“LastName搜索。”集last_name_node = address_node。selectSingleNode(“。/姓”)下面列出了ixmldomnode对象的部分非常有用的属性:节点属性集合属性。节点的标记名形式。nodetypestring。节点的类型ownerdocument返回DOMDocument对象包含的节点。文本。表示节点包含的文本内容。如果该节点包含其他节点,那么文本代表了所有

25、节点的文本内容的组合。XML XML。给出了节点的内容,例如:“FirstName杆/名”。子集合包含了节点的子节点。要给节点增加一个子节点,首先必须给使用DOMDocument对象的节点创建方法,然后将这个新建的节点加入到父节点的子集合中。下面的代码展示了创建一个新的子节点的子程序,并使用父节点的appendChild方法将其加入到父节点中:向指定的父节点添加一个新节点。列兵Sub CreateNode(ByVal缩进为整数,_一个家长IXMLDOMNode,ByVal node_name为字符串,_一个node_value字符串)昏暗的new_node IXMLDOMNode创建新节点。集

26、new_node =父母。ownerdocument createElement(node_name)。“设置节点的文本值。new_node.text = node_value“将节点添加到父节点。parent.appendchild new_node端子savevalues程序Now we can create a simple procedure using XML (Figure 1), the value stored in the XML file, the program starts running when the data is loaded from the VALUE.X

27、ML program, the end of the run, the program in the current values are stored in the VALUE.XML file.The following code shows the structure of the VALUE.XML file: Rod Stephens 1234 Programmer Place Bugsville CO 80276 List1 shows how to write SaveValues, when the form is loaded, the form_load event tri

28、ggers the LoadValues subroutine.LoadValues created a xml_document DOMDocument object, and then load the XML file, use the selectSingleNode method to find the node named values, and then obtained from the value node in the offspring value using the GetNodeValue method.GetNodeValue uses the selectSing

29、leNode method of the value node to find the target node. If the node does not exist, the function will return a default value. If the node is found, GetNodeValue will return the text value of the node. For the data nodes in the value.xml file, text is only the content of the text contained in the no

30、de.When the form is uninstalled, the form_unload event is triggered, and the unload event calls the SaveValues subroutine. The program creates a new DOMDocument object, which creates a new node called value, and then adds the node to the document with the appendChild method of the document.After cre

31、ating all the new nodes, SaveValues calls the DOMDocuments save method to store the new XML file.Note that the new file has covered the old files, use the DOMDocument object to change part of the XML file, you can load the XML file, and then modify the one part, and then save the file, but the origi

32、nal file will be completely covered. This is a small defect, but at this point you can use other programs to modify it.The final part of the List1 CreateNode CreateNode subroutine, the parent node creates a new node and at the same time for the node assignment. In this subroutine, you first refer to

33、 a DOMDocument object, and then use the createElement method of the object to create a new node.The createNode method sets the text property of the node, and then adds the node as a child node to the parent node.List1:Option ExplicitPrivate m_AppPath As StringPrivate Sub Form_Load ()Get the applicat

34、ions startup path.M_AppPath = App.PathIf Right$(m_AppPath, 1) Then m_AppPath = m_AppPath &Load the values.LoadValuesEnd SubPrivate Sub Form_Unload (Cancel As Integer)Save the current values.SaveValuesEnd SubLoad saved values from XML.Private Sub LoadValues ()Dim xml_document As DOMDocumentDim values

35、_node As IXMLDOMNodeLoad the document.Set xml_document = New DOMDocumentXml_document.Load m_AppPath & Values.xmlIf the file doesnt exist, thenxml_document.documentElement is Nothing.If xml_document.documentElement Is Nothing Then文件不存在。什么都不做。退出子最后如果“查找值部分。集values_node = xml_document。selectSingleNode(

36、“值”)“读取保存的值。txtfirstname。文本= getnodevalue(values_node,“名”、“?“?”txtlastname。文本= getnodevalue(values_node,“姓”、“?“?”txtstreet。文本= getnodevalue(values_node,“街”、“?“?”txtcity。文本= getnodevalue(values_node、“城市”、“?“?”txtstate。文本= getnodevalue(values_node、“国家”、“?“?”txtzip。文本= getnodevalue(values_node,“拉链”、“?“

37、?”端子返回节点的值。私有函数getnodevalue(ByVal start_at_node作为ixmldomnode,_一个node_name为字符串,_可选ByVal default_value是字符串”)为字符串昏暗的value_node IXMLDOMNode集value_node = start_at_node。selectSingleNode(“/”和node_name)如果value_node是没有然后getnodevalue = default_value其他的getnodevalue = value_node.text最后如果端功能“保存当前值。私有子savevalues(

38、)昏暗的xml_document作为DOMDocument昏暗的values_node IXMLDOMNode创建XML文档。集xml_document =新DOMDocument“创建值”节节点。集values_node = xml_document createElement(“价值”)。“将值节节点添加到文档中。xml_document.appendchild values_node“为内部的值创建节点“值”节节点。CreateNode values_node,“名”,txtfirstname。文本CreateNode values_node,“姓”,txtlastname。文本Crea

39、teNode values_node,“街”,txtstreet。文本CreateNode values_node,“城市”,txtcity。文本CreateNode values_node,“状态”,txtstate。文本CreateNode values_node,“拉链”,txtzip。文本“保存XML文档。xml_document.save m_apppath &”的价值观。XML”端子向指定的父节点添加一个新节点。列兵Sub CreateNode(ByVal父为IXMLDOMNode,_一个node_name为字符串,一个node_value字符串)昏暗的new_node IXMLD

40、OMNode创建新节点。集new_node =父母。ownerdocument createElement(node_name)。“设置节点的文本值。new_node.text = node_value“将节点添加到父节点。parent.appendchild new_node端子savevaluesindented程序虽然每个人都化了很大的精力去处理XML文档,使他们看上更容易些,但XML工具一般都忽略了那些使文档结构明显的空白和缩进XML,XML解析器也同样忽略缩进和空白。不幸的是我们例子也同样忽略了这些缩进和空白,SaveValues creates a XML file like th

41、is, and all of the code is in the same line. FirstName Rod LastName Stephens Street 1234 Programmer Place City Bugsville State CO Zip 80276 /Zip VB.NET includes the text write class, XML document format can be specified. However, MSXML does not have this function, so if you want to save XML files in

42、 a clear format, you can only add it to the format.List2 lists the program using the SaveValuesIndented code, the SaveValues subroutine and the example above speak almost identical, but he in the creation of the value node to the XML document to create a new tag .Then SaveValues calls CreateNode to

43、create a new data node, but here it passes a new parameter to CreateNode, which represents the indentation of this new node.CreateNodeSave the current values.Private Sub SaveValues ()Dim xml_document As DOMDocumentDim values_node As IXMLDOMNodeCreate the XML document.Set xml_document = New DOMDocume

44、ntCreate the Values section node.Set values_node = xml_document.createElement (Values)Add a new line.Values_node.appendChild xml_document.createTextNode (vbCrLf)Add the Values section node to the document.Xml_document.appendChild values_nodeCreate nodes for the values inside theValues section node.CreateNode 4, values_node, FirstName, txtFirstName.TextCreateNode 4, values

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 社会民生


经营许可证编号:宁ICP备18001539号-1