Difference between revisions of "LMDXML Tutorial"

From LMD
Jump to: navigation, search
Line 1: Line 1:
 +
{{Head-Getting started}}
 +
 
== Example 1. Creating and saving document ==
 
== Example 1. Creating and saving document ==
 +
 
<delphi>
 
<delphi>
 
  var
 
  var
Line 25: Line 28:
 
</delphi>
 
</delphi>
  
There is content of saved file:
+
There is content of saved file: <xml>
<xml>
 
 
  <?xml version="1.0" encoding="windows-1251"?>
 
  <?xml version="1.0" encoding="windows-1251"?>
 
  <base>
 
  <base>
Line 34: Line 36:
 
</xml>
 
</xml>
  
== 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 <xml>
 
  <?xml version="1.0" encoding="window-1251"?>
 
  <?xml version="1.0" encoding="window-1251"?>
 
  <root>
 
  <root>
Line 45: Line 47:
 
</xml>
 
</xml>
  
Delphi code
+
Delphi code <delphi>
<delphi>
 
 
  var
 
  var
 
   aDoc: ILMDXmlDocument;
 
   aDoc: ILMDXmlDocument;

Revision as of 16:17, 5 November 2008

<< Back to Getting started page

[edit]

Example 1. Creating and saving document

<delphi>

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;

</delphi>

There is content of saved file: <xml>

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

</xml>

Example 2. Reading and analyzing documents

External xml file to parse. Test.xml <xml>

<?xml version="1.0" encoding="window-1251"?>
<root>
  <elem1 a1="v1" a2="v2"> 1</elem1>
  <elem2 a1="v1" a2="v2"> 2</elem2>
</root>

</xml>

Delphi code <delphi>

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;

</delphi>

Example 3. It shows how to save structures to xml

<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 Arthurs 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; </delphi>