2016-11-12 7 views
0

У меня есть данные в следующем формате и имеют похожие данные в листе Excel.Как проанализировать XML-файл в VBA

<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-File еще раз, чтобы сделать его действительным. Файл я использовал:

<?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> 
Смежные вопросы