Networking/ja

From Free Pascal wiki
Jump to navigationJump to search

Deutsch (de) English (en) español (es) français (fr) 日本語 (ja) 한국어 (ko) polski (pl) português (pt) русский (ru) slovenčina (sk) 中文(中国大陆)‎ (zh_CN)


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

このページは、Lazarusでのネットワークプログラムについてのチュートリアルを開始するページになるでしょう。 この記事に、リンクやセクション、ページ、あなたのWikiへのリンクを追加していってください。(訳注:最初にこの元記事を書いた人はネットワークプログラミングの専門でなく、独学で勉強したことを書いているそうです。)

このページには、一般的な情報があります。

その他のネットワークのチュートリアル

  • Sockets - TCP/IP ソケットコンポーネント
  • lNet - 軽量ネットワークコンポーネント

TCP/IP Protocol

XML

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

現在、Lazarus上では、XMLをサポートするユニット(群)があります。 これらのユニットは、"XMLRead", "XMLWrite" そして "DOM"と呼ばれていますが、これらは、FreePascalCompilerからの、FCLの一部になっています。

FCLライブラリはすでにLazarusのデフォルトの検索パスにはいっていますので、それらのユニットをusesするようにすれば、XMLの機能を享受できます。FCLは現在(October / 2005)のところ、文書化されていません。 ですから、このチュートリアルでは、これらのユニットをつかって、XMLアクセスの方法をご紹介することにしましょう。

XMLのDOM (Document Object Model)は、異なるシステム間でXMLを利用するための同じようなインターフェースを提供する、標準化されたオブジェクトの集合です。

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

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


基本的な例

たとえば、'C:\Programas\teste.xml'といったXMLファイルにアクセスしてみたいとしましょう。 このファイルは次のようなものだとします:

<?xml version="1.0" encoding="ISO-8859-1"?>
<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>

次のコードはノード名をフォーム上のTMemoに書き出す例です。

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;

TreeViewにXMLを表現する

One common use of XML files is to parse them and show their contents in a tree like format. You can find the TTreeView component on the "Common Controls" tab on Lazarus.

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.

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;

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:

   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;

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 with the contents of a XML file using the XML2Tree function.

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;

WebServices

According to the W3C a Web service is a software system designed to support interoperable machine-to-machine interaction over a network. It has an interface that is described in a machine-processable format such as WSDL. Other systems interact with the Web service in a manner prescribed by its interface using messages, which may be enclosed in a SOAP envelope, or follow a REST approach. These messages are typically conveyed using HTTP, and are normally comprised of XML in conjunction with other Web-related standards. Software applications written in various programming languages and running on various platforms can use web services to exchange data over computer networks like the Internet in a manner similar to inter-process communication on a single computer. This interoperability (e.g., between Windows and Linux applications) is due to the use of open standards. OASIS and the W3C are the primary committees responsible for the architecture and standardization of web services. To improve interoperability between web service implementations, the WS-I organisation has been developing a series of profiles to further define the standards involved.

Web Service Toolkit for FPC & Lazarus

Web Service Toolkit is a web services package for FPC and Lazarus.

External Links

XML