delphi 用 xmldocument 读取 xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20119483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 11:56:22  来源:igfitidea点击:

delphi read xml with xmldocument

xmldelphi

提问by user2997342

I have this XML. I know how to read "EDI_DC40" and get values.

我有这个 XML。我知道如何阅读“EDI_DC40”并获取值。

<ns0:ORDERS xmlns:ns0="http://Purchasing/GenericPartner">
  <EDI_DC40>
    <TABNAM>EDI_DC40</TABNAM>
    <MANDT>210</MANDT>
    <DOCNUM>1498177</DOCNUM>
    <DOCREL>700</DOCREL>
    <STATUS>30</STATUS>
    <DIRECT>1</DIRECT>
    <OUTMOD>2</OUTMOD>
    <EXPRSS></EXPRSS>
    <TEST></TEST>
    <IDOCTYP>ORDERS05</IDOCTYP>
    <CIMTYP></CIMTYP>
    <MESTYP>ORDERS</MESTYP>
    <MESCOD></MESCOD>
    <MESFCT>GEN</MESFCT>
    <STD></STD>
    <STDVRS></STDVRS>
    <STDMES>ORDERS</STDMES>
    <SNDPOR>SAPR3D</SNDPOR>
    <SNDPRT>LS</SNDPRT>
    <SNDPFC></SNDPFC>
    <SNDPRN>R3DCLNT210</SNDPRN>
    <SNDSAD></SNDSAD>
    <SNDLAD></SNDLAD>
    <RCVPOR>BT_EDIPURC</RCVPOR>
    <RCVPRT>LI</RCVPRT>
    <RCVPFC>BA</RCVPFC>
    <RCVPRN>0002000007</RCVPRN>
    <RCVSAD></RCVSAD>
    <RCVLAD></RCVLAD>
    <CREDAT>20101124</CREDAT>
    <CRETIM>085946</CRETIM>
    <REFINT></REFINT>
    <REFGRP></REFGRP>
    <REFMES></REFMES>
    <ARCKEY></ARCKEY>
    <SERIAL>20101116202544</SERIAL>
  </EDI_DC40>
  <E1EDK01>
    <CURCY>EUR</CURCY>
    <ZTERM>Z50D</ZTERM>
    <BELNR>4600006935</BELNR>
  </E1EDK01>
  <E1EDK03>
    <IDDAT>012</IDDAT>
    <DATUM>20101116</DATUM>
  </E1EDK03>
  <E1EDKA1>
    <PARVW>AG</PARVW>
    <TELF1>11111</TELF1>
    <TELFX>+111</TELFX>
    <BNAME>mataz</BNAME>
  </E1EDKA1>
  <E1EDKA1>
    <PARVW>WE</PARVW>
    <LIFNR>1000</LIFNR>
    <NAME1>skl</NAME1>
    <NAME2></NAME2>
    <STRAS>ulica skladi??a 5</STRAS>
    <STRS2></STRS2>
    <ORT01>novo</ORT01>
    <PSTLZ>1000</PSTLZ>
    <LAND1>AU</LAND1>
  </E1EDKA1>
  <E1EDK17>
    <QUALF>001</QUALF>
    <LKOND>CIP</LKOND>
    <LKTEXT>Novo</LKTEXT>
  </E1EDK17>
  <E2EDP01008GRP>
    <E1EDP01>
      <POSEX>00001</POSEX>
      <MENGE>100.000</MENGE>
      <MENEE>PCE</MENEE>
      <VPREI>1.24</VPREI>
      <PEINH>100</PEINH>
      <NETWR>1.24</NETWR>
    </E1EDP01>
    <E1EDP20>
      <WMENG>100.000</WMENG>
      <EDATU>20101126</EDATU>
    </E1EDP20>
    <E1EDP19>
      <QUALF>001</QUALF>
      <IDTNR>000000000000201853</IDTNR>
      <KTEXT>NA</KTEXT>
    </E1EDP19>
  </E2EDP01008GRP>
  <E2EDP01008GRP>
    <E1EDP01>
      <POSEX>00001</POSEX>
      <MENGE>1000.000</MENGE>
      <MENEE>PCE</MENEE>
      <VPREI>2.24</VPREI>
      <PEINH>200</PEINH>
      <NETWR>1.4</NETWR>
    </E1EDP01>
    <E1EDP20>
      <WMENG>200.000</WMENG>
      <EDATU>20101126</EDATU>
    </E1EDP20>
    <E1EDP19>
      <QUALF>001</QUALF>
      <IDTNR>000000000000201853</IDTNR>
      <KTEXT>NA.CIPRINOL TBL A5 RU</KTEXT>
    </E1EDP19>
  </E2EDP01008GRP>
  <E1EDS01>
    <SUMID>002</SUMID>
    <SUMME>1.24</SUMME>
  </E1EDS01>
</ns0:ORDERS>

But i don't know how to read "E2EDP01008GRP" and value inside "POSEX", especially if there are two nodes of the same name. Can anyone point me to right direction or show me in code?

但我不知道如何读取“E2EDP01008GRP”和“POSEX”中的值,特别是如果有两个同名节点。任何人都可以指出我正确的方向或向我展示代码吗?

回答by bummi

Since you mentioned the usage of IXMLDocument in your last question regading this document, an example implementation could look like this:

由于您在有关本文档的上一个问题中提到了 IXMLDocument 的使用,因此示例实现可能如下所示:

var
  DOC:IXMLDocument;
  i: Integer;
  OrderChilds, E1EDP01_Node: IXMLNode;
begin
  DOC := LoadXMLDocument('c:\temp\orders.xml');
  // iterate your root node 'OREDERS'
  for i := 0 to DOC.ChildNodes.Nodes['ORDERS'].ChildNodes.Count - 1 do
  begin
    OrderChilds := DOC.ChildNodes.Nodes['ORDERS'].ChildNodes[i];
    if OrderChilds.NodeName = 'E2EDP01008GRP' then  // Your node(s) of interest
    begin
      E1EDP01_Node := OrderChilds.ChildNodes.Nodes['E1EDP01'];
      if Assigned(E1EDP01_Node) then // did we find the node keeping the 'POSEX' node
        Memo1.Lines.Add(E1EDP01_Node.ChildNodes.Nodes['POSEX'].NodeValue)
    end;
  end;
end;

回答by Arioch 'The

You just read node #1, then you read node #2 then you read node #3, etc. If they would have the same names or not - does not matter. Just read them one after another.

您只需读取节点#1,然后读取节点#2,然后读取节点#3,依此类推。如果它们具有相同的名称,则无关紧要。一个接一个地阅读它们。

You can enumerate them like this: superobject.googlecode.com/git/readme.html after using XML loader superobject.googlecode.com/git

您可以像这样枚举它们: superobject.googlecode.com/git/readme.html 在使用 XML 加载器 superobject.googlecode.com/git 之后

Or you can take any other XML parser.

或者您可以使用任何其他 XML 解析器。

et cetera

等等

You may also try some EDI-targeted library like http://edisdk.sourceforge.net/index.htmthat was later merged into Jedi Code Library (but could be was abandoned and removed later)

您也可以尝试一些以 EDI 为目标的库,例如http://edisdk.sourceforge.net/index.htm,后来合并到 Jedi Code Library(但可能会在以后被放弃和删除)