Solved

Parsing an XML Document

Posted on 2009-07-15
3
517 Views
Last Modified: 2012-08-14
Hi,
Not sure of what I am doing wrong ... I need  to parse this XML document ( Lat and Long from Google)but "g_nodelist" always come to 0 :
the XML response look like this
'-------------
- <kml xmlns="http://earth.google.com/kml/2.0">
- <Response>
  <name>Batu 3 1/2 Jalan Ipoh,51200 Kuala Lumpur,Malaysia</name>
+ <Status>
  <code>200</code>
  <request>geocode</request>
  </Status>
- <Placemark id="p1">
  <address>Batu 3 1/2 Jalan Ipoh, Kuala Lumpur, Kuala Lumpur, Malaysia</address>
- <AddressDetails Accuracy="4" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
- <Country>
  <CountryNameCode>MY</CountryNameCode>
  <CountryName>Malaysia</CountryName>
- <AdministrativeArea>
  <AdministrativeAreaName>Kuala Lumpur</AdministrativeAreaName>
- <Locality>
  <LocalityName>Kuala Lumpur</LocalityName>
- <DependentLocality>
  <DependentLocalityName>Batu 3 1/2 Jalan Ipoh</DependentLocalityName>
  </DependentLocality>
  </Locality>
  </AdministrativeArea>
  </Country>
  </AddressDetails>
+ <ExtendedData>
  <LatLonBox north="3.1992447" south="3.1769632" east="101.6969359" west="101.6649211" />
  </ExtendedData>
+ <Point>
  <coordinates>101.6809285,3.1881040,0</coordinates>
  </Point>
  </Placemark>
+ <Placemark id="p2">
  <address>1, Kampung Sungai Gedong, 34400 Bagan Serai, Perak, Malaysia</address>
- <AddressDetails Accuracy="6" xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
- <Country>
  <CountryNameCode>MY</CountryNameCode>
  <CountryName>Malaysia</CountryName>
- <Thoroughfare>
  <ThoroughfareName>1</ThoroughfareName>
  </Thoroughfare>
  </Country>
  </AddressDetails>
- <ExtendedData>
  <LatLonBox north="6.3017400" south="3.2261770" east="101.6721799" west="100.3627141" />
  </ExtendedData>
- <Point>
  <coordinates>101.0378678,4.8233100,0</coordinates>
  </Point>
  </Placemark>
  </Response>
  </kml>
'-------------------------------------------
My code :

       Dim g_xml As XmlDocument = New XmlDocument
        Dim g_nodelist As XmlNodeList
        Dim g_node As XmlNode
       
        g_xml = New XmlDocument()
        'Load the Xml file
        g_xml.Load("http://maps.google.com/maps/geo?q=Batu 3 1/2 Jalan Ipoh,51200 Kuala Lumpur,Malaysia&output=xml&key=XXXXXXX-ob")
        'Get the list of name nodes
        g_nodelist = g_xml.SelectNodes("/kml//Response/Placemark")
        'Loop through the nodes
        For Each g_node In g_nodelist
             Dim genderAttribute = g_node.Attributes.GetNamedItem("id").Value
               Dim Coordinate = g_node.ChildNodes.Item(0).InnerText
           Next
===================

I must be missing somthing ... I try many different pathfor the name nodes  to no avail. I am open to a different method , I just need to parse the Latitude and longitude  <Point>
  <coordinates>101.6809285,3.1881040,0</coordinates>
  </Point>
, so I it can be stored in a DB .


Thank's

Alain
0
Comment
Question by:hiramlight
  • 2
3 Comments
 
LVL 23

Accepted Solution

by:
Tony McCreath earned 500 total points
ID: 24868235
You need to handle the namespace in the XML. Here's an article on it:

http://support.microsoft.com/kb/318545

I've attached a code snippet of the article in in VB.

In your case the namespace is "http://earth.google.com/kml/2.0", so you would probable do something like...

xmlnsManager.AddNamespace("ge", "http://earth.google.com/kml/2.0")

then your xpath would be like this

g_nodelist = g_xml.SelectNodes("/ge:kml//ge:Response/ge:Placemark")
'Instantiate an XmlDocument object.
Dim xmldoc As New System.Xml.XmlDocument()
 
'Load books.xml into the XmlDocument object. 
xmldoc.Load("C:\books.xml")
 
'Instantiate an XmlNamespaceManager object. 
Dim xmlnsManager As New System.Xml.XmlNamespaceManager(xmldoc.NameTable)
 
'Add the namespaces used in books.xml to the XmlNamespaceManager.
xmlnsManager.AddNamespace("bk", "urn:Books")
xmlnsManager.AddNamespace("pub", "urn:Publisher")
 
 
Dim MSPressBookList As System.Xml.XmlNodeList
 
'Execute the XPath query using the SelectNodes method of the XmlDocument.
'Supply the XmlNamespaceManager as the nsmgr parameter.
'The matching nodes will be returned as an XmlNodeList.
'Use an XmlNode object to iterate through the returned XmlNodeList.
 
MSPressBookList = xmldoc.SelectNodes("//pub:Publisher[. = 'MSPress']/parent::node()/bk:Title", xmlnsManager)
 
For Each MSPressBook As System.Xml.XmlNode In MSPressBookList
        
    System.Diagnostics.Debug.WriteLine(MSPressBook.InnerText)
Next

Open in new window

0
 

Author Comment

by:hiramlight
ID: 24868498
Thank's Tiggerito,

it work beautifull , didn't do a lot of XML parsing , so still much to learn .. had to make a small correction with the Nodelist .. correct stament is :

 g_nodelist = g_xml.SelectNodes("/ge:kml//ge:Response/ge:Placemark", xmlnsManager)

Cheers,

alain
0
 
LVL 23

Expert Comment

by:Tony McCreath
ID: 24868705
Glad you worked it out
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question