Parsing XML document

Hello!

I have a xml document

<?xml version="1.0"?>
<MAIN VERSION="5.5A">
<SUBSYS LV="XXX">
<LV>XXX</LV>
<SUBVERSION>5.51</SUBVERSION>
<SUBSYSNAME>SubVersionName</SUBSYSNAME>
<SUBTITLE>SubTitle</SUBTITLE>
</SUBSYS>
<MAIN>


I want to read all the values and attributes on dialog in my report.
var
Lxml: TfsXMLDocument;
Lnx: TfsXmlItem;
a: string;
begin
...
Lxml:= TfsXMLDocument.Create;
Lxml.LoadFromfile (...);
Lnx:= Lxml.Root;
a:= Lnx.Root.Name;
a:= a + ' ' + Lnx.Root.Prop('Version');
ShowMessage (a);
Lxml.free;
end;

I try and I can not get up to attributes on Subsys node.

Comments

  • hsmhsm
    edited 4:04AM
    Try something like this. I use this code to extract nodes that are repeated so you may not need the 'for' loop if you only have one node
    I also found that it is case sensitive to tag names.
    uses MSXML, 
    
    begin
    var
       i : integer;
       xml:IXMLDOMDocument;
       node:IXMLDOMNode;
       nodes_row: IXMLDomNodeList;
      TheLV,TheSubVersion, TheSubSysName, TheSubTitle : string;
    
    begin
    xml := CreateOleObject('Microsoft.XMLDOM') as IXMLDOMDocument;
    XML.async := false;
    XML.LoadXML(<full filename>);
    if XML.parseError.errorCode <> 0 then
            raise Exception.Create('XML Load error:' + xml.parseError.reason);
    
    nodes_row := xml.selectNodes('/MAIN /SUBSYS');
     for i := 0 to nodes_row.length - 1 do
        begin
            node := nodes_row.item[i]; //get one record
            TheLV := node.selectSingleNode('LV').text);
            TheSubVersion := node.selectSingleNode('SUBVERSION').text);
            TheSubSysName := node.selectSingleNode('SUBSYSNAME').text);
            TheSubTitle := node.selectSingleNode('SUBTITLE').text);
            //... now do something with TheLV,TheSubVersion, TheSubSysName & TheSubTitle 
            end;
    
     end;
    

Leave a Comment