XML Tutorial/de

From Free Pascal wiki
Revision as of 22:11, 27 June 2007 by Swen (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)

Einleitung

Die Extensible Markup Language ist eine von W3C empfohlene Sprache, die geschaffen wurde für den Informationsaustausch zwischen verschiedenen Systemen. Es ist eine textbasierte Art und Weise, um Informationen zu speichern. Moderne Datenaustauschsprachen wie XHTML, ebenso wie die meisten WebServices Technologien, basieren auf XML.

Gegenwärtig gibt es eine Reihe von Units, die Unterstützung für XML unter Free Pascal bieten. Diese Units sind "XMLRead", "XMLWrite" und "DOM" und sie sind ein Teil der Free Component Library (FCL) des Free Pascal Compilers. Die FCL ist bereits im Vorgabe Suchpfad für den Compiler in Lazarus, daher müssen sie nur die Units zu ihrem uses Abschnitt hinzufügen, um die XML Unterstützung zu erhalten. Die FCL ist gegenwärtig (Oktober 2005) noch nicht dokumentiert, daher hat dieses kurze Tutorial die Zielsetzung der Einführung des XML Einstiegs unter Verwendung dieser Units.

Das XML DOM (Document Object Model) ist eine Reihe standardisierter Objekte, die eine ähnliche Schnittstelle für die Benutzung von XML für verschiedene Sprachen und Systeme bieten. Der Standard spezifiziert nur die Methoden, Eigenschaften und andere Schnittstellen Teile des Objekts, die Implementierung frei lassend für verschiedene Sprachen. Die FCL unterstützt gegenwärtig vollständig das XML DOM 1.0.

Einfaches Beispiel

Sagen wir sie wollen auf die XML Datei 'C:\Programme\teste.xml' zugreifen. Hier ist der Dateiinhalt:

<xml>

<?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>

</xml>

Der folgende Code kann die Kontenpunkt Namen in ein TMemo schreiben, das sich auf dem Formular befindet:

<delphi>

var
 Documento: TXMLDocument;
 i, j: Integer;
begin
 Documento := TXMLDocument.Create;
 ReadXMLFile(Documento, 'C:\Programme\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;

</delphi>

Füllen eines TreeView mit XML

Eine übliche Verwendung von XML Dateien ist sie zu analysieren und ihren Inhalt anzuzeigen in einem tree like Format. Sie können die TTreeView Komponente auf dem "Common Controls" Tab von Lazarus finden.

Die Funktion unten nimmt ein zuvor geladenes oder im Code erzeugtes XML Dokument und füllt einen TreeView mit seinen Inhalten. Die Beschriftung von jedem Knoten ist der Inhalt des ersten Attributs eines jeden Knotens.

<delphi> procedure TForm1.XML2Tree(tree: TTreeView; XMLDoc: TXMLDocument); var

 iNode: TDOMNode;
 procedure ProcessNode(Node: TDOMNode; TreeNode: TTreeNode);
 var
   cNode: TDOMNode;
 begin
   if Node = nil then Exit; // Stops if reached a leaf
   
   // Adds a node to the tree
   TreeNode := tree.Items.AddChild(TreeNode, Node.Attributes[0].NodeValue);
   // Goes to the child node
   cNode := Node.ChildNodes.Item[0];
   // Processes all child nodes
   while cNode <> nil do
   begin
     ProcessNoDe(cNode, TreeNode);
     cNode := cNode.NextSibling;
   end;
 end;
   

begin

 iNode := XMLDoc.DocumentElement.ChildNodes.Item[0];
 while iNode <> nil do
 begin
   ProcessNode(iNode, nil); // Recursive
   iNode := iNode.NextSibling;
 end;

end; </delphi>

Modifizieren eines XML Dokuments

The first thing to remember is that TDOMDocument is the "handle" to the DOM. You can get an instance of this class by creating one or by loading a XML document.

Nodes on the other hand cannot be created like a normal object. You *must* use the methods provided by TDOMDocument to create them, and latter use other methods to put them on the correct place on the tree. This is because nodes must be "owned" by a specific document on DOM.

Unterhalb sind einige gebräuchliche Methoden von TDOMDocument:

<delphi>

  function CreateElement(const tagName: DOMString): TDOMElement; virtual;
  function CreateTextNode(const data: DOMString): TDOMText;
  function CreateCDATASection(const data: DOMString): TDOMCDATASection;
    virtual;
  function CreateAttribute(const name: DOMString): TDOMAttr; virtual;

</delphi>

And here an example method that will located the selected item on a TTreeView and then insert a child node to the XML document it represents. The TreeView must be previously filled mit den Inhalten einer XML Datei unter Verwendung der XML2Tree Funktion.

<delphi> procedure TForm1.actAddChildNode(Sender: TObject); var

 Posicao: Integer;
 NovoNo: TDomNode;

begin

 {*******************************************************************
 *  Detects the selected element
 *******************************************************************}
 if TreeView1.Selected = nil then Exit;
 if TreeView1.Selected.Level = 0 then
 begin
   Posicao := TreeView1.Selected.Index;
   NovoNo := XMLDoc.CreateElement('item');
   TDOMElement(NovoNo).SetAttribute('nome', 'Item');
   TDOMElement(NovoNo).SetAttribute('arquivo', 'Arquivo');
   XMLDoc.DocumentElement.ChildNodes.Item[Posicao].AppendChild(NovoNo);
   {*******************************************************************
   *  Updates the TreeView
   *******************************************************************}
   TreeView1.Items.Clear;
   XML2Tree(TreeView1, XMLDoc);
 end
 else if TreeView1.Selected.Level >= 1 then
 begin
   {*******************************************************************
   *  This function only works on the first level of the tree,
   *  but can easely modifyed to work for any number of levels
   *******************************************************************}
 end;

end; </delphi>

Erzeugen eines TXMLDocument aus einem String

Given al XML file in MyXmlString, the following code will create it's DOM:

<delphi> Var

 S : TStringStream;
 XML : TXMLDocument;

begin

 S:= TStringStream.Create(MyXMLString);
 Try
   S.Position:=0;
   XML:=Nil;
   ReadXMLFile(XML,S); // Komplettes XML Dokument
   // Alternativ:
   ReadXMLFragment(AParentNode,S); // Read only XML fragment.
 Finally
   S.Free;
 end;

end; </delphi>

Externe Links