Selecting Multiple XML Nodes from a document and then displaying in a TListbox Delphi

3.2k views Asked by At

i have used IXMLDocument in delphi to create an XML File, i need to access the text value on each of the nodes called Manufacturer and display the results in a TListBox when Button1 is Clicked.

Here is how i have created the XML File, this is called on FormCreate:

procedure TfrmMain.CreateXML;
var
  BikeXMLDoc: IXMLDocument;
  Root, Bike, Manufacturer, Model, Year, Ratio1, Ratio2, Ratio3, Ratio4, Ratio5,
  Ratio6, Ratio7, PriRatio: IXMLNode;
begin
  // Create XML Document for bikes.xml
  BikeXmlDoc := TXMLDocument.Create(nil);
  BikeXmlDoc.Active := True;
  BikeXmlDoc.Options := BikeXmlDoc.Options + [doNodeAutoIndent];
  BikeXmlDoc.Version := '1.0';

  // Create Document Root Element
  Root := BikeXmlDoc.CreateNode('Bikes');
  BikeXmlDoc.DocumentElement := Root;

  // Create First Bike Node
  Bike := BikeXmlDoc.CreateNode('Bike');

  // Add Required Elements with values for Honda Fireblade 2012
  Manufacturer := BikeXmlDoc.CreateNode('Manufacturer');
  Manufacturer.Text := 'Honda';
  Model := BikeXmlDoc.CreateNode('Model');
  Model.Text := 'Fireblade CBR1000';
  Year := BikeXmlDoc.CreateNode('Year');
  Year.Text := '2012';
  Ratio1 := BikeXmlDoc.CreateNode('Ratio1');
  Ratio1.Text := '2.286';
  Ratio2 := BikeXmlDoc.CreateNode('Ratio2');
  Ratio2.Text := '1.778';
  Ratio3 := BikeXmlDoc.CreateNode('Ratio3');
  Ratio3.Text := '1.500';
  Ratio4 := BikeXmlDoc.CreateNode('Ratio4');
  Ratio4.Text := '1.333';
  Ratio5 := BikeXmlDoc.CreateNode('Ratio5');
  Ratio5.Text := '1.214';
  Ratio6 := BikeXmlDoc.CreateNode('Ratio6');
  Ratio6.Text := '1.138';
  Ratio7 := BikeXmlDoc.CreateNode('Ratio7');
  PriRatio := BikeXmlDoc.CreateNode('PriRatio');
  PriRatio.Text := '1.717';

  // Add elements to XML File
  Root.ChildNodes.Add(Bike);
  Bike.ChildNodes.Add(Manufacturer);
  Bike.ChildNodes.Add(Model);
  Bike.ChildNodes.Add(Year);
  Bike.ChildNodes.Add(Ratio1);
  Bike.ChildNodes.Add(Ratio2);
  Bike.ChildNodes.Add(Ratio3);
  Bike.ChildNodes.Add(Ratio4);
  Bike.ChildNodes.Add(Ratio5);
  Bike.ChildNodes.Add(Ratio6);
  Bike.ChildNodes.Add(Ratio7);
  Bike.ChildNodes.Add(PriRatio);

  // Save the XML File
  //ShowMessage('XML File Created : ' + AppFileLocation + 'bikes.xml');
  BikeXmlDoc.SaveToFile(AppFileLocation+'bikes.xml');
  BikeXmlDoc.Active := False;
  BikeXmlDoc := nil;
end;

The code is repeated 4 more times adding 4 more elements to the XML File, i have not added the code here as i didnt think it was neccessary. The Finished XML File is this:

<?xml version="1.0"?>
<Bikes>
  <Bike>
    <Manufacturer>Honda</Manufacturer>
    <Model>Fireblade CBR1000</Model>
    <Year>2012</Year>
    <Ratio1>2.286</Ratio1>
    <Ratio2>1.778</Ratio2>
    <Ratio3>1.500</Ratio3>
    <Ratio4>1.333</Ratio4>
    <Ratio5>1.214</Ratio5>
    <Ratio6>1.138</Ratio6>
    <Ratio7/>
    <PriRatio>1.717</PriRatio>
  </Bike>
  <Bike>
    <Manufacturer>Kawasaki</Manufacturer>
    <Model>ZX6R 636</Model>
    <Year>2013</Year>
    <Ratio1>2.846</Ratio1>
    <Ratio2>2.200</Ratio2>
    <Ratio3>1.850</Ratio3>
    <Ratio4>1.600</Ratio4>
    <Ratio5>1.421</Ratio5>
    <Ratio6>1.300</Ratio6>
    <Ratio7/>
    <PriRatio>1.900</PriRatio>
  </Bike>
  <Bike>
    <Manufacturer>Suzuki</Manufacturer>
    <Model>GSXR1000</Model>
    <Year>2011</Year>
    <Ratio1>2.562</Ratio1>
    <Ratio2>2.052</Ratio2>
    <Ratio3>1.714</Ratio3>
    <Ratio4>1.500</Ratio4>
    <Ratio5>1.360</Ratio5>
    <Ratio6>1.269</Ratio6>
    <Ratio7/>
    <PriRatio>1.617</PriRatio>
  </Bike>
  <Bike>
    <Manufacturer>Triumph</Manufacturer>
    <Model>Daytona 675R</Model>
    <Year>2010</Year>
    <Ratio1>2.312</Ratio1>
    <Ratio2>1.857</Ratio2>
    <Ratio3>1.565</Ratio3>
    <Ratio4>1.350</Ratio4>
    <Ratio5>1.238</Ratio5>
    <Ratio6>1.136</Ratio6>
    <Ratio7/>
    <PriRatio>1.848</PriRatio>
  </Bike>
  <Bike>
    <Manufacturer>Yamaha</Manufacturer>
    <Model>YZF R1</Model>
    <Year>2013</Year>
    <Ratio1>2.533</Ratio1>
    <Ratio2>2.063</Ratio2>
    <Ratio3>1.762</Ratio3>
    <Ratio4>1.522</Ratio4>
    <Ratio5>1.364</Ratio5>
    <Ratio6>1.269</Ratio6>
    <Ratio7/>
    <PriRatio>1.512</PriRatio>
  </Bike>
</Bikes>

How can i make it so when Button1 is pressed the XML File is accessed again, and the text Value of each Manufacturer is listed in ListBox1.Items.

I have tried solutions involving XPath to select only a single node, but i need to be able to select all the nodes.

Thank you people.

2

There are 2 answers

1
TLama On BEST ANSWER

It could be this way (it's untested, written here using online Delphi reference, don't have Delphi by hand now):

uses
  XMLDoc, XMLIntf, XMLDOM;

procedure TForm1.Button1Click(Sender: TObject);
var
  I: Integer;
  XMLDocument: IXMLDocument;
  DOMNodeList: IDOMNodeList;
  DOMNodeSelect: IDOMNodeSelect;    
begin
  XMLDocument := LoadXMLDocument('c:\File.xml');
  if Assigned(XMLDocument) and 
    Supports(XMLDocument.DocumentElement.DOMNode, IDOMNodeSelect, DOMNodeSelect) then
  begin
    DOMNodeList := DOMNodeSelect.selectNodes('/Bikes/Bike/Manufacturer/text()');
    ListBox1.Items.BeginUpdate;
    try
      ListBox1.Items.Clear;
      for I := 0 to DOMNodeList.length - 1 do
        ListBox1.Items.Add(DOMNodeList.item[I].nodeValue);
    finally
      ListBox1.Items.EndUpdate;
    end;           
  end;
end;
0
Arioch 'The On