简体   繁体   English

解析从第三方 Web 服务读取的未知 XML

[英]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.

相关问题 如何停止从第三方组件重建.dcu? - How to stop rebuilding .dcu from third-party components? 任何第三方都可以从我的项目中加载嵌入的资源吗? - Can any third-party load embedded resources from my project? 如何在Delphi中对第三方电子邮件组件进行单元测试? - How to unit test third-party email components in Delphi? 分发带有第三方UI元素的设计时程序包 - Distributing a design-time package w/third-party UI elements 您如何对与第三方 COM 对象交互并实例化的代码进行单元测试? - How do you unit-test code that interacts with and instantiates third-party COM objects? 如何使用版本控制中的第三方组件管理Delphi项目? - How do you manage your Delphi Projects with third-party components in Version Control? 如何在第三方Delphi应用程序中获取控件的属性列表和值? - How to get the property list and values of a control within a third-party Delphi application? 有没有办法自动查找项目中使用的所有第三方组件? - Is there any way of automatically find all third-party components used on a project? 如何从第三方覆盖Delphi对象 - How To Override a Delphi Object from a Third Party 从第三方软件识别/列出COM对象和属性 - Identifying/Listing COM Objects and Properties from third party software
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM