Difference between revisions of "XML Tutorial/de"

From Free Pascal wiki
Jump to navigationJump to search
m
m
Line 13: Line 13:
 
Sagen wir sie wollen auf die XML Datei 'C:\Programme\teste.xml' zugreifen. Hier ist der Dateiinhalt:
 
Sagen wir sie wollen auf die XML Datei 'C:\Programme\teste.xml' zugreifen. Hier ist der Dateiinhalt:
  
<code>
+
<xml>
 
  <?xml version="1.0"?>
 
  <?xml version="1.0"?>
 
  <images directory="mydir">
 
  <images directory="mydir">
Line 21: Line 21:
 
   </imageNode>
 
   </imageNode>
 
  </images>
 
  </images>
</code>
+
</xml>
  
 
Der folgende Code kann die Kontenpunkt Namen in ein TMemo schreiben, das sich auf dem Formular befindet:
 
Der folgende Code kann die Kontenpunkt Namen in ein TMemo schreiben, das sich auf dem Formular befindet:
  
<code>
+
<delphi>
 
  var
 
  var
 
   Documento: TXMLDocument;
 
   Documento: TXMLDocument;
Line 47: Line 47:
 
   Documento.Free;
 
   Documento.Free;
 
  end;
 
  end;
</code>
+
</delphi>
  
 
== Füllen eines TreeView mit XML ==
 
== Füllen eines TreeView mit XML ==
Line 55: Line 55:
 
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.
 
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.
  
<pre>
+
<delphi>
 
procedure TForm1.XML2Tree(tree: TTreeView; XMLDoc: TXMLDocument);
 
procedure TForm1.XML2Tree(tree: TTreeView; XMLDoc: TXMLDocument);
 
var
 
var
Line 88: Line 88:
 
   end;
 
   end;
 
end;
 
end;
</pre>
+
</delphi>
  
 
== Modifizieren eines XML Dokuments ==
 
== Modifizieren eines XML Dokuments ==
Line 98: Line 98:
 
Unterhalb sind einige gebräuchliche Methoden von TDOMDocument:
 
Unterhalb sind einige gebräuchliche Methoden von TDOMDocument:
  
<pre>
+
<delphi>
 
   function CreateElement(const tagName: DOMString): TDOMElement; virtual;
 
   function CreateElement(const tagName: DOMString): TDOMElement; virtual;
 
   function CreateTextNode(const data: DOMString): TDOMText;
 
   function CreateTextNode(const data: DOMString): TDOMText;
Line 104: Line 104:
 
     virtual;
 
     virtual;
 
   function CreateAttribute(const name: DOMString): TDOMAttr; virtual;
 
   function CreateAttribute(const name: DOMString): TDOMAttr; virtual;
</pre>
+
</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 [[Networking#Populating a TreeView with XML|XML2Tree Funktion]].
 
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 [[Networking#Populating a TreeView with XML|XML2Tree Funktion]].
  
<pre>
+
<delphi>
 
procedure TForm1.actAddChildNode(Sender: TObject);
 
procedure TForm1.actAddChildNode(Sender: TObject);
 
var
 
var
Line 142: Line 142:
 
   end;
 
   end;
 
end;
 
end;
</pre>
+
</delphi>
  
 
== Erzeugen eines TXMLDocument aus einem String ==
 
== Erzeugen eines TXMLDocument aus einem String ==
Line 148: Line 148:
 
Given al XML file in MyXmlString, the following code will create it's DOM:
 
Given al XML file in MyXmlString, the following code will create it's DOM:
  
<pre>
+
<delphi>
 
Var
 
Var
 
   S : TStringStream;
 
   S : TStringStream;
Line 165: Line 165:
 
   end;
 
   end;
 
end;
 
end;
</pre>
+
</delphi>

Revision as of 11:00, 27 June 2007

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>