Delphi-PRAXiS
Seite 1 von 2  1 2      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   XML (https://www.delphipraxis.net/46-xml/)
-   -   Delphi Attribut auslesen.. (https://www.delphipraxis.net/91225-attribut-auslesen.html)

mtin 1. Mai 2007 22:10


Attribut auslesen..
 
Hallo, ich versuche hier verzweifelt ein Attribut auszulesen und es will und will nich klappen -.-

xml:
XML-Code:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<yweather:condition text="Fair" code="33" temp="7" date="Tue, 01 May 2007 10:20 pm CEST" />
</channel>
</rss>
(gekürtzt)


code:
Delphi-Quellcode:
xmldoc.LoadFromXML(weather);
xmldoc.Active:=true;
value:=xmldoc.DocumentElement.ChildNodes['channel'].ChildNodes['yweather:condition'].GetAttributeNS('temp','');
aber es kommt nur immer sowas wie kann nicht "null" in "string" konvertieren :(

was ist denn da nur los?

Luckie 1. Mai 2007 22:54

Re: Attribut auslesen..
 
Attribute liest man wie Knoten aus, nur mit einem @ davor.

mtin 1. Mai 2007 23:03

Re: Attribut auslesen..
 
wie lese ich einen Knoten aus?
ChildNodes['bla'].Text?

und wohin soll das @?

Luckie 1. Mai 2007 23:12

Re: Attribut auslesen..
 
Vor das bla: '@bla'.

mtin 1. Mai 2007 23:20

Re: Attribut auslesen..
 
hmmm...also ich blick da echt überhaupt nicht durch!

jetzt kommt die Meldung

Project raised exception class EOleException with message 'This name may not contain the ':' character:
yweather-->:<--@temp'.

bei der Zeile
value:=Form1.xmldoc.DocumentElement.ChildNodes['channel'].ChildNodes['yweather:condition'].ChildNodes['@temp'].Text;

und der rss Datei
http://xml.weather.yahoo.com/forecastrss?p=GMXX1930&u=c
( <yweather:condition text="Fair" code="33" temp="7" date="Tue, 01 May 2007 10:20 pm CEST" /> )

Luckie 1. Mai 2007 23:25

Re: Attribut auslesen..
 
Die Eigenschaft ChildNodes sieht mir so aus, als wenn es eine Liste wäre. Also müsste da wohl irgendwie ein Zähler hin:
Delphi-Quellcode:
value:=Form1.xmldoc.DocumentElement.ChildNodes[i].Node['@temp'].Text;
Oder so ähnlich. Aber dazu gibt es hier im Forum auch schon einen Thread. Benutz einfach mal die Suche.

mtin 1. Mai 2007 23:35

Re: Attribut auslesen..
 
na mensch, nen bisschen rumprobiert hab ich auch schon -.-

ChildNodes funktioniert, sofern ich nur den Wert dieses Knotens will und nicht den WErt von nem Attribut :(
value:=Form1.xmldoc.DocumentElement.ChildNodes['channel'].ChildNodes['description'].Text
funktioniert z.b. wunderbar

und mit der Suche hab ich mich mittlerweile auch schon bestimmt ne stunde beschäftigt :wall:

marabu 2. Mai 2007 07:18

Re: Attribut auslesen..
 
Hallo,

so geht es:

Delphi-Quellcode:
var
  xnChannel, xnCondition: IXMLNode;
begin
  xnChannel := xmldoc.DocumentElement.ChildNodes['channel'];
  xnCondition := xnChannel.ChildNodes['condition'];
  ShowMessage(VarToStr(xnCondition.Attributes['temp']));
end;
Grüße vom marabu

mtin 2. Mai 2007 12:33

Re: Attribut auslesen..
 
hm...danke das ihr euch alle solche Mühe macht, aber es geht trotzdem nicht :wall:

Result bleibt leer...

hier mal der Komplette Quelltext, einfach ein TXMLDocument aufs Form geknallt und dann müsste das doch gehen :wall:

Delphi-Quellcode:
function getWeather():string;
var weatherCheck:TidHTTP;
weather:string;
xnChannel, xnCondition: IXMLNode;
begin

try
  weatherCheck := TidHTTP.Create;
  //http://xml.weather.yahoo.com/forecastrss?p=GMXX1930&u=c
  weather := weatherCheck.Get('http://xml.weather.yahoo.com/forecastrss?p=GMXX1930&u=c');

  Form1.xmldoc.LoadFromXML(weather);
  Form1.xmldoc.Active:=true;
  xnChannel := Form1.xmldoc.DocumentElement.ChildNodes['channel'];
  xnCondition := xnChannel.ChildNodes['condition'];
  result := VarToStr(xnCondition.Attributes['temp']);

  weatherCheck.Free;
except
  result := 'could not retrieve weather data';
end;

end;

marabu 3. Mai 2007 06:04

Re: Attribut auslesen..
 
Hallo,

mein Code-Beispiel war auf dein XML-Fragment aus Beitrag #1 zugeschnitten. Wenn du die Daten aus dem Feed verwendest, dann musst du darauf achten, dass <condition> kein direkter childNode von <channel> ist (rss/channel/item/condition):

Delphi-Quellcode:
var
  xnChannel, xnItem, xnCondition: IXMLNode;
begin
  xnChannel := xmldoc.DocumentElement.ChildNodes['channel'];
  xnItem := xnChannel.ChildNodes['item'];
  xnCondition := xnChannel.ChildNodes['condition'];
  ShowMessage(VarToStr(xnCondition.Attributes['temp']));
end;
Freundliche Grüße


Alle Zeitangaben in WEZ +1. Es ist jetzt 07:06 Uhr.
Seite 1 von 2  1 2      

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz