Показать сообщение отдельно
  #4  
Старый 23.03.2018, 02:37
Аватар для LIONSMILE
LIONSMILE LIONSMILE вне форума
Новичок
 
Регистрация: 19.03.2018
Сообщения: 51
Версия Delphi: Delphi 7
Репутация: 10
По умолчанию

А вот еще вопрос касаемо XML, не могу разобраться с циклами снова. Теперь здесь одинаковые значения в тегах, которые необходимо получить.
Образец файла:
Код:
<?xml version="1.0" encoding="utf-8"?>
<weather created="eurometeo.ru" date="2018-03-21 22:00:00">
<city id="russia/moskva">
		<country>russia/moskva</country>
		<cityname>Москва</cityname>
		<cityname2>в Москвe</cityname2>
		<citytime>2018-03-21 23:00:00</citytime>
		<step>
			<datetime>2018-03-21 04:00:00</datetime>
			<pressure>742.43</pressure> 
			<temperature>-5.41</temperature>
			<humidity>25</humidity>
			<cloudcover>0</cloudcover> 
			<windspeed>0.67</windspeed>
			<windgust>0.86</windgust>
			<winddir>36</winddir>
			<precipitation>0.00</precipitation>
		</step>
		<step>
			<datetime>2018-03-21 10:00:00</datetime>
			<pressure>741.94</pressure> 
			<temperature>-2.95</temperature>
			<humidity>32</humidity>
			<cloudcover>0</cloudcover> 
			<windspeed>1.76</windspeed>
			<windgust>3.02</windgust>
			<winddir>299</winddir>
			<precipitation>0.00</precipitation>
		</step>

</city>
</weather>

и код того что получилось:
Код:
procedure TForm1.FormCreate(Sender: TObject);
begin
StringGrid1.Cells[0,0]:= 'Наименование города';
StringGrid1.Cells[1,0]:= 'Время';
StringGrid1.Cells[2,0]:= 'Прогноз';
StringGrid1.Cells[3,0]:= 'Атмосферное давление';
StringGrid1.Cells[4,0]:= 'Температура воздуха ';
StringGrid1.Cells[5,0]:= 'Относительная влажность';
StringGrid1.Cells[6,0]:= 'Облачность';
StringGrid1.Cells[7,0]:= 'Скорость ветра';
StringGrid1.Cells[8,0]:= 'Скорость порывов ветра';
StringGrid1.Cells[9,0]:= 'Направление ветра';
StringGrid1.Cells[10,0]:= 'Осадки';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
 i: integer;
begin
XMLDocument1.LoadFromFile('test.xml');
XMLDocument1.Active := true;
with XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['datetime']  do
  for i:= 0 to ChildNodes.Count-1 do
   begin
StringGrid1.RowCount:= StringGrid1.RowCount+1;
StringGrid1.Cells[0,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['cityname'].Text);
StringGrid1.Cells[1,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['citytime'].Text);
StringGrid1.Cells[2,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['datetime'].Text);
StringGrid1.Cells[3,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['pressure'].Text);
StringGrid1.Cells[4,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['temperature'].Text);
StringGrid1.Cells[5,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['humidity'].Text);
StringGrid1.Cells[6,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['cloudcover'].Text);
StringGrid1.Cells[7,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['windspeed'].Text);
StringGrid1.Cells[8,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['windgust'].Text);
StringGrid1.Cells[9,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['winddir'].Text);
StringGrid1.Cells[10,StringGrid1.RowCount-1]:=VarToStr (XMLDocument1.DocumentElement.ChildNodes['city'].ChildNodes['step'].ChildNodes['precipitation'].Text);
XMLDocument1.Active := false;
end;
end;

Смог прочитать данные только из первого блока, попытался сделать цикл по образцу, не читает. Что я делаю не так подскажите пожалуйста?
Ответить с цитированием