Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1328
  • Last Modified:

TXMLDocument : how to locate an element name with a prefix

I'm using the Google Calendar API, and I need to parse the XML which is returned by Google.  But I don't manage to query an element node when a prefix is used (gd:when in this case.

This is my code

 Entry := XMLDocument.DocumentElement.Childnodes['entry'];
 CalendarEntry := Entry.ChildNodes['gd:when'];
StartTime := Copy(CalendarEntry.Attributes['startTime'],12,5);
 endTime := Copy(CalendarEntry.Attributes['endTime'],12,5);

I always get a runtime error that the returned value is null.  Strangely enough, if I don't use the element name (gd:when) but the index then it works fine.  But the index is not always the same.

Note that element nodes without prefix (e.g. 'title') work fine.

Anyone can tell how to find the values of elements having a prefix in their name ?

I include the XML file.



<?xml version='1.0' encoding='UTF-8'?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:gCal='http://schemas.google.com/gCal/2005' xmlns:gd='http://schemas.google.com/g/2005'><id>http://www.google.com/calendar/feeds/default/allcalendars/full</id><updated>2009-12-16T19:16:24.119Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/gCal/2005#calendarmeta'/><title type='text'>walter.lecossois@gmail.com's Calendar List</title><link rel='alternate' type='text/html' href='http://www.google.com/calendar/render'/><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full'/><link rel='http://schemas.google.com/g/2005#post' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full'/><link rel='self' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full'/><author><name>walter.lecossois@gmail.com</name><email>walter.lecossois@gmail.com</email></author><generator version='1.0' uri='http://www.google.com/calendar'>Google Calendar</generator><openSearch:startIndex>1</openSearch:startIndex><entry><id>http://www.google.com/calendar/feeds/default/allcalendars/full/walter.lecossois%40gmail.com</id><published>2009-12-16T19:16:24.008Z</published><updated>2009-12-16T17:50:53.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/gCal/2005#calendarmeta'/><title type='text'>Walter Lecossois</title><content type='application/atom+xml' src='http://www.google.com/calendar/feeds/walter.lecossois%40gmail.com/private/full'/><link rel='alternate' type='application/atom+xml' href='http://www.google.com/calendar/feeds/walter.lecossois%40gmail.com/private/full'/><link rel='http://schemas.google.com/gCal/2005#eventFeed' type='application/atom+xml' href='http://www.google.com/calendar/feeds/walter.lecossois%40gmail.com/private/full'/><link rel='http://schemas.google.com/acl/2007#accessControlList' type='application/atom+xml' href='http://www.google.com/calendar/feeds/walter.lecossois%40gmail.com/acl/full'/><link rel='self' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full/walter.lecossois%40gmail.com'/><link rel='edit' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full/walter.lecossois%40gmail.com'/><author><name>walter.lecossois@gmail.com</name><email>walter.lecossois@gmail.com</email></author><gCal:accesslevel value='owner'/><gCal:color value='#A32929'/><gCal:hidden value='false'/><gCal:selected value='true'/><gCal:timezone value='Europe/Brussels'/><gCal:timesCleaned value='0'/></entry><entry><id>http://www.google.com/calendar/feeds/default/allcalendars/full/2p7anv7asdmk9prjmpdf5upuog%40group.calendar.google.com</id><published>2009-12-16T19:16:24.007Z</published><updated>2009-11-23T08:47:20.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/gCal/2005#calendarmeta'/><title type='text'>Familie Lecossois</title><summary type='text'/><content type='application/atom+xml' src='http://www.google.com/calendar/feeds/2p7anv7asdmk9prjmpdf5upuog%40group.calendar.google.com/private/full'/><link rel='alternate' type='application/atom+xml' href='http://www.google.com/calendar/feeds/2p7anv7asdmk9prjmpdf5upuog%40group.calendar.google.com/private/full'/><link rel='http://schemas.google.com/gCal/2005#eventFeed' type='application/atom+xml' href='http://www.google.com/calendar/feeds/2p7anv7asdmk9prjmpdf5upuog%40group.calendar.google.com/private/full'/><link rel='http://schemas.google.com/acl/2007#accessControlList' type='application/atom+xml' href='http://www.google.com/calendar/feeds/2p7anv7asdmk9prjmpdf5upuog%40group.calendar.google.com/acl/full'/><link rel='self' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full/2p7anv7asdmk9prjmpdf5upuog%40group.calendar.google.com'/><link rel='edit' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full/2p7anv7asdmk9prjmpdf5upuog%40group.calendar.google.com'/><author><name>Familie Lecossois</name></author><gCal:accesslevel value='owner'/><gCal:color value='#2952A3'/><gCal:hidden value='false'/><gd:where valueString=''/><gCal:selected value='true'/><gCal:timezone value='Europe/Brussels'/><gCal:timesCleaned value='0'/></entry><entry><id>http://www.google.com/calendar/feeds/default/allcalendars/full/fabienne.wilmet%40gmail.com</id><published>2009-12-16T19:16:24.008Z</published><updated>2009-12-16T17:50:54.000Z</updated><category scheme='http://schemas.google.com/g/2005#kind' term='http://schemas.google.com/gCal/2005#calendarmeta'/><title type='text'>Fabienne Wilmet</title><content type='application/atom+xml' src='http://www.google.com/calendar/feeds/fabienne.wilmet%40gmail.com/private/full'/><link rel='alternate' type='application/atom+xml' href='http://www.google.com/calendar/feeds/fabienne.wilmet%40gmail.com/private/full'/><link rel='http://schemas.google.com/gCal/2005#eventFeed' type='application/atom+xml' href='http://www.google.com/calendar/feeds/fabienne.wilmet%40gmail.com/private/full'/><link rel='self' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full/fabienne.wilmet%40gmail.com'/><link rel='edit' type='application/atom+xml' href='http://www.google.com/calendar/feeds/default/allcalendars/full/fabienne.wilmet%40gmail.com'/><author><name>fabienne.wilmet@gmail.com</name><email>fabienne.wilmet@gmail.com</email></author><gCal:accesslevel value='read'/><gCal:color value='#5229A3'/><gCal:hidden value='false'/><gCal:selected value='true'/><gCal:timezone value='Europe/Brussels'/><gCal:timesCleaned value='0'/></entry></feed>

Open in new window

0
lecossois
Asked:
lecossois
  • 2
1 Solution
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Once you got Entry then you should first find the node 'when' and then use FindNameSpaceUri
Not on delphi now, but something like this should work

NODE := XMLDOCUMENT.DocumentElement.ChildNodes.FindNode('when');
value := NODE.FindNamespaceURI('gd');
0
 
lecossoisAuthor Commented:
That returns an URI ('http://schemas.google.com/g/2005').  I need the value of the attribute StartTime of the element gd:when.



0
 
Ferruccio AccalaiSenior developer, analyst and customer assistance Commented:
Ok, I've mistyped the suggestion (was not on delphi, as I said :D)
I've tested it now and here's your solution.

First of all remember that if the Option DONodeAutoCreate is True than XMLDocument autocreate the node even if it isn't found.

BTW to get the correct attributes from node with prefix, yes, you must use the FindNamespaceURI, but like in the attached code, that works like a charm.

procedure TForm1.GetGDWhenAttributes;
var
  EntryNode, CalendarEntry: IXMLNode;
  StartTime, EndTime: String;
begin
  EntryNode := XMLDocument.DocumentElement.ChildNodes['entry'];
  if assigned(EntryNode) then
    CalendarEntry := EntryNode.ChildNodes.FindNode('when', EntryNode.FindNamespaceURI('gd'));
  if assigned(CalendarEntry) then
    begin
      StartTime := Copy(CalendarEntry.Attributes['startTime'], 12, 5);
      EndTime := Copy(CalendarEntry.Attributes['endTime'], 12, 5);
    end;
end;

Open in new window

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!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now