想用VB 生成xml file
想用VB写个程序用于生成xml file. 但没有什么思路
要求:需要生成好几个xml file, 但每个都有固定的模板,只需要改几个参数值而已(参数值通过VB的界面输入)。
没怎么写过程序,问题可能很幼稚,请见谅。
[解决办法]
Q我:5507350
[解决办法]
两个办法,
一个是采用xml的控件,有相关的函数来写。
还有一个就是将其作为标准的txt 文件,做一个模板,直接去写文件就是了,然后处理模板里面的宏替换标识。
[解决办法]
'先引用MS XML,2.6对象库
'Form1上7个标签,7个文本框
Option Explicit
Private p_AppPath As String
' 返回各个节点的值
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 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
Private Sub LoadValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
' 载入文件
Set xml_document = New DOMDocument
xml_document.Load p_AppPath & "Personal.xml"
If xml_document.documentElement Is Nothing Then
Exit Sub
End If
'寻找节点
Set values_node = xml_document.selectSingleNode("个人信息")
' 读取各个节点的值
txtName.Text = GetNodeValue(values_node, "姓名", "???")
txtSex.Text = GetNodeValue(values_node, "性别", "???")
txtBirthday.Text = GetNodeValue(values_node, "出生日期", "???")
txtAddress.Text = GetNodeValue(values_node, "地址", "???")
txtZip.Text = GetNodeValue(values_node, "邮编", "???")
txtQQ.Text = GetNodeValue(values_node, "QQ", "???")
txtHomepage.Text = GetNodeValue(values_node, "个人主页", "???")
Set xml_document = Nothing
End Sub
' 保存现有的值
Private Sub SaveValues()
Dim xml_document As DOMDocument
Dim values_node As IXMLDOMNode
' 建立XML文件
Set xml_document = New DOMDocument
Set values_node = xml_document.createElement("个人信息")
xml_document.appendChild values_node
CreateNode values_node, "姓名", txtName.Text
CreateNode values_node, "性别", txtSex.Text
CreateNode values_node, "出生日期", txtBirthday.Text
CreateNode values_node, "地址", txtAddress.Text
CreateNode values_node, "邮编", txtZip.Text
CreateNode values_node, "QQ", txtQQ.Text
CreateNode values_node, "个人主页", txtHomepage.Text
' 保存XML文件
xml_document.save p_AppPath & "Personal.xml"
Set xml_document = Nothing
End Sub
Private Sub CreateNode(ByVal parent As IXMLDOMNode, ByVal node_name As String, ByVal node_value As String)
Dim new_node As IXMLDOMNode
Set new_node = parent.ownerDocument.createElement(node_name)
new_node.Text = node_value
parent.appendChild new_node
End Sub
Private Sub Form_Load()
' 获得程序运行目录
p_AppPath = App.Path
If Right$(p_AppPath, 1) <> "" Then p_AppPath = p_AppPath & ""
' 加载值
LoadValues
End Sub
Private Sub Form_Unload(Cancel As Integer)
' 保存现有的值
SaveValues
End Sub