Difference between revisions of "LMDXML Tutorial"

From LMD
Jump to: navigation, search
m (LMDXML moved to LMDXML Tutorial)
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
 +
{{Head-Getting started}}
 +
 
== Example 1. Creating and saving document ==
 
== Example 1. Creating and saving document ==
<delphi>
+
 
 +
<syntaxhighlight lang="delphi">
 
  var
 
  var
 
   aDoc: ILMDXmlDocument;
 
   aDoc: ILMDXmlDocument;
Line 23: Line 26:
 
   aDoc.Save('Test.xml');
 
   aDoc.Save('Test.xml');
 
  end;
 
  end;
</delphi>
+
</syntaxhighlight>
  
 
There is content of saved file:
 
There is content of saved file:
<xml>
+
<syntaxhighlight lang="xml">
 
  <?xml version="1.0" encoding="windows-1251"?>
 
  <?xml version="1.0" encoding="windows-1251"?>
 
  <base>
 
  <base>
Line 32: Line 35:
 
   <elem2 a1="elem2 a1" a2="elem2 a2"/>
 
   <elem2 a1="elem2 a1" a2="elem2 a2"/>
 
  </base>
 
  </base>
</xml>
+
</syntaxhighlight>
  
== Example 2. Reading and analyzing documents ==
+
== Example 2. Reading and analyzing documents ==
External xml file to parse. Test.xml
+
 
<xml>
+
External xml file to parse. Test.xml  
 +
<syntaxhighlight lang="xml">
 
  <?xml version="1.0" encoding="window-1251"?>
 
  <?xml version="1.0" encoding="window-1251"?>
 
  <root>
 
  <root>
Line 43: Line 47:
 
   <elem2 a1="v1" a2="v2"> 2</elem2>
 
   <elem2 a1="v1" a2="v2"> 2</elem2>
 
  </root>
 
  </root>
</xml>
+
</syntaxhighlight>
  
Delphi code
+
Delphi code <syntaxhighlight lang="delphi">
<delphi>
 
 
  var
 
  var
 
   aDoc: ILMDXmlDocument;
 
   aDoc: ILMDXmlDocument;
Line 73: Line 76:
 
   Memo1.Lines.Add(anElem2.GetAttr('a2'));
 
   Memo1.Lines.Add(anElem2.GetAttr('a2'));
 
  end;
 
  end;
</delphi>
+
</syntaxhighlight>
 +
 
 +
== Example 3. It shows how to save structures to xml ==
 +
 
 +
<syntaxhighlight lang="delphi">
 +
type
 +
    TSex =(sxMale, sxFemale);
 +
 
 +
type
 +
    TAuthor = record
 +
        FirstName: string;
 +
        LastName: string;
 +
        Sex: TSex;
 +
    end;
 +
 
 +
type
 +
    TBook = record
 +
        ISBN: string;
 +
        Title: string;
 +
        Authors: array of TAuthor;
 +
    end;
 +
 
 +
var
 +
  LLibrary: array of TBook;
 +
 
 +
procedure WriteAuthor(AParent: ILMDXmlNode; AAuthor: TAuthor);
 +
begin
 +
  with AParent.AppendElement('author') do
 +
  begin
 +
    with AppendElement('firstname') do
 +
      AppendText(AAuthor.FirstName);
 +
    with AppendElement('lastname') do
 +
      AppendText(AAuthor.LastName);
 +
    SetAttr('sex', GetEnumName(TypeInfo(TSex), ord(AAuthor.Sex)));
 +
  end;
 +
end;
 +
 
 +
procedure WriteBook(AParent: ILMDXmlNode; ABook: TBook);
 +
var
 +
  i: integer;
 +
  LAuthors: ILMDXmlElement;
 +
begin
 +
  with AParent.AppendElement('book') do
 +
  begin
 +
    with AppendElement('isbn') do
 +
      AppendText(ABook.ISBN);
 +
    with AppendElement('title') do
 +
      AppendText(ABook.Title);
 +
    LAuthors := AppendElement('authors');
 +
    for i := Low(ABook.Authors) to High(ABook.Authors) do
 +
      WriteAuthor(LAuthors, ABook.Authors[i]);
 +
  end;
 +
end;
 +
 
 +
procedure TForm9.Button1Click(Sender: TObject);
 +
var
 +
  LXMLLib: ILMDXmlDocument;
 +
  i: Integer;
 +
begin
 +
  SetLength(LLibrary, 3);
 +
  LLibrary[0].ISBN := 'ISBN-10: 0586010807';
 +
  LLibrary[0].Title := 'Foundation';
 +
  SetLength(LLibrary[0].Authors, 1);
 +
  LLibrary[0].Authors[0].FirstName := 'Isaac';
 +
  LLibrary[0].Authors[0].LastName := 'Asimov';
 +
  LLibrary[0].Authors[0].Sex := sxMale;
 +
 
 +
  LLibrary[1].ISBN := 'ISBN-10: 0553293370';
 +
  LLibrary[1].Title := 'Foundation and Empire';
 +
  SetLength(LLibrary[1].Authors, 1);
 +
  LLibrary[1].Authors[0].FirstName := 'Isaac';
 +
  LLibrary[1].Authors[0].LastName := 'Asimov';
 +
  LLibrary[1].Authors[0].Sex := sxMale;
 +
 
 +
  LLibrary[2].ISBN := 'ISBN-10: 0543903508';
 +
  LLibrary[2].Title := 'A Connecticut Yankee in King Arthur''s Court';
 +
  SetLength(LLibrary[2].Authors, 1);
 +
  LLibrary[2].Authors[0].FirstName := 'Mark';
 +
  LLibrary[2].Authors[0].LastName := 'Twain';
 +
  LLibrary[2].Authors[0].Sex := sxMale;
 +
 
 +
  LXMLLib := LMDCreateXmlDocument('library');
 +
  for i := Low(LLibrary) to High(LLibrary) do
 +
    WriteBook(LXMLLib.DocumentElement, LLibrary[i]);
 +
  LXMLLib.Save('library.xml');
 +
end;
 +
</syntaxhighlight>
 +
 
 +
== Example 4. How to read XML with unknown structure an fill TTreeView with it ==
 +
 
 +
<syntaxhighlight lang="delphi">
 +
procedure XMLToTreeView(const AFileName, AOptElement, AOptAttr: String; ATreeView: TTreeView);
 +
var
 +
  LXMLDoc: ILMDXmlDocument;
 +
 
 +
  procedure FillTree(ARootElement: ILMDXmlNode; AParent: TTreeNode);
 +
  var
 +
    i: Integer;
 +
    LCurrentNode: TTreeNode;
 +
    LNode: ILMDXmlNode;
 +
  begin
 +
    for i := 0 to ARootElement.ChildNodes.Count - 1 do
 +
    begin
 +
      if (ARootElement.ChildNodes.Item[i].NodeType = LMD_NODE_ELEMENT) then
 +
      begin
 +
        LCurrentNode := ATreeView.Items.AddChild(AParent, ARootElement.ChildNodes[i].NodeName + ': ' +
 +
                                                ARootElement.ChildNodes[i].Text);
 +
        LNode := ARootElement.ChildNodes[i].SelectSingleNode(AOptElement);
 +
        if Assigned(LNode) then
 +
          LCurrentNode.Text := Format('Has child element %s, ', [AOptElement]) + LCurrentNode.Text;
 +
        if ARootElement.ChildNodes[i].AttrExists(AOptAttr) then
 +
        begin
 +
          LCurrentNode.Text := Format('Has the "%s" attribute, ', [AOptAttr]) + LCurrentNode.Text;
 +
        end;
 +
        FillTree(ARootElement.ChildNodes[i], LCurrentNode);
 +
      end;
 +
    end;
 +
  end;
 +
 
 +
begin
 +
  LXMLDoc := LMDCreateXmlDocument();
 +
  LXMLDoc.Load(AFileName);
 +
  FillTree(LXMLDoc.DocumentElement, nil);
 +
end;
 +
</syntaxhighlight>

Latest revision as of 13:35, 18 August 2017

<< Back to Getting started page

[edit]

Example 1. Creating and saving document

 var
   aDoc: ILMDXmlDocument;
   anElem: ILMDXmlElement;
 begin
   // Create empty XML document with root element 'base'
   aDoc := LMDCreateXmlDocument('base');

   // Create 'elem1' element and add it as child node for root element
   anElem := aDoc.DocumentElement.AppendElement('elem1');

   // Add 'a1' and 'a2' attributes to 'elem1'
   anElem.SetAttr('a1', 'elem1 a1');
   anElem.SetAttr('a2', 'elem1 a2');

   // Add another element 'elem2'
   anElem := aDoc.DocumentElement.AppendElement('elem');
   anElem.SetAttr('a1', 'elem2 a1');
   anElem.SetAttr('a2', 'elem2 a2');

   // Save document to file
   aDoc.Save('Test.xml');
 end;

There is content of saved file:

 <?xml version="1.0" encoding="windows-1251"?>
 <base>
   <elem1 a1="elem1 a1" a2="elem1 a2"/>
   <elem2 a1="elem2 a1" a2="elem2 a2"/>
 </base>

Example 2. Reading and analyzing documents

External xml file to parse. Test.xml

 <?xml version="1.0" encoding="window-1251"?>
 <root>
   <elem1 a1="v1" a2="v2"> 1</elem1>
   <!--  -->
   <elem2 a1="v1" a2="v2"> 2</elem2>
 </root>
Delphi code
 var
   aDoc: ILMDXmlDocument;
   anElem2: ILMDXmlNode;
 begin
   // Create empty XML document 
   aDoc := LMDCreateXmlDocument;

   // Read content from file
   aDoc.Load('Test.xml');

   // Show in Memo1 full text of xml that we read above
   Memo1.Lines.Text := aDoc.Xml;

   Memo1.Lines.Add('------------------------');

   // Search element with tag 'elem2'
   anElem2 := aDoc.DocumentElement.SelectSingleNode('elem2');

   // Show in Memo1 XML-text anElem2
   Memo1.Lines.Add(anElem2.Xml);

   Memo1.Lines.Add('------------------------');

   // Show in Memo1 value of 'a2' attribute from anElem2 element
   Memo1.Lines.Add(anElem2.GetAttr('a2'));
 end;

Example 3. It shows how to save structures to xml

type
    TSex =(sxMale, sxFemale);

type
    TAuthor = record
        FirstName: string;
        LastName: string;
        Sex: TSex;
    end;

type
    TBook = record
        ISBN: string;
        Title: string;
        Authors: array of TAuthor;
    end;

var
  LLibrary: array of TBook;

procedure WriteAuthor(AParent: ILMDXmlNode; AAuthor: TAuthor);
begin
  with AParent.AppendElement('author') do
  begin
    with AppendElement('firstname') do
      AppendText(AAuthor.FirstName);
    with AppendElement('lastname') do
      AppendText(AAuthor.LastName);
    SetAttr('sex', GetEnumName(TypeInfo(TSex), ord(AAuthor.Sex)));
  end;
end;

procedure WriteBook(AParent: ILMDXmlNode; ABook: TBook);
var
  i: integer;
  LAuthors: ILMDXmlElement;
begin
  with AParent.AppendElement('book') do
  begin
    with AppendElement('isbn') do
      AppendText(ABook.ISBN);
    with AppendElement('title') do
      AppendText(ABook.Title);
    LAuthors := AppendElement('authors');
    for i := Low(ABook.Authors) to High(ABook.Authors) do
      WriteAuthor(LAuthors, ABook.Authors[i]);
  end;
end;

procedure TForm9.Button1Click(Sender: TObject);
var
  LXMLLib: ILMDXmlDocument;
  i: Integer;
begin
  SetLength(LLibrary, 3);
  LLibrary[0].ISBN := 'ISBN-10: 0586010807';
  LLibrary[0].Title := 'Foundation';
  SetLength(LLibrary[0].Authors, 1);
  LLibrary[0].Authors[0].FirstName := 'Isaac';
  LLibrary[0].Authors[0].LastName := 'Asimov';
  LLibrary[0].Authors[0].Sex := sxMale;

  LLibrary[1].ISBN := 'ISBN-10: 0553293370';
  LLibrary[1].Title := 'Foundation and Empire';
  SetLength(LLibrary[1].Authors, 1);
  LLibrary[1].Authors[0].FirstName := 'Isaac';
  LLibrary[1].Authors[0].LastName := 'Asimov';
  LLibrary[1].Authors[0].Sex := sxMale;

  LLibrary[2].ISBN := 'ISBN-10: 0543903508';
  LLibrary[2].Title := 'A Connecticut Yankee in King Arthur''s Court';
  SetLength(LLibrary[2].Authors, 1);
  LLibrary[2].Authors[0].FirstName := 'Mark';
  LLibrary[2].Authors[0].LastName := 'Twain';
  LLibrary[2].Authors[0].Sex := sxMale;

  LXMLLib := LMDCreateXmlDocument('library');
  for i := Low(LLibrary) to High(LLibrary) do
    WriteBook(LXMLLib.DocumentElement, LLibrary[i]);
  LXMLLib.Save('library.xml');
end;

Example 4. How to read XML with unknown structure an fill TTreeView with it

procedure XMLToTreeView(const AFileName, AOptElement, AOptAttr: String; ATreeView: TTreeView);
var
  LXMLDoc: ILMDXmlDocument;

  procedure FillTree(ARootElement: ILMDXmlNode; AParent: TTreeNode);
  var
    i: Integer;
    LCurrentNode: TTreeNode;
    LNode: ILMDXmlNode;
  begin
    for i := 0 to ARootElement.ChildNodes.Count - 1 do
    begin
      if (ARootElement.ChildNodes.Item[i].NodeType = LMD_NODE_ELEMENT) then
      begin
        LCurrentNode := ATreeView.Items.AddChild(AParent, ARootElement.ChildNodes[i].NodeName + ': ' + 
                                                 ARootElement.ChildNodes[i].Text);
        LNode := ARootElement.ChildNodes[i].SelectSingleNode(AOptElement);
        if Assigned(LNode) then
          LCurrentNode.Text := Format('Has child element %s, ', [AOptElement]) + LCurrentNode.Text;
        if ARootElement.ChildNodes[i].AttrExists(AOptAttr) then
        begin
          LCurrentNode.Text := Format('Has the "%s" attribute, ', [AOptAttr]) + LCurrentNode.Text;
        end;
        FillTree(ARootElement.ChildNodes[i], LCurrentNode);
      end;
    end;
  end;

begin
  LXMLDoc := LMDCreateXmlDocument();
  LXMLDoc.Load(AFileName);
  FillTree(LXMLDoc.DocumentElement, nil);
end;