Difference between revisions of "LMDXML Tutorial"

From LMD
Jump to: navigation, search
m (LMDXML moved to LMDXML Tutorial)
Line 1: Line 1:
== 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>
 

Revision as of 16:17, 21 February 2008