2016-11-12 222 views
0

我有以下格式的數據並且在Excel工作表中有類似的數據。如何在VBA中解析XML文件

<LegalEntityDataVO> 
    <LegalEntityDataVORow> 
     <Name>Siemens Corporation</Name> 
     <LegalEntityIdentifier>010</LegalEntityIdentifier> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>Siemens Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Income Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Federal Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>US Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Service Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Oil Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
    </LegalEntityDataVORow> 
<LegalEntityDataVO> 

所以我的要求是比較Excel數據和XML數據。具體來說,我的任務描述如下:

If **LegalEntityIdentifier** value in Excel = **LegalEntityIdentifier** value in xml then 

( 
If(**MainEstablishmentFlag** value in Excel = **MainEstablishmentFlag** value in Xml then 

    (
     Compare **Name** in Excel with **Name** in XML 
    ) 
) 


**LegalEntityIdentifier** childnode of LegalEntityDataVORow 

**MainEstablishmentFlag** childnode of EstablishmentDataVORow 

**Name** childnode of RegistrationDataEtbVORow 

這裏是我所面臨的問題:

  1. LegalEntityDataVORow包含許多EstablishmentDataVORow
  2. EstablishmentDataVORow包含許多RegistrationDataEtbVORow

在我的XML文件中,我有100 <LegalEntityDataVORow>。我如何在VBA中運行上述任務?

+0

http://stackoverflow.com/questions/11305/how-to-parse-xml-using-vba – cullan

+0

XML是一種樹狀結構的文檔和Excel數據是單調的兩維度格式。所以儘管內容可能,但這兩者不能相似。請顯示錶格Excel數據。 – Parfait

回答

1

將下面的代碼生成的代碼下面的輸出輸出以下文件:

代碼:

Sub parse_data() 

    Dim strXmlFileName As String: strXmlFileName = "Drive:\Path\Filename.xml" 
    Dim docXmlDocument As New MSXML2.DOMDocument60 

    Dim wsDataToCompare As Worksheet: Set wsDataToCompare = ActiveWorkbook.Sheets("DataToCompare") 
    Dim strLegalEntityIdentifierToCompare As String: strLegalEntityIdentifierToCompare = wsDataToCompare.Cells(1, 1).Value 
    Dim strMainEstablishmentFlagToCompare As String: strMainEstablishmentFlagToCompare = wsDataToCompare.Cells(2, 1).Value 

    Dim ndeEntityData As IXMLDOMNode 
    Dim ndeEntityDataChild As IXMLDOMNode 
    Dim ndeEstablishmentData As IXMLDOMNode 

    Dim strNameToExtract As String 

    docXmlDocument.Load strXmlFileName 

    For Each ndeEntityData In docXmlDocument.DocumentElement.ChildNodes 

     If ndeEntityData.SelectSingleNode("LegalEntityIdentifier").Text = strLegalEntityIdentifierToCompare Then 

      For Each ndeEntityDataChild In ndeEntityData.ChildNodes 

       If ndeEntityDataChild.BaseName = "EstablishmentData" Then 

        If ndeEntityDataChild.SelectSingleNode("EstablishmentDataVORow/MainEstablishmentFlag").Text = strMainEstablishmentFlagToCompare Then 

         strNameToExtract = ndeEntityDataChild.SelectSingleNode("EstablishmentDataVORow/Name").Text 
         Debug.Print strNameToExtract 

        End If 

       End If 

      Next ndeEntityDataChild 

     End If 

    Next ndeEntityData 

End Sub 

輸出:

Siemens Corporation 
US Corporation 

注意,我不得不擴大您的再次使XML文件有效。我使用的文件是:

<?xml version="1.0" encoding="UTF-8"?> 
<LegalEntityDataVO> 
    <LegalEntityDataVORow> 
     <Name>Siemens Corporation</Name> 
     <LegalEntityIdentifier>010</LegalEntityIdentifier> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>Siemens Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Income Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PROFILES</SourceTable> 
        <Name>United States Federal Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
     <EstablishmentData> 
     <EstablishmentDataVORow> 
      <MainEstablishmentFlag>Y</MainEstablishmentFlag> 
      <Name>US Corporation</Name> 
      <GeographyCode>US</GeographyCode> 
      <RegistrationDataEtb> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Service Tax</Name> 
       </RegistrationDataEtbVORow> 
       <RegistrationDataEtbVORow> 
        <SourceTable>XLE_ETB_PAYBLES</SourceTable> 
        <Name>United States Oil Tax</Name> 
       </RegistrationDataEtbVORow> 
      </RegistrationDataEtb> 
     </EstablishmentDataVORow> 
     </EstablishmentData> 
    </LegalEntityDataVORow> 
</LegalEntityDataVO>