1

我在 delphi 中使用 IXMLDocument 创建了一个 XML 文件,我需要访问每个名为 Manufacturer 的节点上的文本值,并在单击 Button1 时在 TListBox 中显示结果。

这是我创建 XML 文件的方式,在 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;

该代码重复了 4 次,向 XML 文件添加了 4 个更多元素,我没有在此处添加代码,因为我认为这不是必需的。完成的 XML 文件是这样的:

<?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>

我怎样才能做到这一点,当按下 Button1 时,再次访问 XML 文件,并且每个制造商的文本值都列在 ListBox1.Items 中。

我尝试了涉及 XPath 的解决方案来仅选择一个节点,但我需要能够选择所有节点。

谢谢大家。

4

2 回答 2

3

可能是这种方式(它未经测试,在这里使用在线 Delphi 参考编写,现在手头没有 Delphi):

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;
于 2012-10-09T10:38:19.760 回答
2

从文件加载文档:

通过 XPath 加载节点列表

枚举选定节点并提取文本

另外:https ://stackoverflow.com/questions/tagged/delphi+xpath

于 2012-10-09T10:11:28.620 回答