Solved

Parsing an XML Document

Posted on 2009-07-15
3
513 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:
Tiggerito 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:Tiggerito
ID: 24868705
Glad you worked it out
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
A short film showing how OnPage and Connectwise integration works.

914 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now