[英]Parsing an unknown XML read from a third-party webservice
I'm trying to analyze the values of an XML like this (obtained from a third party web service), inserting the values in a tStringlist to examine them later我正在尝试分析像这样的 XML 的值(从第三方 web 服务获得),将值插入 tStringlist 以稍后检查它们
<?xml version="1.0" encoding="utf-8"?>
<libretti pagina="1" n_pagine="1" n_libretti="2">
<libretto cod_catasto="202000000011"
cod_chiave="5e9f2bf12" completo="si"
data="2020-04-21" resp_cognome="Colombo"
resp_nome="Gianluca" ub_comune="VILLAFRANCA DI VERONA"
ub_provincia="VR" ub_indirizzo="Via dei pini"
ub_civico="1" ub_palazzo="" ub_scala="" ub_interno=""/>
<libretto cod_catasto="202000000012"
cod_chiave="5e9f2bfsw" completo="si"
data="2020-04-22" resp_cognome="Palumso"
resp_nome="Federico" ub_comune="VILLAFRANCA DI VERONA"
ub_provincia="VR" ub_indirizzo="Via Prosetta"
ub_civico="2" ub_palazzo="" ub_scala="" ub_interno=""/>
</libretti>
What I really need is to get a simple list of all the vaules in it like the following.我真正需要的是获得其中所有价值的简单列表,如下所示。
For example:例如:
libretto:
cod_catasto="202000000011"
cod_chiave="5e9f2bf12"
ompleto="si"
data="2020-04-21"
resp_cognome="Colombo"
resp_nome="Gianluca"
ub_comune="VILLAFRANCA DI VERONA"
ub_provincia="VR"
ub_indirizzo="Via dei pini"
ub_civico="1"
ub_palazzo=""
ub_scala=""
ub_interno=""
and so on...等等...
I'm stuck with the code below, which doesn't list all the values but stops at the first one, showing only the value "libretto="我坚持使用下面的代码,它没有列出所有值,而是停在第一个值,只显示值“libretto=”
procedure TForm5.ParseCercaXMLToMemo(TestXML:String);
var
Doc: IXMLDocument;
i: Integer;
LDocument: IXMLDocument;
LNodeElement, LNode: IXMLNode;
LAttrValue: string;
begin
LDocument := TXMLDocument.Create(nil);
LDocument.LoadFromXML(TestXML);
{ Find a specific node. }
LNodeElement := LDocument.ChildNodes.FindNode('libretti');
if (LNodeElement <> nil) then
begin
for I := 0 to LNodeElement.ChildNodes.Count - 1 do
begin
LNode := LNodeElement.ChildNodes.Get(I);
memo1.lines.add(LNode.NodeName + '=' + LNode.Text);
end;
end;
end;
As suggested by @Oliver I tried this code that finally works as expected.正如@Oliver 所建议的那样,我尝试了这段代码,最终按预期工作。
procedure TForm5.ParseCercaXMLToMemo(TestXML:String);
var
Doc: IXMLDocument;
i,y: Integer;
LDocument: IXMLDocument;
LNodeElement, LNode: IXMLNode;
LAttrValue: string;
begin
LDocument := TXMLDocument.Create(nil);
LDocument.LoadFromXML(TestXML); { File should exist. }
{ Find a specific node. }
LNodeElement := LDocument.ChildNodes.FindNode('libretti');
if (LNodeElement <> nil) then
begin
for I := 0 to LNodeElement.ChildNodes.Count - 1 do
begin
for y := 0 to LNodeElement.ChildNodes[i].AttributeNodes.count-1 do
begin
memo1.lines.add(LNodeElement.ChildNodes[i].AttributeNodes[y].LocalName+' = '+LNodeElement.ChildNodes[i].AttributeNodes[y].Text);
end;
end;
end;
end;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.