XML Tutorial/pt

From Lazarus wiki
Revision as of 23:26, 19 June 2007 by Sekelsenmat (talk | contribs)
Jump to navigationJump to search

Deutsch (de) English (en) español (es) français (fr) magyar (hu) Bahasa Indonesia (id) italiano (it) 日本語 (ja) 한국어 (ko) português (pt) русский (ru) 中文(中国大陆)‎ (zh_CN)

Introdução

A "Extensible Markup Language" é uma linguagem recomendada pela W3C para a troca de informações entre diferentes sistemas. É um formato baseado em texto de guardar informações. Linguagens modernas de troca de dados, como o XHTML, além da maioria das tecnologia de WebServices, são baseados no XML.

Currently there is a set of units that provides support for XML on Lazarus. These units are called "XMLRead", "XMLWrite" and "DOM" and they are part of the Free Component Library (FCL) from the Free Pascal Compiler. The FCL is already on the default search path for the compiler on Lazarus, so you only need to add the units to your uses clause in order to get XML support. The FCL is not documented currently (October / 2005), so this short tutorial aims at introducing XML access using those units.

The XML DOM (Document Object Model) is a set of standarized objects that provide a similar interface for the use of XML on different languages and systems. The standard only specifies the methods, properties and other interface parts of the object, leaving the implementation free for different languages. The FCL currently supports fully the DOM 1.0.

Basic Example

Let´s say you want to access a XML file called 'C:\Programas\teste.xml'. Here is the file content:

<?xml version="1.0"?>
<images directory="mydir">
 <imageNode URL="graphic.jpg" title="">
   <Peca DestinoX="0" DestinoY="0">Pecacastelo.jpg1.swf</Peca>
   <Peca DestinoX="0" DestinoY="86">Pecacastelo.jpg2.swf</Peca>
 </imageNode>
</images>

The following code can write the Node´s names to a TMemo placed on a form:

var
 Documento: TXMLDocument;
 i, j: Integer;
begin
 Documento := TXMLDocument.Create;
 ReadXMLFile(Documento, 'C:\Programas\teste.xml');
 Memo.Lines.Clear;
 with Documento.DocumentElement.ChildNodes do
 begin
   for i := 0 to (Count - 1) do
   begin
     Memo.Lines.Add(Item[i].NodeName + ' ' + Item[i].NodeValue);
     for j := 0 to (Item[i].ChildNodes.Count - 1) do
     begin
       Memo.Lines.Add(Item[i].ChildNodes.Item[j].NodeName + ' '
        + Item[i].ChildNodes.Item[j].NodeValue);
     end;
   end;
 end;
 Documento.Free;
end;