Solved

Parsing an XML Document

Posted on 2009-07-15
3
509 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

13 Experts available now in Live!

Get 1:1 Help Now