Jump to content

Parse Xml result


rootx
 Share

Recommended Posts

Hi, I need I little help to finish my script.

I would like to extract the type value and put it in a variable, and then use what I'm interested in Ex: street_number and country etc...

I miss a pass in my loop !!!  My Target is: The couple of value is "type" and the correspondent "long_name"

Script

#include <File.au3>

$xml = @ScriptDir&"\xml.xml"

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($xml)

$status = $oXML.SelectSingleNode("//GeocodeResponse/status")
If  $status.text = "OK" Then
    $oNodes = $oXML.SelectNodes("//result")
    For $oNode In $oNodes ;<--------------------------------------------result
        $oChilds = $oNode.childNodes
        For $oChild In $oChilds ;"<---------------------------------- result child
            If $oChild.tagName = "address_component" Then
            $ads = $oChild.childNodes
            For $ad in $ads ;<-------------------------------------- address component
                ;???????? how filter type??
                ConsoleWrite($ad.tagName & " = " & $ad.text & @LF)
            Next
            EndIf
        Next
    Next
Else
    MsgBox("","","Error")
EndIf

XML

<?xml version="1.0" encoding="UTF-8"

 

Link to comment
Share on other sites

Hi @rootx.

I'm not 100% sure i know what you mean by filter.

here's the code with what i understood it as.

#cs

<?xml version="1.0" encoding="UTF-8"?>
<GeocodeResponse>
 <status>OK</status>
 <result>
  <type>street_address</type>
  <formatted_address>Via Canavelle, 6, 17019 Varazze SV, Italien</formatted_address>
  <address_component>
   <long_name>6</long_name>
   <short_name>6</short_name>
   <type>street_number</type>
  </address_component>
  <address_component>
   <long_name>Via Canavelle</long_name>
   <short_name>Via Canavelle</short_name>
   <type>route</type>
  </address_component>
  <address_component>
   <long_name>Varazze</long_name>
   <short_name>Varazze</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Varazze</long_name>
   <short_name>Varazze</short_name>
   <type>administrative_area_level_3</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Provincia di Savona</long_name>
   <short_name>SV</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Liguria</long_name>
   <short_name>Liguria</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Italien</long_name>
   <short_name>IT</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>17019</long_name>
   <short_name>17019</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>44.3816280</lat>
    <lng>8.5732980</lng>
   </location>
   <location_type>ROOFTOP</location_type>
   <viewport>
    <southwest>
     <lat>44.3802790</lat>
     <lng>8.5719490</lng>
    </southwest>
    <northeast>
     <lat>44.3829770</lat>
     <lng>8.5746470</lng>
    </northeast>
   </viewport>
  </geometry>
  <place_id>ChIJBbaJyAEi0xIRvw7CuvuWSAk</place_id>
 </result>
 <result>
  <type>locality</type>
  <type>political</type>
  <formatted_address>17019 Varazze, Savona, Italien</formatted_address>
  <address_component>
   <long_name>Varazze</long_name>
   <short_name>Varazze</short_name>
   <type>locality</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Varazze</long_name>
   <short_name>Varazze</short_name>
   <type>administrative_area_level_3</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Savona</long_name>
   <short_name>SV</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Ligurien</long_name>
   <short_name>Ligurien</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Italien</long_name>
   <short_name>IT</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>17019</long_name>
   <short_name>17019</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>44.3594336</lat>
    <lng>8.5773126</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>44.3479178</lat>
     <lng>8.5537618</lng>
    </southwest>
    <northeast>
     <lat>44.3836989</lat>
     <lng>8.6216356</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>44.3479178</lat>
     <lng>8.5537618</lng>
    </southwest>
    <northeast>
     <lat>44.3836989</lat>
     <lng>8.6216356</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJ5U2aabEY0xIRF7RN5OI8mC0</place_id>
 </result>
 <result>
  <type>administrative_area_level_3</type>
  <type>political</type>
  <formatted_address>17019 Varazze, Savona, Italien</formatted_address>
  <address_component>
   <long_name>Varazze</long_name>
   <short_name>Varazze</short_name>
   <type>administrative_area_level_3</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Savona</long_name>
   <short_name>SV</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Ligurien</long_name>
   <short_name>Ligurien</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Italien</long_name>
   <short_name>IT</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>17019</long_name>
   <short_name>17019</short_name>
   <type>postal_code</type>
  </address_component>
  <geometry>
   <location>
    <lat>44.3890436</lat>
    <lng>8.5611142</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>44.3479178</lat>
     <lng>8.5128981</lng>
    </southwest>
    <northeast>
     <lat>44.4343216</lat>
     <lng>8.6331957</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>44.3479178</lat>
     <lng>8.5128981</lng>
    </southwest>
    <northeast>
     <lat>44.4343216</lat>
     <lng>8.6331957</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJg2lMrUof0xIREoZS3Ai0BhI</place_id>
 </result>
 <result>
  <type>postal_code</type>
  <formatted_address>17019 Varazze, Savona, Italien</formatted_address>
  <address_component>
   <long_name>17019</long_name>
   <short_name>17019</short_name>
   <type>postal_code</type>
  </address_component>
  <address_component>
   <long_name>Varazze</long_name>
   <short_name>Varazze</short_name>
   <type>administrative_area_level_3</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Savona</long_name>
   <short_name>SV</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Ligurien</long_name>
   <short_name>Ligurien</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Italien</long_name>
   <short_name>IT</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>44.3890436</lat>
    <lng>8.5611142</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>44.3479420</lat>
     <lng>8.5128780</lng>
    </southwest>
    <northeast>
     <lat>44.4343450</lat>
     <lng>8.6331740</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>44.3479420</lat>
     <lng>8.5128780</lng>
    </southwest>
    <northeast>
     <lat>44.4343450</lat>
     <lng>8.6331740</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJW5QImx0i0xIRMEu3k4DmBRw</place_id>
 </result>
 <result>
  <type>administrative_area_level_2</type>
  <type>political</type>
  <formatted_address>Savona, Italien</formatted_address>
  <address_component>
   <long_name>Savona</long_name>
   <short_name>SV</short_name>
   <type>administrative_area_level_2</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Ligurien</long_name>
   <short_name>Ligurien</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Italien</long_name>
   <short_name>IT</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>44.2887995</lat>
    <lng>8.2650580</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>43.9389590</lat>
     <lng>7.9774583</lng>
    </southwest>
    <northeast>
     <lat>44.5287488</lat>
     <lng>8.6709170</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>43.9389590</lat>
     <lng>7.9774583</lng>
    </southwest>
    <northeast>
     <lat>44.5287488</lat>
     <lng>8.6709170</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJ2yz67bv60hIR8H08R33mBQM</place_id>
 </result>
 <result>
  <type>administrative_area_level_1</type>
  <type>political</type>
  <formatted_address>Ligurien, Italien</formatted_address>
  <address_component>
   <long_name>Ligurien</long_name>
   <short_name>Ligurien</short_name>
   <type>administrative_area_level_1</type>
   <type>political</type>
  </address_component>
  <address_component>
   <long_name>Italien</long_name>
   <short_name>IT</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>44.3167917</lat>
    <lng>8.3964938</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>43.7596721</lat>
     <lng>7.4948099</lng>
    </southwest>
    <northeast>
     <lat>44.6764264</lat>
     <lng>10.0710317</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>43.7596721</lat>
     <lng>7.4948099</lng>
    </southwest>
    <northeast>
     <lat>44.6764264</lat>
     <lng>10.0710317</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJtU8s2GUS0xIRkHw8R33mBQE</place_id>
 </result>
 <result>
  <type>country</type>
  <type>political</type>
  <formatted_address>Italien</formatted_address>
  <address_component>
   <long_name>Italien</long_name>
   <short_name>IT</short_name>
   <type>country</type>
   <type>political</type>
  </address_component>
  <geometry>
   <location>
    <lat>41.8719400</lat>
    <lng>12.5673800</lng>
   </location>
   <location_type>APPROXIMATE</location_type>
   <viewport>
    <southwest>
     <lat>35.4897000</lat>
     <lng>6.6267201</lng>
    </southwest>
    <northeast>
     <lat>47.0920000</lat>
     <lng>18.7975999</lng>
    </northeast>
   </viewport>
   <bounds>
    <southwest>
     <lat>35.4897000</lat>
     <lng>6.6267201</lng>
    </southwest>
    <northeast>
     <lat>47.0920000</lat>
     <lng>18.7975999</lng>
    </northeast>
   </bounds>
  </geometry>
  <place_id>ChIJA9KNRIL-1BIRb15jJFz1LOI</place_id>
 </result>
</GeocodeResponse>

#ce

#include <File.au3>

$xml = StringRegExp(FileRead(@ScriptFullPath), "(?s)#cs(.*?)#ce", 1)[0]

Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.loadXML($xml)

$status = $oXML.SelectSingleNode("//GeocodeResponse/status")
If  $status.text = "OK" Then
    $oNodes = $oXML.SelectNodes("//result")
    For $oNode In $oNodes ;<--------------------------------------------result
        $oChilds = $oNode.childNodes
        For $oChild In $oChilds ;"<---------------------------------- result child
            If $oChild.tagName = "address_component" Then
                $oType = $oChild.SelectSingleNode("./type")
                $oLong_name = $oChild.SelectSingleNode("./long_name")
                If IsObj($oType) And IsObj($oLong_name) Then
                    ConsoleWrite($oType.text & " = " & $oLong_name.text & @CRLF)
                EndIf
            EndIf
        Next
    Next
Else
    MsgBox("","","Error")
EndIf

 

Link to comment
Share on other sites

it seems to work, I did not understand how to always get the German language ... in the results ! Look at Ligurien and Liguria, I decide the language in the https query then google write a mixed result.... The result[0] is Italian... the other German, at this point how can I grab the correct language? 

The geocode microsoft service is more accurate! THX

#include <File.au3>

$xml = @ScriptDir&"\xml.xml"
Local $oXML = ObjCreate("Microsoft.XMLDOM")
$oXML.load($xml)
dim $Value[8] = ["street_number", _
"route", _
"locality", _
"administrative_area_level_1", _
"administrative_area_level_2", _
"administrative_area_level_3", _
"country", _
"postal_code"]

$status = $oXML.SelectSingleNode("//GeocodeResponse/status")
If  $status.text = "OK" Then
local $country,$adm_1,$adm_2,$adm_3,$locality,$route,$numb,$pcode
    $oNodes = $oXML.SelectNodes("//result")
    For $oNode In $oNodes ;<--------------------------------------------result
        $oChilds = $oNode.childNodes
        For $oChild In $oChilds ;"<---------------------------------- result child
            If $oChild.tagName = "address_component" Then
                $oType = $oChild.SelectSingleNode("./type")
                $oLong_name = $oChild.SelectSingleNode("./long_name")
                If IsObj($oType) And IsObj($oLong_name) Then
                    ConsoleWrite($oType.text & " = " & $oLong_name.text & @CRLF)
                    If $oType.text = $Value[0] Then
                        $numb = $oLong_name.text
                    ElseIf $oType.text = $Value[1] Then
                        $route = $oLong_name.text
                    ElseIf $oType.text = $Value[2] Then
                        $locality = $oLong_name.text
                    ElseIf $oType.text = $Value[3] Then
                        $adm_1 = $oLong_name.text
                    ElseIf $oType.text = $Value[4] Then
                        $adm_2 = $oLong_name.text
                    ElseIf $oType.text = $Value[5] Then
                            $adm_3 = $oLong_name.text
                    ElseIf $oType.text = $Value[6] Then
                        $country = $oLong_name.text
                    ElseIf $oType.text = $Value[7] Then
                        $pcode = $oLong_name.text
                    Else
                        ConsoleWrite("Not Found"&@CRLF)
                    EndIf
                EndIf
            EndIf
        Next
    Next
MsgBox("","","Country: "&$country &@LF _
    &"Adm 1: "&$adm_1 &@LF _
    &"Adm 2: "&$adm_2 &@LF _
    &"Adm 3: "&$adm_3 &@LF _
    &"Locality: "& $locality &@LF _
    & "Street: "& $route &@LF _
    & "Numb: "&$numb&@LF _
    & "POBOX: "& $pcode)
Else
    MsgBox("","","Error")
EndIf

 

Edited by rootx
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...