Thema: Delphi XML auslesen

Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#3

AW: XML auslesen

  Alt 16. Feb 2014, 16:31
So geht's:

Inhalt von test3.xml
Delphi-Quellcode:
<?xml version="1.0" encoding="UTF-8"?>
<TESTFILE>
-<project>
<name>UBA </name>
<controller>1</controller>
<version>1.0</version>
<creationdate>2014-02-15</creationdate>
</project>
-<group>
<name>TEST</name>
-<element>
<name>KATEGORIE</name>
-<item>
<name>EINTRAG1</name>
<valueint>0</valueint>
<unit> </unit>
<write>0</write>
</item>
-<item>
<name>EINTRAG2</name>
<valueint>0</valueint>
<unit> </unit>
<write>0</write>
</item>
</element>
</group>
</TESTFILE>
Delphi-Quellcode:
procedure MM(s:String);
begin
  Form2.Memo1.lines.add(s);
end;

procedure ReadXMLFile2(const FileName:TFileName); //uses comobj
const Msxml2_DOMDocument='Msxml2.DOMDocument.6.0';
var XmlDoc, Nodes : OleVariant;
begin
  XmlDoc := CreateOleObject(Msxml2_DOMDocument);
  try
    XmlDoc.Async := False;
    XmlDoc.Load(FileName);
    XmlDoc.SetProperty('SelectionLanguage','XPath');
    if (XmlDoc.parseError.errorCode <> 0) then
       raise Exception.CreateFmt('Error in Xml Data %s',[XmlDoc.parseError]);

try Nodes := XmlDoc.selectNodes('//name');
MM(Nodes.Item(0).Text);
except on E:Exception do MM('ERROR'); end;

try Nodes := XmlDoc.selectNodes('//creationdate');
MM(Nodes.Item(0).Text);
except on E:Exception do MM('ERROR'); end;
// nach diesem Muster andere Nodes auslesen

  finally
   XmlDoc :=Unassigned;
  end;
end;

procedure TForm2.Button3Click(Sender: TObject);
begin
Memo1.Lines.LoadFromFile(ExtractFilePath(Application.ExeName)+'test3.xml');
    ReadXMLFile2(ExtractFilePath(Application.ExeName)+'test3.xml');
end;

Geändert von hathor (16. Feb 2014 um 16:34 Uhr)
  Mit Zitat antworten Zitat