Difference between revisions of "XML Tutorial/ja"

From Free Pascal wiki
Jump to navigationJump to search
Line 218: Line 218:
 
   tmpNodes.Free;
 
   tmpNodes.Free;
 
  end;
 
  end;
 +
</code>
  
 
出力は以下のようになります。
 
出力は以下のようになります。
Line 244: Line 245:
 
   Opendialog1.Execute;
 
   Opendialog1.Execute;
 
   ReadXMLFile(Doc,Opendialog1.FileName);
 
   ReadXMLFile(Doc,Opendialog1.FileName);
 
+
 
   // ChildNodes メソッドの使用
 
   // ChildNodes メソッドの使用
 
   with Doc.DocumentElement.ChildNodes do
 
   with Doc.DocumentElement.ChildNodes do
Line 281: Line 282:
 
</pre>
 
</pre>
  
 +
=== XML を TreeView で表示する ===
 +
 +
XMLファイルの一般的な利用として、XMLファイルの内容を TreeView の形で表示することがあります。 Lazarusの ”Common Controls” タブに TTreeView コンポーネントがあります。
  
=== XML TreeView を作る ===
+
The function below will take a XML document previously loaded from a file or generated on code, and will populate a TreeView with it´s contents. The caption of each node will be the content of the first attribute of each node.
 +
以下の機能は、
 +
先にファイルから読み込んだか、コードの中で生成した XMLドキュメントを取得し、その内容をTreeViewの形で表示します。
 +
それぞれのノードのキャプションは、それぞれのノードのはじめの属性の内容となるでしょう。
  
一般的なXMLファイルの利用法として、XMLの内容を TreeView の形で見せることがあります。 Lazarusの ”Common Controls” タブに TTreeView コンポーネントがあります。
+
はじめの設定
  
The function below will take a XML document previously loaded from a file or generated on code, and will populate a TreeView with it´s contents. The caption of each node will be the content of the first attribute of each node.
+
1.usesに XMLRead, Dom を追加
  
 +
2.form1にmemo1,button1,OpenDialogを追加
  
<delphi>
+
<code>
procedure TForm1.XML2Tree(tree: TTreeView; XMLDoc: TXMLDocument);
+
procedure TForm1.XML2Tree(tree: TTreeView; XMLDoc: TXMLDocument);
var
+
var
 
   iNode: TDOMNode;
 
   iNode: TDOMNode;
  
Line 301: Line 309:
 
      
 
      
 
     // Adds a node to the tree
 
     // Adds a node to the tree
     TreeNode := tree.Items.AddChild(TreeNode, Node.Attributes[0].NodeValue);
+
     TreeNode := tree.Items.AddChild(TreeNode,  
 +
    Node.Attributes[0].NodeValue);
  
 
     // Goes to the child node
 
     // Goes to the child node
Line 314: Line 323:
 
   end;
 
   end;
 
      
 
      
begin
+
begin
  iNode := XMLDoc.DocumentElement.FirstChild;
+
  iNode := XMLDoc.DocumentElement.FirstChild;
  while iNode <> nil do
+
  while iNode <> nil do
  begin
+
  begin
    ProcessNode(iNode, nil); // Recursive
+
    ProcessNode(iNode, nil); // Recursive
    iNode := iNode.NextSibling;
+
    iNode := iNode.NextSibling;
  end;
+
  end;
end;
+
end;
</delphi>
+
</code>
 
 
 
 
=== TreeViewにXMLを表現する ===
 
 
 
XMLファイルの一般的な利用のひとつに、ツリー形式に構文を解析して、内容を表示することです。 Lazarusの上の「Common Control」タブに、ツリー形式に表示するためのTTreeViewコンポーネントがあります。
 
 
 
以下での機能は、あらかじめファイルからロードされたか、コードで作られたXMLドキュメントを取得し、コンテンツの内容をTreeViewに表示します。
 
それぞれのノードのキャプションは、それぞれの(XMLの内容の)最初の属性になるでしょう。
 
 
 
<pre>
 
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;
 
</pre>
 
 
 
 
 
  
  

Revision as of 14:08, 10 December 2008

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)

日本語版メニュー
メインページ - Lazarus Documentation日本語版 - 翻訳ノート - 日本語障害情報

導入

XML(=The Extensible Markup Language)とは、W3C が推奨する、異なるシステム間での情報のやりとりをするための言語です。 これは、情報を保存するために、テキストファイルを基礎としています。XHTMLのようなモダンなデータ交換方法 -それらはWebサービス技術などでもよく使われていますが - は、XMLを基礎としています。

現在、Lazarusには、XMLをサポートするユニット群があります。 これらのユニットは、"XMLRead", "XMLWrite" そして "DOM"と呼ばれていますが、これらは、Free Pascal Compiler における FCL(Free Component Library)の一部です。

FCLはすでにLazarusのデフォルトの検索パスにはいっていますので、XMLに関する機能を実装するには、それらのユニットをusesに追加するだけで可能となります。 FCLは現在(October / 2005 訳注たぶん2008/12時点も同じ)のところ、文書化されていませんので、このチュートリアルでは、これらのユニットをつかって、XMLへのアクセス方法を紹介することにします。

XMLのDOM (Document Object Model)は、異なる言語やシステム間でXMLを利用するために同じようなインターフェースを提供する、標準化されたオブジェクトの集合です。 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 XML DOM 1.0.

オブジェクトの標準化といっても、メソッド、プロパティ、オブジェクトの他へのインターフェース部分のみで、他の言語のための自由な実装は除去されています。 FCLは現在、完全にDOM 1.0をサポートしています.

(訳注:XML,DOMを「完全に」パースする、という実装は、かなり大変なことです。また、異機種間での利用を目的に作られていることに注意しましょう。Lazarusは、クロスコンパイル環境であり、これを標準で持っている事は、とても便利に快適にプログラムが出来ると思われます。日本語がどこまで検証されているのか、というところは、訳からはちょっと分かりませんが...。訳注注:日本語版wikiにおいて、"XML"の項目が"ネットワーク"に書かれていたときの訳注をそのまま乗せています。)

利用例

以下に、XMLデータの利用例を記述しています。徐々に複雑な内容を説明していきます。


textノードを読み込む

Delphi プログラマーの皆さん: TXMLDocumentを用いるときには注意して下さい。ノードのテキストは個々のTEXTノードとして取得されます。 Note that when working with TXMLDocument, the text within a Node is considered a separate TEXT Node.

したがって、分割ノードとしてノードのテキスト値を取得する必要があります。 As a result, you must access a node's text value as a separate node.

別の方法としては、TextContentプロパティによって、与えられた一つのノード以下の全てのテキストノードが、一つにまとまって取得できます。 Alternatively, the TextContent property may be used to retrieve content of all text nodes beneath the given one, concatenated together.

ReadXMLFile プロシージャは、いつも新しいTXMLDocumentを生成します。 このため、あらかじめTXMLDocumentを生成する必要はありません。しかし、不要になったときに、Freeをコールしてdocumentを破棄することを確実に行って下さい。

例えば、以下のxmlファイルについてアクセスする場合、

注:全てのxmlサンプル類、サンプルコードは、UTF-8で保存する必要があるようです。また、以下の例では文頭に"半角スペース"を記入して、整形済みテキストとして表示しており、ほとんどのコードはそのまま動きますが、<?xml><sample>は先頭の半角スペースを削除しないとうまく動きませんでした。

<xml>

<?xml version="1.0"?>
<sample name="s1" >sampleXML
  <group name="g1" >G1
    <user>123</user>
    <item val="int" >999</item>
    <item val="str" >abc</item>
  </group>
  <group name="g2" >G2
   <item val="str" >def</item>
   </group>
  <gr name="gr1" >GR1
    <item val="str" >def</item>
  </gr>
</sample>

</xml>

以下のコードで、テキストノードから値を取得する場合の、正しい方法と間違った方法をお見せします。

注:既存のコードがうまく動かなかったので(おそらく私のコンソールアプリの理解不足です)、全てのコードは"プロジェクト"->"新規プロジェクト"->"アプリケーション"から、GUIアプリとして作成しています。 (全てのコードはwindowsXPSP2上のlazarus0.9.26で動作確認しました。)

はじめの設定

1.usesに XMLRead, Domを追加

2.form1にmemo1,button1,OpenDialogを追加

procedure TForm1.Button1Click(Sender: TObject);
var
 tmpNode: TDOMNode;
 Doc: TXMLDocument;
begin
 // Doc := TXMLDocument.Create;//ここで生成する必要はありません。
 Memo1.Lines.Clear;

 // xml fileを読み込みます。
 Opendialog1.Execute;
 ReadXMLFile(Doc,Opendialog1.FileName);
 // "password" ノードを取得します。
 tmpNode := Doc.DocumentElement.FindNode('group');

 // 選択したノードの値の書き出し
 Memo1.Lines.Add(tmpNode.NodeValue);// 空白値""しか取得できません。
 // (ノードのテキスト値は、実際は個々の小ノードになっています。)
 Memo1.Lines.Add(tmpNode.FirstChild.NodeValue);// "abc" が取得できます。
 // 他の方法
 Memo1.Lines.Add(tmpNode.TextContent);
 // 最後に、documentを破棄します。
 Doc.Free;
end;

出力は以下のようになります。

 
G1
G1    123999abc

ノードの名前を出力する

DOMツリーの読み取り方の簡単なノート:ノードに順番にアクセスする必要がある場合、最善の方法は、FirstChildNextSibling プロパティ(前方から繰り返し), または LastChildPreviousSibling (後方から繰り返し)を用いることです。

ランダムアクセスには、ChildNodes または GetElementsByTagName メソッドが使えますが、これらは 最終的に破棄する必要のある TDOMNodeList オブジェクトを生成します。

これは、他の DOM 実装(例えばMXSML)とは異なります。この理由は、FCL での実装がobject-basedであり、interface-based では無いためです。

以下の例では、どのようにform上のTMemoにノードの名前を出力するかを示します。 (訳注:元々の例は、上側の例と異なりおそらくイタリアその辺りの人が書かれているようですので(ファイル名がtestoとか)、記述を上記例に統一しました。)

以下のxmlファイルにアクセスする場合、

<xml>

<?xml version="1.0"?>
<sample name="s1" >sampleXML
  <group name="g1" >G1
    <user>123</user>
    <item val="int" >999</item>
    <item val="str" >abc</item>
  </group>
  <group name="g2" >G2
   <item val="str" >def</item>
   </group>
  <gr name="gr1" >GR1
    <item val="str" >def</item>
  </gr>
</sample>

</xml>


はじめの設定

1.usesに XMLRead, Dom を追加

2.form1にmemo1,button1,OpenDialogを追加

1a.FirstChildNextSibling プロパティを使用(順番にアクセス) (訳注:英語版のコードは上記ノートと利用プロパティ、メソッドに整合性が無いため修正しました。)

procedure TForm1.Button1Click(Sender: TObject);
var
 Child: TDOMNode;
 Doc: TXMLDocument;
 cnt: Integer;
begin
 Memo1.Lines.Clear;
 Opendialog1.Execute;
 ReadXMLFile(Doc,Opendialog1.FileName);

 // FirstChild プロパティの使用
 Child := Doc.DocumentElement.FirstChild;
 while Assigned(Child) do
   begin
     cnt:=cnt+1;
     Memo1.Lines.Add(inttostr(cnt)
       + ' ' + Child.NodeName + ' ' + Child.NodeValue);
     // NextSibling プロパティの使用
     Child := Child.NextSibling;
   end;
 Doc.Free;
end;

出力は以下のようになります。

1 #text sampleXML
2 group 
3 group 
4 gr 

1b.ChildNodesgetElementsByTagName メソッドを使用(ランダムアクセス) 注)DOMNodeListのノード数はCountとなる。

procedure TForm1.Button1Click(Sender: TObject);
var
 tmpNodes: TDOMNodeList;
 Doc: TXMLDocument;
 i: Integer;
begin
 Memo1.Lines.Clear;
 Opendialog1.Execute;
 ReadXMLFile(Doc,Opendialog1.FileName);

 // ChildNodes メソッドの使用
 tmpNodes:=Doc.DocumentElement.ChildNodes;
 Memo1.Lines.Add('1.ChildNodes count=' + inttostr(tmpNodes.Count));
 if(tmpNodes.Count <> 0)  then
 for i:=0 to tmpNodes.Count-1 do begin
     Memo1.Lines.Add(inttostr(i)
      + ' ' + tmpNodes[i].NodeName
      + ' ' + tmpNodes[i].NodeValue);
 end;
 tmpNodes.Free;

 // getElementsByTagName メソッドの使用
 tmpNodes:= Doc.GetElementsByTagName('group');
 Memo1.Lines.Add('2.getEBTN count=' + inttostr(tmpNodes.Count));
 if(tmpNodes.Count <> 0)  then
 for i:=0 to tmpNodes.Count-1 do begin
     Memo1.Lines.Add(inttostr(i)
      + ' ' + tmpNodes[i].NodeName
      + ' ' + tmpNodes[i].NodeValue);
 end;
 tmpNodes.Free;
end;

出力は以下のようになります。

 1.ChildNodes count=4
 0 #text sampleXML
 1 group 
 2 group 
 3 gr 
 2.getEBTN count=2
 0 group 
 1 group 

2.ChildNodes メソッドを用いた順次アクセスのコードは以下のようになります。 (注:日本語版の"ネットワーク"項目のXMLに記述されていたコードがあったので、とりあえず入れておきます。)

procedure TForm1.Button1Click(Sender: TObject);
var
 Doc: TXMLDocument;
 i, j: Integer;
begin
 Memo1.Lines.Clear;
 Opendialog1.Execute;
 ReadXMLFile(Doc,Opendialog1.FileName);

 // ChildNodes メソッドの使用
 with Doc.DocumentElement.ChildNodes do
 begin
   for i := 0 to (Count - 1) do
   begin
     Memo1.Lines.Add('i' + inttostr(i)
        + Item[i].NodeName + ' ' + Item[i].NodeValue);
     for j := 0 to (Item[i].ChildNodes.Count - 1) do
     begin
       Memo1.Lines.Add(' j' + inttostr(j)
        + ' ' + Item[i].ChildNodes.Item[j].NodeName
        + ' ' + Item[i].ChildNodes.Item[j].NodeValue);
     end;
   end;
 end;
 Doc.Free;
end;

出力は以下のようになります。

 i0#text sampleXML
 i1group 
  j0 #text G1
  j1 user 
  j2 item 
  j3 item 
 i2group 
  j0 #text G2
  j1 item 
 i3gr 
  j0 #text GR1
  j1 item 

XML を TreeView で表示する

XMLファイルの一般的な利用として、XMLファイルの内容を TreeView の形で表示することがあります。 Lazarusの ”Common Controls” タブに TTreeView コンポーネントがあります。

The function below will take a XML document previously loaded from a file or generated on code, and will populate a TreeView with it´s contents. The caption of each node will be the content of the first attribute of each node. 以下の機能は、 先にファイルから読み込んだか、コードの中で生成した XMLドキュメントを取得し、その内容をTreeViewの形で表示します。 それぞれのノードのキャプションは、それぞれのノードのはじめの属性の内容となるでしょう。

はじめの設定

1.usesに XMLRead, Dom を追加

2.form1にmemo1,button1,OpenDialogを追加

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.FirstChild;
   // Processes all child nodes
   while cNode <> nil do
   begin
     ProcessNode(cNode, TreeNode);
     cNode := cNode.NextSibling;
   end;
 end;
   
begin
  iNode := XMLDoc.DocumentElement.FirstChild;
  while iNode <> nil do
  begin
    ProcessNode(iNode, nil); // Recursive
    iNode := iNode.NextSibling;
  end;
end;


Modifying a XML document

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.

Below are some common methods from 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 locate the selected item on a TTreeView and then insert a child node to the XML document it represents. The TreeView must be previously filled with the contents of a XML file using the XML2Tree function.

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

 position: Integer;
 NovoNo: TDomNode;

begin

 {*******************************************************************
 *  Detects the selected element
 *******************************************************************}
 if TreeView1.Selected = nil then Exit;
 if TreeView1.Selected.Level = 0 then
 begin
   position := TreeView1.Selected.Index;
   NovoNo := XMLDoc.CreateElement('item');
   TDOMElement(NovoNo).SetAttribute('nome', 'Item');
   TDOMElement(NovoNo).SetAttribute('arquivo', 'Arquivo');
   with XMLDoc.DocumentElement.ChildNodes do
   begin
     Item[position].AppendChild(NovoNo);
     Free;
   end;
   {*******************************************************************
   *  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>

Create a TXMLDocument from a 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); // Complete XML document
   // Alternatively:
   ReadXMLFragment(AParentNode,S); // Read only XML fragment.
 Finally
   S.Free;
 end;

end; </delphi>

Validating a document

Since March 2007, DTD validation facility has been added to the FCL XML parser. Validation is checking that logical structure of the document conforms to the predefined rules, called Document Type Definition (DTD).

Here is an example of XML document with a DTD:

<xml>

 <?xml version='1.0'?>
 <!DOCTYPE root [
 <!ELEMENT root (child)+ >
 <!ELEMENT child (#PCDATA)>
 ]>
 <root>
   <child>This is a first child.</child>
   <child>And this is the second one.</child>
 </root>

</xml>

This DTD specifies that 'root' element must have one or more 'child' elements, and that 'child' elements may have only character data inside. If parser detects any violations from these rules, it will report them.

Loading such document is slightly more complicated. Let's assume we have XML data in a TStream object:

<delphi> procedure TMyObject.DOMFromStream(AStream: TStream); var

 Parser: TDOMParser;
 Src: TXMLInputSource;
 TheDoc: TXMLDocument;

begin

 // create a parser object
 Parser := TDOMParser.Create;
 // and the input source
 Src := TXMLInputSource.Create(AStream);
 // we want validation
 Parser.Options.Validate := True;
 // assign a error handler which will receive notifications
 Parser.OnError := @ErrorHandler;
 // now do the job
 Parser.Parse(Src, TheDoc);
 // ...and cleanup
 Src.Free;
 Parser.Free;

end;

procedure TMyObject.ErrorHandler(E: EXMLReadError); begin

 if E.Severity = esError then  // we are interested in validation errors only
   writeln(E.Message);

end; </delphi>

Generating a XML file

Below is the complete code to write in a XML file. (This was taken from a tutorial in DeveLazarus blog ) Please, remember DOM and XMLWrite libs in uses clause

<delphi> unit Unit1;

{$mode objfpc}{$H+}

interface

uses

 Classes, SysUtils, LResources, Forms, Controls, Graphics, Dialogs, StdCtrls,
 DOM, XMLWrite;

type

 { TForm1 }
 TForm1 = class(TForm)
   Button1: TButton;
   Label1: TLabel;
   Label2: TLabel;
   procedure Button1Click(Sender: TObject);
 private
   { private declarations }
 public
   { public declarations }
 end;
 

var

 Form1: TForm1;
 

implementation

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject); var

 xdoc: TXMLDocument;                                  // variable to document
 RootNode, parentNode, nofilho: TDOMNode;                    // variable to nodes

begin

 //create a document
 xdoc := TXMLDocument.create;
 //create a root node
 RootNode := xdoc.CreateElement('register');
 Xdoc.Appendchild(RootNode);                           // save root node
 //create a parent node
 RootNode:= xdoc.DocumentElement;
 parentNode := xdoc.CreateElement('usuario');
 TDOMElement(parentNode).SetAttribute('id', '001');       // create atributes to parent node
 RootNode.Appendchild(parentNode);                          // save parent node
 //create a child node
 parentNode := xdoc.CreateElement('nome');                // create a child node
 //TDOMElement(parentNode).SetAttribute('sexo', 'M');     // create atributes
 nofilho := xdoc.CreateTextNode('Fernando');         // insert a value to node
 parentNode.Appendchild(nofilho);                         // save node
 RootNode.ChildNodes.Item[0].AppendChild(parentNode);       // insert child node in respective parent node
 //create a child node
 parentNode := xdoc.CreateElement('idade');               // create a child node
 //TDOMElement(parentNode).SetAttribute('ano', '1976');   // create atributes
 nofilho := xdoc.CreateTextNode('32');               // insert a value to node
 parentNode.Appendchild(nofilho);                         // save node
 .ChildNodes.Item[0].AppendChild(parentNode);       // insert a childnode in respective parent node
 writeXMLFile(xDoc,'teste.xml');                     // write to XML
 Xdoc.free;                                          // free memory

end;

initialization

 {$I unit1.lrs}

end. </delphi>

The result will be the XML file below: <xml> <?xml version="1.0"?> <register>

 <usuario id="001">
   <nome>Fernando</nome>
   <idade>32</idade>
 </usuario>

</register> </xml>

--Fernandosinesio 22:28, 24 April 2008 (CEST)fernandosinesio@gmail.com

Encoding

According to the XML standard, although there may be an encoding attribute in the first line of the XML, there is no need for it. As of version 0.9.26 of Lazarus, there is an encoding property in a TXMLDocument, but it is ignored. writeXMLFile always uses UTF-8 and doesn´t generate an encoding attribute in first line of the XML file.

External Links


Multithreaded Application Tutorial