Search code examples
excelxmlvbaweb-scrapingxmlhttprequest

Extract data from XML response in XMLHTTP VBA


I am trying to get the response of POST method and I already got a point using the code

Sub Test()
    Dim http As New XMLHTTP60, html As New HTMLDocument, ws As Worksheet, myUrl As String, postData As String
    Set ws = ThisWorkbook.Worksheets("Sheet1")
    myUrl = "http://idahoparcels.us:8080/cgi-bin/mapserv.exe?MAP=%2Fms4w%2Fapps%2Fdt%2F.%2Fdemo%2Fparcels%2Felmore%2Fparcels.map&typename=ms%3Aparcels"
    postData = CreateObject("Scripting.FileSystemObject").OpenTextFile(ThisWorkbook.Path & "\FormData.txt").ReadAll
    With http
        .OPEN "POST", myUrl, False
        .send postData
        html.body.innerHTML = .responseText
        
        'Stuck Here
        
    End With
End Sub

** The contents in the FormData.txt is like that

<GetFeature xmlns="http://www.opengis.net/wfs" service="WFS" version="1.1.0"
 outputFormat="text/xml; subtype=gml/2.1.2" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  xsi:schemaLocation="http://www.opengis.net/wfs 
   http://schemas.opengis.net/wfs/1.1.0/wfs.xsd">
       <Query typeName="ms:parcels" srsName="EPSG:4326">
       <Filter xmlns="http://www.opengis.net/ogc">
       <PropertyIsLike wildCard="%" singleChar="_" escapeChar="\"
           matchCase="false">
          <PropertyName>PARCEL_ID</PropertyName>
          <Literal>%RP01N07E210010%</Literal>
       </PropertyIsLike>
 </Filter>
 </Query>
 </GetFeature>

The output or response is like XML .. but I have no great idea of how to deal with XML as response Can you guide me with one example only so as to learn about that? For example: I need "ms:ownership" which should be "USDA FOREST SERVICE"


Solution

  • Here's an example:

    
        Sub Test()
            
            Dim http As New XMLHTTP60, ws As Worksheet, myUrl As String, postData As String
            Dim doc As MSXML2.DOMDocument60, xml As String, els, el
            
            Set ws = ThisWorkbook.Worksheets("Sheet1")
            myUrl = "http://idahoparcels.us:8080/cgi-bin/mapserv.exe?MAP=%2Fms4w%2Fapps%2Fdt%2F" & _
                    ".%2Fdemo%2Fparcels%2Felmore%2Fparcels.map&typename=ms%3Aparcels"
            
            postData = ws.Range("A1").Value 'loading from cell for testing...
            
            With http
                .Open "POST", myUrl, False
                .send postData
                xml = .responseText
                ws.Range("B1").Value = xml 'log response for review
            End With
            
            Set doc = New MSXML2.DOMDocument60
            
            'You must add any namespace(s) for any desired elements
            'Watch the formatting...
            doc.SetProperty "SelectionNamespaces", _
                "xmlns:ms='http://mapserver.gis.umn.edu/mapserver' " & _
                "xmlns:wfs='http://www.opengis.net/wfs' " & _
                "xmlns:gml='http://www.opengis.net/gml' " & _
                "xmlns:ogc='http://www.opengis.net/ogc'"
            
            doc.LoadXML xml
            
            Set els = doc.SelectNodes("//ms:OWNERSHIP")
            For Each el In els
                Debug.Print el.xml
                Debug.Print el.Text
            Next el
        
        End Sub