Value.xml文件存在如下结点
<?xml version="1.0" encoding="GB2312" ?>
<Root>
<Person>
<Name>张三</Name>
</Person>
</Root>如何在Root下面再新增一个<Company><Name>公司名称</Name></Company>的子结点
<?xml version="1.0" encoding="GB2312" ?>
<Root>
<Person>
<Name>张三</Name>
</Person>
</Root>如何在Root下面再新增一个<Company><Name>公司名称</Name></Company>的子结点
"<Person>" & vbCrLf & _
" <FirstName>Rod</FirstName>" & vbCrLf & _
" <LastName>Stephens</LastName>" & vbCrLf & _
"</Person>"
DOMDocument的xml属性返回文档的xml描述,可以显示这些返回值看看这些文档究竟是什么样子,也可以将它存储为一个文件,但这完全不必要,因为DOMDocument对象的save方法已经自动将他们存储了。 DOMDocument对象的documentElement属性表示文档数据的根结点,通常情况下操作XML文档都从这里开始。 DOMDocument提供了几种创建新节点的方法。CreateElement方法为文档创建一个新的元素节点,其他创建节点的方法有createAttribute, createProcessingInstruction, 和 createTextNode,在这里就不一一介绍了。 IXMLDOMNode类 IXMLDOMNode类描述了一个节点,该类提供了一系列用于搜索和操纵XML文档的属性和方法。
selectSingleNode 方法用于搜索指定节点的后代,用于搜索指定节点路径的语言称为XPATH,XPATH非常棘手,本文就不详细说明其规范了。下面我们将介绍两个对搜索子节点有特别有用并且简单的方法。 在给selectsingleNode方法中输入子节点的名字,该方法将在节点的子节点进行精确匹配搜索。如果在输入的字符串前面加上".//",那么将搜索节点的全部后代。' 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代表了所有节点的文本内容的组合。 xml.给出了节点的xml内容,例如:"<FirstName>Rod</FirstName>". ChildNodes集合包含了节点的子节点。要给节点增加一个子节点,首先必须给使用DOMDocument对象的节点创建方法,然后将这个新建的节点加入到父节点的childNodes集合中。下面的代码展示了创建一个新的子节点的子程序,并使用父节点的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 the new node.
Set new_node = parent.ownerDocument.createElement(node_name)' Set the node's text value.
new_node.Text = node_value' Add the node to the parent.
parent.appendChild new_node
End Sub
SaveValues 程序 现在我们可以使用XML创建一个简单的程序(如图1),其值存储到XML文件中,在程序开始运行时,程序从VALUE.XML文件中加载数据,在程序运行结束时,将程序中的现行值存入VALUE.XML文件中。 下面的代码是显示了VALUE.XML文件的结构:<Values>
<FirstName>Rod</FirstName>
<LastName>Stephens</LastName>
<Street>1234 Programmer Place</Street>
<City>Bugsville</City>
<State>CO</State>
<Zip>80276</Zip>
</Values> List1显示了怎样编写SaveValues,当载入表单时,form_load事件触发LoadValues子程序。
LoadValues创建了一个名为xml_document的DOMDocument对象,然后载入xml文件,使用selectSingleNode方法查找名为values的节点,然后使用GetNodeValue方法获得从value节点后代中得到的值。 GetNodeValue使用value节点的selectSingleNode方法寻找目标节点,如果节点不存在函数将返回一个缺省值,如果找到这个节点GetNodeValue将返回该节点的text值。对于value.xml文件中的数据节点,text仅仅是包含在节点中的文本内容。 当窗体卸载时触发form_unload事件,unload事件调用SaveValues子程序。程序创建一个新的DOMDocument对象,该对象创建一个新的名为value的节点,然后用文档的appendChild方法将节点添加到文档中。 在创建所有新的节点后,SaveValues调用DOMDocument's save方法存储新的xml文件。 注意这个新的文件已经覆盖了旧文件,使用DOMDocument对象无法部分改变XML文件,可以加载XML文件,然后修改其中一部分,然后保存文件,但原文件将被完全覆盖。这是一个小的缺陷,但在这时可以使用其它程序进行修改。 List1的最后一部分是CreateNode子程序,CreateNode 为父节点创建一个新节点并同时给这个节点赋值。在这个子程序中首先引用一个DOMDocument对象,然后使用该对象的createElement方法创建一个新的节点。 createNode方法设置节点的text属性,然后将节点作为子节点添加到父节点中。List1:Option ExplicitPrivate m_AppPath As StringPrivate Sub Form_Load()
' Get the application's startup path.
m_AppPath = App.Path
If Right$(m_AppPath, 1) <> "\" Then m_AppPath = m_AppPath & "\"' Load the values.
LoadValues
End SubPrivate 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_node As IXMLDOMNode' Load the document.
Set xml_document = New DOMDocument
xml_document.Load m_AppPath & "Values.xml"' If the file doesn't exist, then
' xml_document.documentElement is Nothing.
If xml_document.documentElement Is Nothing Then
' The file doesn't exist. Do nothing.
Exit Sub
End If' Find the Values 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_node, "City", "???")
txtState.Text = GetNodeValue(values_node, "State", "???")
txtZip.Text = GetNodeValue(values_node, "Zip", "???")
End Sub' Return the node's value.
Private Function GetNodeValue(ByVal start_at_node As IXMLDOMNode, _
ByVal node_name As String, _
Optional ByVal default_value As String = "") As String
Dim value_node As IXMLDOMNodeSet 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 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 node.
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 document.
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.createElement(node_name)' Set the node's text value.
new_node.Text = node_value' Add the node to the parent.
parent.appendChild new_node
End Sub
http://www.yesky.com/20021016/1635180_1.shtml
Root下面再新增一个子点就可以了,
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.createElement(node_name)' Set the node's text value.
new_node.Text = node_value' Add the node to the parent.
parent.appendChild new_node
End Sub
Dim sPath As String
Dim oDoc As New DOMDocument
Dim oRoot As IXMLDOMElement
Dim oPerson As IXMLDOMElement
Dim oName As IXMLDOMElement
sPath = App.Path & IIf(Len(App.Path) = 3, "Value.xml", "\Value.xml")
oDoc.async = False
oDoc.Load sPath
If oDoc.parseError.errorCode = 0 Then
Set oRoot = oDoc.documentElement
Set oPerson = oDoc.createElement("Person")
Set oName = oDoc.createElement("Name")
oName.Text = "李四"
oPerson.appendChild oName
oRoot.appendChild oPerson
oDoc.loadXML oRoot.xml
oDoc.save sPath
Set oName = Nothing
Set oPerson = Nothing
Set oRoot = Nothing
Set oDoc = Nothing
End If
End Sub
调用其appendChild方法即可。
'网志:http://blog.csdn.net/xiaoyaolz
'交个朋友,一起编程,学习,一生的朋友
关注,xml太简单了,却又不简单