收藏 分享(赏)

利用VB操作XML数据.txt

上传人:初中学霸 文档编号:21758562 上传时间:2024-04-22 格式:TXT 页数:10 大小:14.62KB
下载 相关 举报
利用VB操作XML数据.txt_第1页
第1页 / 共10页
利用VB操作XML数据.txt_第2页
第2页 / 共10页
利用VB操作XML数据.txt_第3页
第3页 / 共10页
利用VB操作XML数据.txt_第4页
第4页 / 共10页
利用VB操作XML数据.txt_第5页
第5页 / 共10页
亲,该文档总共10页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

1、XML XML򵥵洢򵥵XMLXML XMLAccess,OracleSQL Server洢磺򡢲XMLXML򵥡XML XMLXMLXMLWindowsMac OS, LinuxXMLXML XML XML佻XMLINI洢INIVisual BasicINI򵥵 XMLXML ASPASP.NETWEBXML󶨿 XMLXMLXML洢 XML XML򵥣XML򿪺HTMLXML绰 Phone987-654-3210/Phone XMLPhone/Phone/phone/PHONE

2、 PhoneTypeWorkFax: Phone Type=WorkFax987-654-3210Phone PhoneNumber洢绰 Phone Type=WorkFax Number=987-654-3210 / XML Addresses Entry Type=Personal FirstNameAndy/FirstName LastNameFickle/LastName Street1234 Programmer Place/Street CityBugsville/City StateCO/State Zip82379/Zip Phone Type=Home354-493-9489

3、/Phone /Entry Entry Type=Work FirstNameBetty/FirstName LastNameMasterson/LastName Phone Type=Work937-878-4958/Phone Phone Type=WorkFax937-878-4900/Phone /Entry . /Addresses Entry绰EntryWorkWorkFax绰Entry XML XMLXMLXMLVisual BasicWindows LXMLXMLXMLXML Writer XML DOM洢XML󣬵DOM XSDXMLXSLXMLWEBHTML

4、XMLXML鼮XML Visual Basic.NetXMLXSLXMLVB.NETXMLMSXML汾4.0Visual Basic6.0洢XML Basic 6.0Microsoft XML V4.0Microsoft V4.0,OK,VBXML DOMDocument (DOM)XMLDOMDocumentXMLDOMMSXML DOMDocumentLoadxmlloadxmlxml磬xmlxml_document Dim xml_document As New DOMDocument xml_document.loadXML _ Person & vbCrLf & _ FirstNam

5、eRod/FirstName & vbCrLf & _ LastNameStephens/LastName & vbCrLf & _ /Person DOMDocumentxmlxml洢DOMDocumentsave洢 DOMDocumentdocumentElementXML DOMDocumentCreateElementcreateAttribute, createProcessingInstruction, createTextNode IXMLDOMNode IXMLDOMNodeXML selectSingleNode XPATHXPATH淶򵥵 selectsing

6、leNode./ Search for a child node named LastName. Set last_name_node = address_node.selectSingleNode(LastName) Search for any descendant named LastName. Set last_name_node = address_node.selectSingleNode(./LastName) IXMLDOMNode attributes. nodeName. nodeTypeString. ownerDocument.DOMDocument text.text

7、 xml.xml磺FirstNameRod/FirstName. ChildNodesDOMDocumentchildNodes򣬲appendChild Add a new node to the indicated parent node. Private Sub CreateNode(ByVal indent As Integer, _ ByVal parent As IXMLDOMNode, ByVal node_name As String, _ ByVal node_value As String) Dim new_node As IXMLDOMNode Create

8、 the new node. Set new_node = parent.ownerDocument.createElement(node_name) Set the nodes text value. new_node.Text = node_value Add the node to the parent. parent.appendChild new_node End Sub SaveValues XML򵥵1洢XMLVALUE.XMLVALUE.XML VALUE.XML: Values FirstNameRod/FirstName LastNameStephens/La

9、stName Street1234 Programmer Place/Street CityBugsville/City StateCO/State Zip80276/Zip /Values List1SaveValuesform_loadLoadValues LoadValuesxml_documentDOMDocumentxmlselectSingleNodevaluesGetNodeValuevalue GetNodeValuevalueselectSingleNodeGetNodeValuetextvalue.xmltext form_unloadunloadSaveValues

10、61747;򴴽DOMDocument󣬸󴴽valueappendChild SaveValuesDOMDocuments save洢xml DOMDocumentXMLXML󱣴 List1CreateNodeCreateNode DOMDocumentcreateElement createNodetext󽫽 List1: Option Explicit Private m_AppPath As String Private Sub Form_Load() Get the applications startup

11、 path. m_AppPath = App.Path If Right$(m_AppPath, 1) Then m_AppPath = m_AppPath & Load the values. LoadValues End Sub Private Sub Form_Unload(Cancel As Integer) Save the current values. SaveValues End Sub Load saved values from XML. Private Sub LoadValues() Dim xml_document As DOMDocument Dim values_

12、node As IXMLDOMNode Load the document. Set xml_document = New DOMDocument xml_document.Load m_AppPath & Values.xml If the file doesnt exist, then xml_document.documentElement is Nothing. If xml_document.documentElement Is Nothing Then The file doesnt exist. Do nothing. Exit Sub End If Find the Value

13、s section. Set values_node = xml_document.selectSingleNode(Values) Read the saved values. txtFirstName.Text = GetNodeValue(values_node, FirstName, ?) txtLastName.Text = GetNodeValue(values_node, LastName, ?) txtStreet.Text = GetNodeValue(values_node, Street, ?) txtCity.Text = GetNodeValue(values_nod

14、e, City, ?) txtState.Text = GetNodeValue(values_node, State, ?) txtZip.Text = GetNodeValue(values_node, Zip, ?) End Sub Return the nodes value. Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, _ ByVal node_name As String, _ Optional ByVal default_value As String = ) As String Dim va

15、lue_node As IXMLDOMNode Set value_node = start_at_node.selectSingleNode(./ & node_name) If value_node Is Nothing Then GetNodeValue = default_value Else GetNodeValue = value_node.Text End If End Function Save the current values. Private Sub SaveValues() Dim xml_document As DOMDocument Dim values_node

16、 As IXMLDOMNode Create the XML document. Set xml_document = New DOMDocument Create the Values section node. Set values_node = xml_document.createElement(Values) Add the Values section node to the document. xml_document.appendChild values_node Create nodes for the values inside the Values section nod

17、e. CreateNode values_node, FirstName, txtFirstName.Text CreateNode values_node, LastName, txtLastName.Text CreateNode values_node, Street, txtStreet.Text CreateNode values_node, City, txtCity.Text CreateNode values_node, State, txtState.Text CreateNode values_node, Zip, txtZip.Text Save the XML docu

18、ment. xml_document.save m_AppPath & Values.xml End Sub Add a new node to the indicated parent node. Private Sub CreateNode(ByVal parent As IXMLDOMNode, _ ByVal node_name As String, ByVal node_value As String) Dim new_node As IXMLDOMNode Create the new node. Set new_node = parent.ownerDocument.create

19、Element(node_name) Set the nodes text value. new_node.Text = node_value Add the node to the parent. parent.appendChild new_node End Sub SaveValuesIndented xmlxmlxmlxml SaveValuesxml ValuesFirstNameRod/FirstNameLastNameStephens/LastNa meStreet1234 Programmer Place/StreetCityBugsville/Ci tyStateCO/Sta

20、teZip80276/Zip/Values VB.NETXML涨MSXMLXML List2SaveValuesIndentedSaveValuesvalueXMLvalue SaveValues CreateNodeCreateNode CreateNode Save the current values. Private Sub SaveValues() Dim xml_document As DOMDocument Dim values_node As IXMLDOMNode Create the XML document. Set xml_document = New DOMDocum

21、ent Create 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_node Create nodes for the values inside the Values section

22、 node. CreateNode 4, values_node, FirstName, txtFirstName.Text CreateNode 4, values_node, LastName, txtLastName.Text CreateNode 4, values_node, Street, txtStreet.Text CreateNode 4, values_node, City, txtCity.Text CreateNode 4, values_node, State, txtState.Text CreateNode 4, values_node, Zip, txtZip.

23、Text Save the XML document. xml_document.save m_AppPath & Values.xml End Sub Add a new node to the indicated parent node. Private Sub CreateNode(ByVal indent As Integer, _ ByVal parent As IXMLDOMNode, ByVal node_name As String, _ ByVal node_value As String) Dim new_node As IXMLDOMNode Indent. parent

24、.appendChild parent.ownerDocument.createTextNode(Space$(indent) Create the new node. Set new_node = parent.ownerDocument.createElement(node_name) Set the nodes text value. new_node.Text = node_value Add the node to the parent. parent.appendChild new_node Add a new line. parent.appendChild parent.ownerDocument.createTextNode(vbCrLf) End Sub XML棬漰򵥵XML飬XMLMSXMLXML

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 教育专区 > 高中资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:文库网官方知乎号:文库网

经营许可证编号: 粤ICP备2021046453号世界地图

文库网官网©版权所有2025营业执照举报