如何解析XML文件并写入数据,然后保存 [英] How to parse an XML file and write in data then save it

查看:171
本文介绍了如何解析XML文件并写入数据,然后保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理如下事情:


  1. 带有一些单元格的Excel表格,最终用户被要求输入
    所需值;完成

  2. 最终用户读取这些单元格
    中输入的值的代码;

  3. 加载XML文件;完成

  4. 解析它,然后将从Excel单元格检索的值写入XML文件(遵循规则),然后保存;我被困在这里

  5. 启动我的应用程序(另一个脚本会在以后使用XML文件值)。

  1. An Excel sheet with some cells where the end user is asked to enter required values; done
  2. Code that read values entered in these cells by end users;
  3. Load an XML file; done
  4. Parse it and then write the values retrieved from Excel cells into XML file (following a rule) then save it; I am stuck here!
  5. Start my application (another script will use the XML file values later).

将尽可能简化:

Excel文件将如以下示例...

The Excel file will be like the following example...

   CellA     CellB
1  T1        V1
2  T2        V2
3  T3        V3
4  T4        V4
5  T5        V5
6  T6        V6

T表示标题。最终用户将输入值V1,V2,... V6

"T" refers to title. The end user will enter the Values V1, V2, ... V6

XML文件的结构如下:

The XML file is structured like this:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Environment>
    <Variable>
        <Name></Name>
        <Caption>T1</Caption>
        <Type>TEXT</Type>
        <Value>V1</Value>
        <Description></Description>
    </Variable>
        <Variable>
        <Name></Name>
        <Caption>T2</Caption>
        <Type>TEXT</Type>
        <Value>V2</Value>
        <Description></Description>
    </Variable>
        <Variable>
        <Name></Name>
        <Caption>T3</Caption>
        <Type>TEXT</Type>
        <Value>V3</Value>
        <Description></Description>
    </Variable> <Variable>
        <Name></Name>
        <Caption>T4</Caption>
        <Type>TEXT</Type>
        <Value>V4</Value>
        <Description></Description>
    </Variable> <Variable>
        <Name></Name>
        <Caption>T5</Caption>
        <Type>TEXT</Type>
        <Value>V5</Value>
        <Description></Description>
    </Variable>
    </Variable> <Variable>
        <Name></Name>
        <Caption>T6</Caption>
        <Type>TEXT</Type>
        <Value>V6</Value>
        <Description></Description>
    </Variable>
</Environment>

如您所见,我需要解析此文件并输入值(V1 .... V6)参考每一个。

As you can see I need to parse this file and enter values (V1....V6) into with reference to for each one.

以下是我的VBA代码,直到我被卡住的行:

Below is my VBA code until the line where I am stuck:

'Option Explicit
Private Sub RunTest_Click()

Dim envFrmwrkPath As String
Dim ApplicationName As String
Dim TestIterationName, ServerIp, Login, Password, TraderLiteLogPath  As String
Dim objfso, app, Eval As Object
Dim i, Msgarea`enter code here`

Dim EnvVarXML As MSXML2.DOMDocument60

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'''these are added when trying to find a way to parse the xml --- you can change them
Dim Variable As MSXML2.IXMLDOMNode
'Dim oAttributes As MSXML.IXMLDOMNamedNodeMap
Dim ORoot As MSXML2.IXMLDOMNode
Dim objUIElement As MSXML2.IXMLDOMElement
Dim OChildren As MSXML2.IXMLDOMNodeList
Dim OChild As MSXML2.IXMLDOMNode
Dim OVariable As MSXML2.IXMLDOMNode
Dim OAttributes As MSXML2.IXMLDOMNamedNodeMap
'Dim objUIElement As Object
Dim field As Object
''''''''''''''''''''''''''''''''''''''''''''''''''''


'load Env Variables from Excel

ApplicationName = ActiveSheet.Range("E4").Value
envFrmwrkPath = ActiveSheet.Range("E6").Value
TestIterationName = ActiveSheet.Range("E8").Value
ServerIp = ActiveSheet.Range("E10").Value
Login = ActiveSheet.Range("E12").Value
Password = ActiveSheet.Range("E14").Value
TraderLiteLogPath = ActiveSheet.Range("E16").Value

'load xml file
Set objParser = CreateObject("Microsoft.XMLDOM")
Set EnvVarXML = New MSXML2.DOMDocument60


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''test_load
'''''''''''''''-----------------------------------------------------
'Set EnvVarXML = CreateObject("Microsoft.XMLDOM")
'Set EnvVarXML = New MSXML2.DOMDocument60
 'If EnvVarXML.Load(envFrmwrkPath & "\Environment\EnvVar.xml") Then
  ' for debug only
  'MsgBox "file loaded correctly", vbOKOnly
  ' for debug only
 'Else
 ' for debug only
 'MsgBox "file not loaded", vbcrtical
 ' for debug only
  'End If
'''''''''''''''-----------------------------------------------------
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'load xml file
EnvVarXML.Load (envFrmwrkPath & "\Environment\EnvVar.xml")

 'parse file and change values
 '''the following may have no sense for an experiment one of you
 Set ORoot = EnvVarXML.DocumentElement
 For Each OVariable In ORoot.ChildNodes
   Set OAttributes = OVariable.Attributes
   Set OChildren = OVariable.ChildNodes
    '''deleted many lines as found no way ''''''' 
       Set EnvVarXML = Nothing
 Next
    EnvVarXML.Save (envFrmwrkPath & "\Environment\EnvVar.xml")


推荐答案

这是一些可以帮助您开始的代码

Here is some code that can help you get started

Dim doc As DOMDocument
Set doc = New DOMDocument
doc.Load "C:\x.xml"
Dim Variables As IXMLDOMNodeList
Dim variable As IXMLDOMNode
Set Variables = doc.SelectNodes("/Environment/Variable")
For Each variable In Variables
    Debug.Print variable.SelectNodes("Caption").Item(0).Text
    Debug.Print variable.SelectNodes("Type").Item(0).Text
Next

要使其正常工作,请选择工具 - 引用,然后选择Microsoft XML v6.0。
有很多方法可以解决这个问题,但XPath(SelectNodes中使用的语言)在这种情况下是非常好的。

To make it work, select Tools - References and select Microsoft XML v6.0. There are many ways to solve this, but XPath (the language used in SelectNodes) is very good to know in cases like this.

这篇关于如何解析XML文件并写入数据,然后保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆