Форумчанин
Регистрация: 07.11.2009
Сообщений: 218
|
Работа с компонентом SAX парсинг XML
Здравствуйте!
Не как не могу понять как парсить с XML файла с помощью компонента SAX. Другие компоненты не предлагать.
Вот писать в файл получилось таким образом :
Код:
procedure TForm1.Button1Click(Sender: TObject);
var
saxWrite: TSAXDocumentWriter;
Attributes: TAttributesImpl;
outStream: TMemoryStream;
FS: TFileStream;
i: Integer;
begin
saxWrite:=TSAXDocumentWriter.Create(Application);
outStream:=TMemoryStream.Create;
try
saxWrite.Stream:=outStream;
saxWrite.IncludeProlog:=True;
saxWrite.Indent:=True;
saxWrite.Version:='1.0';
saxWrite.Encoding:='Windows-1251';
saxWrite.Standalone:=True;
Attributes:=TAttributesImpl.Create;
try
saxWrite.StartDocument;
Attributes.addAttribute('', '', 'Developer', '', '***********');
Attributes.addAttribute('', '', 'Data', '', '29.12.10');
Attributes.addAttribute('', '', 'Application', '', 'Test version');
saxWrite.StartElement('','','Table', Attributes);
Attributes.clear;
saxWrite.StartElement('','','People', Attributes);
Attributes.addAttribute('', '', 'Имя', '', '*******'); //Записываем атребуты.
Attributes.addAttribute('', '', 'Фамилия', '', '********'); //Записываем атребуты.
Attributes.addAttribute('', '', 'Ник', '', '********'); //Записываем атребуты.
saxWrite.StartElement('','','User', Attributes);
saxWrite.EndElement('', '', 'User');
Attributes.clear;
Attributes.addAttribute('', '', 'Имя', '', '********); //Записываем атребуты.
Attributes.addAttribute('', '', 'Фамилия', '', '*******'); //Записываем атребуты.
Attributes.addAttribute('', '', 'Ник', '', '********'); //Записываем атребуты.
saxWrite.StartElement('','','User', Attributes);
saxWrite.EndElement('', '', 'User');
Attributes.clear;
Attributes.addAttribute('', '', 'Имя', '', '********'); //Записываем атребуты.
Attributes.addAttribute('', '', 'Фамилия', '', '*********'); //Записываем атребуты.
Attributes.addAttribute('', '', 'Ник', '', '*******'); //Записываем атребуты.
saxWrite.StartElement('','','User', Attributes);
saxWrite.EndElement('', '', 'User');
Attributes.clear;
saxWrite.EndElement('', '', 'People');
saxWrite.StartElement('', '', 'Cont', Attributes);
Randomize;
for i:=0 to 1000 do
begin
Attributes.addAttribute('', '', 'Дом.Тел.', '', IntToStr(MyRandom(10, 99)) + '-' + IntToStr(MyRandom(10, 99)) + '-' + IntToStr(MyRandom(10, 99)));
Attributes.addAttribute('', '', 'Моб.Тел.', '', '050' + IntToStr(MyRandom(100000, 999999)));
saxWrite.StartElement('','','Cont', Attributes);
saxWrite.EndElement('', '', 'Cont');
Attributes.clear;
end;
saxWrite.EndElement('', '', 'Cont');
saxWrite.EndElement('', '', 'Table');
saxWrite.EndDocument;
finally
Attributes.Free;
end;
finally
saxWrite.Free;
outStream.Position:=0;
outStream.SaveToFile(ExtractFilePath(Application.ExeName)+'1.xml');
outStream.Free;
end;
end;
Вот какой файл мы получаем :
Код:
<?xml version="1.0" encoding="Windows-1251" standalone="yes"?>
<Table Developer="********" Data="29.12.10" Application="Test version">
<People>
<User Имя="*****" Фамилия="*****" Ник="******"/>
<User Имя="******" Фамилия="*******" Ник="******"/>
<User Имя="******" Фамилия="******" Ник="******"/>
</People>
<Cont>
<Cont Дом.Тел.="82-15-83" Моб.Тел.="050910210"/>
<Cont Дом.Тел.="87-22-66" Моб.Тел.="050899892"/>
<Cont Дом.Тел.="69-82-51" Моб.Тел.="050612089"/>
<Cont Дом.Тел.="88-66-15" Моб.Тел.="050729543"/>
<Cont Дом.Тел.="53-29-62" Моб.Тел.="050686371"/>
<Cont Дом.Тел.="96-82-27" Моб.Тел.="050322147"/>
</Cont>
</Table>
Вопрос в чем, мне нужно его читать и все выводить в ListBox.
Помогите пожалуйста!
Заранее очень благодарен.
Я бы изменил мир, но Бог не дает исходников...
Если помог, нажми на весы и поставь плюс - в знак благодарности
|