使用excel vba读取自定义xml输出

时间:2013-04-22 07:54:02

标签: excel-vba xml-parsing vba excel

我们的一个系统以下列格式向我们提供XML。

你能帮我解决一下如何解析这个XML并使用excel 2010 vba将它存储到数组中吗?

我想阅读标签中的内容。

提前感谢您的帮助。

<report_output>
<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-'
     xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-'
     xmlns:rs='urn:schemas-microsoft-com:rowset'
     xmlns:z='#RowsetSchema'
     content="Report"
     title="CURVE REPORT"
     resultHeading="DUMPCURVE"
     resultName="CURVE REPORT"
     runat="04/22/13"
     user="xxx"
     database="xxx"
     version="xxx"
     applicationdate="04/22/13"
     >
  <s:Schema id='RowsetSchema'>
    <s:ElementType name='row' content='eltOnly'>
<s:attribute type='GeneratedPK' />      <s:attribute type='Ccy'/>
      <s:attribute type='dmIndex'/>
      <s:attribute type='CurveID'/>
      <s:attribute type='CurveDate'/>
      <s:attribute type='Days'/>
      <s:attribute type='Rate'/>
      <s:extends type='rs:rowbase'/>
    </s:ElementType>
<s:AttributeType name='GeneratedPK' rs:number='1' rs:maybenull='false' 
rs:keycolumn='true' rs:autoincrement='true' rs:writeunknown='true'>
<s:datatype dt:type='int' />
</s:AttributeType>
    <s:AttributeType name='Ccy' rs:number='2' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='string' dt:maxLength='4' origDataType='STRING'/>
    </s:AttributeType>
    <s:AttributeType name='dmIndex' rs:number='3' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='string' dt:maxLength='6' origDataType='STRING'/>
    </s:AttributeType>
    <s:AttributeType name='CurveID' rs:number='4' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='string' dt:maxLength='9' origDataType='STRING'/>
    </s:AttributeType>
    <s:AttributeType name='CurveDate' rs:number='5' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='string' dt:maxLength='10' origDataType='DATE  '/>
    </s:AttributeType>
    <s:AttributeType name='Days' rs:number='6' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='string' dt:maxLength='35' origDataType='STRING'/>
    </s:AttributeType>
    <s:AttributeType name='Rate' rs:number='7' rs:maybenull='true' rs:writeunknown='true'>
      <s:datatype dt:type='number' dt:maxLength='50' origDataType='RATES '/>
    </s:AttributeType>
  </s:Schema>
  <rs:data>
  <z:row
 GeneratedPK='1'
    Ccy='xxx'
    dmIndex='xxx'
    CurveID='xxxx'
    CurveDate='2013-04-23T00:00:00'
    Days='1'
    Rate='0000'
  />
  <z:row
 GeneratedPK='2'
    Ccy='xxx'
    dmIndex='xxxxx'
    CurveID='xxxxx'
    CurveDate='2013-05-24T00:00:00'
    Days='32'
    Rate='00000000'
  />
  <z:row
 GeneratedPK='3'
    Ccy='xxx'
    dmIndex='xxx'
    CurveID='xxxx'
    CurveDate='2013-04-23T00:00:00'
    Days='1'
    Rate='0000'
  />
  <z:row
 GeneratedPK='4'
    Ccy='xxx'
    dmIndex='xxxxx'
    CurveID='xxxxx'
    CurveDate='2013-05-24T00:00:00'
    Days='32'
    Rate='00000000'
  />
<z:row
 GeneratedPK='5'
    Ccy='xxx'
    dmIndex='xxx'
    CurveID='xxxx'
    CurveDate='2013-04-23T00:00:00'
    Days='1'
    Rate='0000'
  />
  <z:row
 GeneratedPK='6'
    Ccy='xxx'
    dmIndex='xxxxx'
    CurveID='xxxxx'
    CurveDate='2013-05-24T00:00:00'
    Days='32'
    Rate='00000000'
  />
</rs:data>
</xml>




</report_output>

到目前为止,在excel vba中我尝试了以下代码,但无法继续进行

Dim xmldoc As New MSXML2.DOMDocument
xmldoc.Load ("C:\DOCUME~1\xyz\LOCALS~1\Temp\zr_aud_bbsw_130422.xml")

2 个答案:

答案 0 :(得分:2)

感谢Philip的帮助,我可以解析我的XML。以下是我的最终代码,它读取XML并为我提取存储在属性

下的数据
Option Explicit
Public Sub LoadDocument()
Dim xDoc As MSXML.DOMDocument
Set xDoc = New MSXML.DOMDocument
xDoc.validateOnParse = False
If xDoc.Load("C:\LOG\zr_aud_bbsw_130422.xml") Then
   ' The document loaded successfully.
   ' Now do something intersting.
   DisplayNode xDoc.ChildNodes, 0
Else
   ' The document failed to load.
   ' See the previous listing for error information.
End If
End Sub

Public Sub DisplayNode(ByRef Nodes As MSXML.IXMLDOMNodeList, _
   ByVal Indent As Integer)

   Dim xNode As MSXML.IXMLDOMNode
   Dim xAttribute As MSXML.IXMLDOMAttribute
   Indent = Indent + 2

   For Each xNode In Nodes
      If xNode.NodeType = NODE_ELEMENT And Trim(xNode.ParentNode.nodeName) = "rs:data" Then
        For Each xAttribute In xNode.Attributes
            Debug.Print Space$(Indent) & xAttribute.BaseName & _
            ":" & xAttribute.NodeValue
        Next xAttribute

      End If

      If xNode.HasChildNodes Then
         DisplayNode xNode.ChildNodes, Indent
      End If
   Next xNode
End Sub

答案 1 :(得分:0)

不要忘记选项使用xpath语法来选择节点

这将返回所有行节点的节点列表

   Dim nodelist As MSXML2.IXMLDOMNodeList
   Set nodelist = xDoc.SelectNodes("//rs:data/z:row")

使用xpath可以在一个或多个属性上放置条件,例如

   Dim nodelist As MSXML2.IXMLDOMNodeList
   Set nodelist = xDoc.SelectNodes("//rs:data/z:row[@CurveDate='2013-04-23T00:00:00']")

[]括号可以放在xpath表达式中的其他和多个节点上,并且可以使用通配符,并且可以使用前向和前向引用父节点或子节点。

在此处详细了解xpath http://www.w3schools.com/xpath/xpath_syntax.asp