Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Parsing an XML Document

Posted on 2009-07-15
3
Medium Priority
?
527 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

618 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