【问题标题】:Parsing a XML Web page file with VBA使用 VBA 解析 XML 网页文件
【发布时间】:2012-10-12 19:29:06
【问题描述】:

我正在研究一个 MS Access 项目 (2003),并希望解析网页返回的 XML 代码。它实际上在我们的 Intranet 上。

比如我想解析https://myLivelinkServer.myEnterprise.com/livelink/livelink.exe?func=search&boolean2=or&where1=something&where1=anything&outputformat=xml

我想将结果放入记录集中。以下是上述页面生成的一些 XML 的示例,其中包含另一个搜索查询(只有 2 个结果):

<?xml version="1.0" encoding="ISO-8859-1"?>
<Output>
    <SearchResults>
        <SearchResult>
            <OTCreatedBy DisplayName="Créé par">
                <![CDATA[5063560]]>
            </OTCreatedBy>
            <OTLocation DisplayName="Emplacement" Protocol="https" ServerName="livelink.myServer.com" URL="/livelink/livelink.exe?func=ll&amp;objId=53080774&amp;objAction=browse">
                2000 1237258 10236341 12909408 14593490 40170727 53076463 53076481 53080774 60751603 
            </OTLocation>
            <OTMIMEType DisplayName="MIMEType" Protocol="https" ServerName="livelink.myServer.com" IconURL="/llk97s/otemail/emailcontainer.gif">

            </OTMIMEType>
            <OTName DisplayName="Nom" Protocol="https" ServerName="livelink.myServer.com" ViewURL="/livelink/livelink.exe?func=ll&amp;objId=60751603&amp;objAction=browse" DownloadURL="/livelink/livelink.exe?func=ll&amp;objID=60751603&amp;objAction=download">
                2008-07-04 09 22 - ACCUSÉ DE RÉCEPTION - DEMANDE D'ACHAT # 12854509 - Louise Plante
            </OTName>
            <OTObjectDate DisplayName="Date" Mask="YYYY-MM-DD">
                2012-05-29
            </OTObjectDate>
            <OTObjectSize DisplayName="Taille" Suffix="Eléments">
                3
            </OTObjectSize>
            <Functions Protocol="https" ServerName="livelink.myServer.com" ServerPort="443" URL="/livelink/livelink.exe?func=SearchResultsFunctionMenu&amp;hitID=1&amp;searchCacheID=2006218356&amp;version=0&amp;dataID=60751603&amp;HH=false&amp;subtype=264" />
        </SearchResult>
        <SearchResult>
            <OTCreatedBy DisplayName="Créé par">
                <![CDATA[5063560]]>
            </OTCreatedBy>
            <OTLocation DisplayName="Emplacement" Protocol="https" ServerName="livelink.myServer.com" URL="/livelink/livelink.exe?func=ll&amp;objId=60739493&amp;objAction=browse">

            </OTLocation>
            <OTMIMEType DisplayName="MIMEType" Protocol="https" ServerName="livelink.myServer.com" IconURL="/llk97s/webdoc/outlook.gif">
                application/x-outlook-msg
            </OTMIMEType>
            <OTName DisplayName="Nom" Protocol="https" ServerName="livelink.myServer.com" ViewURL="/livelink/livelink.exe?func=ll&amp;objId=60751711&amp;objAction=Open&amp;vernum=1&amp;nexturl=%2Flivelink%2Flivelink%2Eexe%3Ffunc%3Dsearch%26where1%3D12854509%26boolean2%3Dor%26where2%3D3091%2D40014%2D024%2D01%2D0%2DCE%2DL%2DQUF28%2D01%2DUA%26outputformat%3Dxml%26gofor%3D2" DownloadURL="/livelink/livelink.exe?func=ll&amp;objID=60751711&amp;objAction=download">
                2008-07-04 09 22 - ACCUSÉ DE RÉCEPTION - DEMANDE D'ACHAT # 12854509 - Louise Plante
            </OTName>
            <OTObjectDate DisplayName="Date" Mask="YYYY-MM-DD">
                2008-07-04
            </OTObjectDate>
            <OTObjectSize DisplayName="Taille" Suffix="Ko">
                23
            </OTObjectSize>
            <Functions Protocol="https" ServerName="livelink.myServer.com" ServerPort="443" URL="/livelink/livelink.exe?func=SearchResultsFunctionMenu&amp;hitID=2&amp;searchCacheID=2006218356&amp;version=1&amp;dataID=60751711&amp;HH=false&amp;subtype=264" />
        </SearchResult>
    </SearchResults>

    <!-- File: websbroker/resultbar.xml -->
    <SearchResultsInformation>
        <CurrentStartAt>0</CurrentStartAt>
        <NumberResultsThisPage>2</NumberResultsThisPage>
        <RawTotalResults>123</RawTotalResults>
        <EstTotalResults>66</EstTotalResults>
        <TotalResults>3</TotalResults>
        <PreviousQuery>
            <Parameter Name="where1"><![CDATA[12854509]]></Parameter>
            <Parameter Name="boolean2"><![CDATA[or]]></Parameter>
            <Parameter Name="where2"><![CDATA[3091-40014-024-01-0-CE-L-QUF28-01-UA]]></Parameter>
            <Parameter Name="outputformat"><![CDATA[xml]]></Parameter>
            <Parameter Name="gofor"><![CDATA[2]]></Parameter>
        </PreviousQuery>
    </SearchResultsInformation>
    <!-- End File: websbroker/resultbar.xml -->
</Output>

有没有一种简单的方法可以用 VBA(显然没有任何浏览器)打开该页面并解析它?

【问题讨论】:

    标签: vba ms-access xml-parsing livelink


    【解决方案1】:

    您可以将 URL 作为 MSXML DOMDocument 加载,然后使用其方法解析出您想要的值。

    您可以在这个 SO 问题中找到一个简短的代码大纲示例:Late Binding conversion for XML import syntax

    这里有更多的 VBA 代码示例:site:stackoverflow.com vba msxml

    【讨论】:

      猜你喜欢
      • 2012-07-15
      • 1970-01-01
      • 1970-01-01
      • 2019-07-29
      • 2021-05-23
      • 1970-01-01
      • 2013-03-28
      • 2018-03-12
      相关资源
      最近更新 更多