Solved

Parsing an XML Document

Posted on 2009-07-15
3
523 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

690 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