在Excel VBA MSXML2.ServerXMLHTTP中检索节点

时间:2016-04-05 19:48:47

标签: xml vba excel-vba xml-parsing excel

我编写了一个简单的Excel 2010 VBA宏来从REST API检索XML输出。我可以将整个XML输出放在相邻的单元格中。我现在要做的是解析输出,以便只将名为ab的节点放入相邻的单元格中。我作弊并允许返回整个XML内容,然后只使用MID函数来查找ab和/ ab之间的字符串但是它很乱。我想学习如何正确地执行此操作,以便将来可以解析任何特定节点并将其放在相邻列中。

这是Excel中的VBA宏“SendID”,它提交URL + ActiveCell值并将SourceHTMLText返回到相邻列。我不得不阻止用户名和密码,因此它不适合你,但下面提供了一个样本返回:

Sub SendID()

    Set objHTTP = CreateObject("MSXML2.ServerXMLHTTP")
    URL = "URL for REST API goes here" & ActiveCell.Value
    objHTTP.Open "GET", URL, False
    objHTTP.setRequestHeader "User-Agent", _
                   "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)"
    objHTTP.send ("")
    SourceHTMLText = objHTTP.responseText



  'If the return has the word Apache in it then there was no return.  
  '  Put no DOI in the adjacent cell and move on.

  If InStr(SourceHTMLText, "Apache") > 0 Then
      ActiveCell.Offset(0, 1).Value = "no DOI"
  Else
      ' Place the value in the adjacent cell
      ActiveCell.Offset(0, 1).Value = SourceHTMLText
  End If

End Sub

以下是一个REST API调用的输出。

"<?xml version=""1.0""?>
<searchResponse xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">
  <Hits xmlns=""http://epnet.com/webservices/SearchService/Response/2007/07/"">1</Hits>
  <Statistics xmlns=""http://epnet.com/webservices/SearchService/Response/2007/07/"">
    <Statistic>
      <Database>ffh</Database>
      <Hits>1</Hits>
    </Statistic>
  </Statistics>
  <SearchResults xmlns=""http://epnet.com/webservices/SearchService/Response/2007/07/"">
    <records xmlns="""">
      <rec recordID=""1"">
        <pdfLink />
        <plink>http://search.ebscohost.com/login.aspx?direct=true&amp;db=ffh&amp;AN=2015-06-Ne0978&amp;site=ehost-live</plink>
        <header shortDbName=""ffh"" uiTerm=""2015-06-Ne0978"" longDbName=""FSTA - Food Science and Technology Abstracts"" uiTag=""AN"">
          <controlInfo>
            <bkinfo>
              <aug />
            </bkinfo>
            <dissinfo />
            <confinfo />
            <jinfo>
              <jid type=""issn"">20487177</jid>
              <jtl>Food Science &amp; Nutrition</jtl>
              <issn type=""print"">20487177</issn>
            </jinfo>
            <pubinfo>
              <dt year=""2014"" month=""01"" day=""01"" />
              <vid>2</vid>
              <iid>6</iid>
            </pubinfo>
            <artinfo>
              <ui>2015-06-Ne0978</ui>
              <ppf>786</ppf>
              <ppct>6</ppct>
              <tig>
                <atl>Fatty acid profile of gamma-irradiated and cooked African oil bean seed (<i>Pentaclethra macrophylla</i> Benth).</atl>
              </tig>
              <aug>
                <au>Olotu, I.</au>
                <au>Enujiugha, V.</au>
                <au>Obadina, A.</au>
                <au>Owolabi, K.</au>
              </aug>
              <su>AFRICAN OIL BEANS</su>
              <su>COOKING</su>
              <su>FATTY ACIDS</su>
              <su>GAMMA IRRADIATION</su>
              <su>IRRADIATION</su>
              <su>LEGUMES</su>
              <su>OILS</su>
              <su>OILSEEDS</su>
              <su>RANCIDITY</su>
              <su>Fats, oils and margarine</su>
              <ab>The safety and shelf-life of food products can be, respectively, ensured and extended with important food-processing technologies such as irradiation. The joint effect of cooking and 10 kGy gamma irradiation on the fatty acid composition of the oil of <i>Pentaclethra macrophylla</i> Benth was evaluated. Oils from the raw seed, cooked seeds, irradiated seeds (10 kGy), cooked, and irradiated seeds (10 kGy) were extracted and analyzed for their fatty acid content. An omega-6-fatty acid (linoleic acid) was the principal unsaturated fatty acid in the bean seed oil (24.6%). Cooking significantly (<i>P </i>&lt; 0.05) increased Erucic acid by 3.3% and Linolenic acid by 23.0%. Combined treatment significantly (<i>P</i>&lt; 0.05) increased C18:2, C6:0, C20:2, C18:3, C20:3, C24:0, and C22:6 being linoleic, caproic, eicosadienoic, linolenic, eicosatrienoic, ligoceric, and docosahexaenoic acid, respectively, and this increase made the oil sample to have the highest total fatty acid content (154.9%), unsaturated to saturated fatty acid ratio (109.6), and unsaturated fatty acid content (153.9%). 10 kGy irradiation induces the formation of C20:5 (eicosapentaenoic), while cooking induced the formation of C20:4 (arachidic acid), C22:6 (Heneicosanoic acid), and C22:2 (docosadienoic acid). Combined 10 kGy cooking and irradiation increased the susceptibility of the oil of the African oil bean to rancidity.</ab>
              <pubtype>Journal Article</pubtype>
              <doctype>Journal Article</doctype>
              <ougenre>article</ougenre>
            </artinfo>
            <holdings islocal=""N"" />
          </controlInfo>
        </header>
      </rec>
    </records>
  </SearchResults>
</searchResponse>"

提前谢谢。

0 个答案:

没有答案