我在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的解决方案,只选择一个节点,但我需要能够选择所有节点。
谢谢大家。
答案 0 :(得分: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;
答案 1 :(得分:2)