如何解析XML文件,并在数据写入然后将其保存(How to parse an XML file an

2019-10-19 07:09发布

我的工作类似如下:

  1. Excel工作表的一些细胞,其中所述最终用户要求输入所需的值; DONE
  2. 读取这些细胞由最终用户输入的值的代码;
  3. 加载XML文件; DONE
  4. 解析它然后写(以下规则)然后将其保存从Excel细胞成XML文件中检索到的值; 我困在这里!
  5. 开始我的应用程序(另一种脚本以后将使用XML文件中的值)。

我将简化尽可能:

Excel文件会像下面的例子...

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

“T”指的称号。 最终用户将进入价值V1,V2,V6 ...

该XML文件的结构是这样的:

<?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)为参照的每一个。

下面是我的VBA代码,直到我在哪里卡住行:

'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")

Answer 1:

下面是一些代码,可以帮助你开始

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 6.0版。 有很多方法来解决这个问题,但XPath的(在的SelectNodes使用的语言)是非常好的在这样的情况就知道了。



文章来源: How to parse an XML file and write in data then save it