使用 PHP 提取带有 ID 标记的 XML


Extract XML with ID tag using PHP

我有一个相对复杂的XML文件,如下所示,但有更多的"匹配"标签(类似于150个实体,每个实体包含100+"匹配"ID):

<Query results>    
  <Entity>
   <Name>Bill</Name>
   <Watchlist>
      <Match ID=1>
          <Info></Info>
      </Match>
      <Match ID=2>
          <Info></Info>
      </Match>
      ...
      <Match ID=99>
          <Info></Info>
      </Match>
  </Entity>
</Query results>

我编写了以下 PHP 来从每个"实体"的"匹配"标签中提取"信息",但它只提取"信息"的第一个"匹配"标签,即 ID=1:

xml=simplexml_load_file($file) or die("Error: Cannot create object");
foreach($xml->children() as $i) { 
    echo "Name:" . $i->Name . " <br /> ";
    echo "Info:" . $i->Watchlist->Match . " <br /> ";
} 

我想做的是从每个"实体"的所有"匹配"标签中提取"信息",以便我可以将它们放入将显示以下内容的 HTML 表中:

Name   Match
Bill   ID=1>Info
Bill   ID=2>Info
Bill   ID=3>Info
...
Bill   ID=99>Info

任何帮助提取所有ID将不胜感激。

**更新**

我尝试了以下代码:

$xml=simplexml_load_file($fileinfo) or die("Error: Cannot create object");
foreach($xml->children() as $i) { 
    echo "Name:" . $i->Name . " <br /> ";
    foreach($i->WatchList->Match as $match) {
        echo $i->WatchList->Match->Info;
    }
 } 

这给出了一些有趣的结果,它给了我正确数量的ID记录(154),但它重复了ID=1"信息"标签中的文本,即如果ID=1"信息"说"HELLO WORLD!"它重复了154次。

<?xml version="1.0" encoding="utf-16"?>
<Query_Results xmlns="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/ResultsExport.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/ResultsExport.xsd https://support.bridgerinsight.lexisnexis.com/downloads/xsd/4.2/ResultsExport.xsd">
<Entity RecordID="105998">
    <GeneralInfo>
        <EntityType>Unknown</EntityType>
        <Name>aleksandr borisovich totoonov</Name>
        <AccountID>74</AccountID>
        <IDLabel>Account ID</IDLabel>
        <IDNumber>74</IDNumber>
        <AlertState>Open</AlertState>
    </GeneralInfo>
    <RecordDetailInfo>
        <EntityType>Unknown</EntityType>
        <SearchDate>2014-06-30 10:50:27Z</SearchDate>
        <Origin>Batch</Origin>
        <FullName>aleksandr borisovich totoonov</FullName>
        <Identifications>
            <Type>Account ID</Type>
            <Number>74</Number>
        </Identifications>
    </RecordDetailInfo>
    <RecordHistoryInfo>
        <HistoryItem>
            <Event>Alert Opened</Event>
            <Note>Alert threshold met</Note>
            <DateAndTime>2014-06-30 10:50:29Z</DateAndTime>
            <UserName>System</UserName>
        </HistoryItem>
        <HistoryItem>
            <Event>Watchlist Search Conducted</Event>
            <Note>Watchlist search conducted</Note>
            <DateAndTime>2014-06-30 10:50:27Z</DateAndTime>
            <UserName>CEN</UserName>
        </HistoryItem>
        <HistoryItem>
            <Event>Record Created</Event>
            <Note>Record created from batch search of 'Ukraine Test 2.csv'</Note>
            <DateAndTime>2014-06-30 10:50:27Z</DateAndTime>
            <UserName>Liam McGinn</UserName>
        </HistoryItem>
    </RecordHistoryInfo>
    <WatchList>
        <Match ID="1">
            <EntityName>Totoonov, Aleksandr Borisovich</EntityName>
            <EntityScore>100</EntityScore>
            <BestName>Totoonov, Aleksandr Borisovich</BestName>
            <BestNameScore>100</BestNameScore>
            <FileName>FACTIVA PFA EUROPE.BDF</FileName>
            <SourceDate>2014-06-26 22:00:00Z</SourceDate>
            <DistributionDate>2014-06-27 15:52:44Z</DistributionDate>
            <ResultDate>2014-06-30 09:50:27Z</ResultDate>
            <EntityUniqueID>DJ768252</EntityUniqueID>
            <MatchDetails><Entity Type="2"><Number>768252</Number><Date>09/04/2014</Date><Reason>Politically Exposed Person (PEP); Special Interest Person (SIP); Sanctions Lists</Reason><CheckSum>722219</CheckSum><Gender>Male</Gender><Name><Full>Aleksandr Borisovich Totoonov</Full><First>Aleksandr</First><Middle>Borisovich</Middle><Last>Totoonov</Last></Name><Notes></Notes><AKAs><AKA ID="1" Type="1"><Full>Александр Борисович Тотоонов</Full></AKA><AKA ID="2" Type="1"><Full>Aleksander Borisovich Totoonov</Full><First>Aleksander</First><Middle>Borisovich</Middle><Last>Totoonov</Last></AKA><AKA ID="3" Type="1"><Full>Alexander Totoonov</Full><First>Alexander</First><Last>Totoonov</Last></AKA><AKA ID="4" Type="1"><Full>Alexander B. Totoonov</Full><First>Alexander</First><Middle>B.</Middle><Last>Totoonov</Last></AKA><AKA ID="5" Type="1"><Full>Alexander Borisovich Totoonov</Full><First>Alexander</First><Middle>Borisovich</Middle><Last>Totoonov</Last></AKA><AKA ID="6" Type="1"><Full>Alexandr Borisovich Totoonov</Full><First>Alexandr</First><Middle>Borisovich</Middle><Last>Totoonov</Last></AKA></AKAs><Addresses><Address ID="1" Type="1"><Country>Russia</Country><Notes>Resident of</Notes></Address></Addresses><IDs><ID ID="1" Type="27"><Number>768252</Number></ID></IDs><Descriptions><Description ID="1" Type="4"><Value>03/04/1957</Value></Description><Description ID="2" Type="4"><Value>03/03/1957</Value></Description><Description ID="3" Type="22"><Value>OFAC Program ID : UKRAINE2</Value></Description><Description ID="4" Type="22"><Value>OFAC Unique ID : 16674</Value></Description><Description ID="5" Type="22"><Value>EU Sanctions Programme Indicator : UKR</Value></Description><Description ID="6" Type="22"><Value>EU Consolidated Electronic List ID : 7294</Value></Description><Description ID="7" Type="22"><Value>HM Treasury Group ID : 12917</Value></Description><Description ID="8" Type="1"><Value>Russia</Value></Description><Description ID="9" Type="10"><Value>Member, Federation Council (Upper House), North Ossetia-Alania Republic to the Federal Government</Value><Notes>Type: Primary Occupation | Category: Members of the National Legislature | Start Date: 28 Nov 2012</Notes></Description><Description ID="10" Type="10"><Value>Deputy Prime Minister, North Ossetia-Alania Republic</Value><Notes>Type: Previous Roles | Category: Heads &amp; Deputy Heads of Regional Government | Start Date: 25 Jun 2010 | End Date: 28 Nov 2012</Notes></Description><Description ID="11" Type="10"><Value>Permanent Representative, North Ossetia-Alania Republic to the Federal Government</Value><Notes>Type: Previous Roles | Category: Senior Civil Servants-Regional Government | Start Date: Jun 2005 | End Date: 28 Nov 2012</Notes></Description><Description ID="12" Type="10"><Value>First Deputy Prime Minister, North Ossetia-Alania Republic</Value><Notes>Type: Previous Roles | Category: Heads &amp; Deputy Heads of Regional Government | Start Date: 30 Dec 2008 | End Date: 25 Jun 2010</Notes></Description><Description ID="13" Type="10"><Value>Deputy Prime Minister, North Ossetia-Alania Republic</Value><Notes>Type: Previous Roles | Category: Heads &amp; Deputy Heads of Regional Government | Start Date: Jun 2005 | End Date: 30 Dec 2008</Notes></Description><Description ID="14" Type="11"><Value>Vladikavkaz, Russia</Value></Description><Description ID="15" Type="11"><Value>Ordzhonikidze, Russia</Value></Description><Description ID="16" Type="11"><Value>Vladikavkaz, Russia</Value></Description><Description ID="17" Type="22"><Value>Source Descriptions</Value><Notes>https://www.gov.uk/government/uploads/system/uploads/attachment_data/file/293326/Notice_Ukraine_Sovereignty_180314.pdf | http://www.international.gc.ca/sanctions/russia_developments-developpements_russie3.aspx?lang=eng | http://www.treasury.gov/resource-center/sanctions/OFAC-Enforcement/Pages/20140320_33.aspx | http://www.treasury.gov/press-center/press-releases/Pages/jl23331.aspx | http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2014:078:0016:0021:EN:PDF | Council Decision 2014/145/CFSP | http://eur-lex.europa.eu/LexUriServ/LexUriServ.do?uri=OJ:L:2014:078:0006:0015:EN:PDF | Council Regulation (EU) No 269/2014 | http://hmt-sanctions.s3.amazonaws.com/sanctionsconlist.htm | http://www.international.gc.ca/sanctions/russia-russie.aspx?lang=eng | http://www.treasury.gov/ofac/downloads/t11sdn.pdf | http://www.seco.admin.ch/themen/00513/00620/00622/05405/index.html?lang=fr | Swiss Federal Council Ordinance 02-Apr-2014 (RS 946.231.176.72) | http://www.dfat.gov.au/sanctions/downloads/regulation8_consolidated.xls</Notes></Description><Description ID="18" Type="22"><Value>Images</Value><Notes>http://www.noar.ru/images/ID/abt_bujet.jpg | http://www.noar.ru/2jhgjhg.JPG | http://council.gov.ru/media/persons/186x248/41d495a126cd9dc74daf.jpg</Notes></Description><Description ID="19" Type="22"><Value>Sanctions</Value><Notes>OFAC - Specially Designated National List(Start Date: 20 Mar 2014); | HM Treasury Consolidated List(Start Date: 18 Mar 2014); | DFAT (Australia) Consolidated List(Start Date: 19 Jun 2014); | EC 269/2014 Ukraine List(Start Date: 17 Mar 2014); | EC 2014/145/CFSP Ukraine List(Start Date: 17 Mar 2014); | DFAIT (Canada) Special Economic Measures (Russia) Regulations(Start Date: 19 Mar 2014); | SECO (Switzerland) Ukraine List(Start Date: 02 Apr 2014);</Notes></Description></Descriptions></Entity></MatchDetails>
        </Match>
        <Match ID="2">
            <EntityName>Totoonov, Aleksandr Borisovich</EntityName>
            <EntityScore>100</EntityScore>
            <BestName>Totoonov, Aleksandr Borisovich</BestName>
            <BestNameScore>100</BestNameScore>
            <FileName>EU CONSOLIDATED LIST.BDF</FileName>
            <SourceDate>2014-06-26 14:41:06Z</SourceDate>
            <DistributionDate>2014-06-26 19:22:06Z</DistributionDate>
            <ResultDate>2014-06-30 09:50:27Z</ResultDate>
            <EntityUniqueID>LN0000382174</EntityUniqueID>
            <MatchDetails><Entity Type="2"><Number>7294</Number><CheckSum>23930</CheckSum><Gender>Male</Gender><Name><Full>Aleksandr Borisovich Totoonov</Full><First>Aleksandr</First><Middle>Borisovich</Middle><Last>Totoonov</Last></Name><Notes>Legal Basis: 269/2014 (OJ L 78) | Reg Date: 2014-03-17 | Additional Information: Date of listing: 17.3.2014</Notes><Descriptions><Description ID="1" Type="10"><Value>Member of the Committee on culture, science, and information of the Federation Council of the Russian Federation.</Value></Description><Description ID="2" Type="4"><Value>03/03/1957</Value><Notes>Legal Basis: 269/2014 (OJ L 78) | Reg Date: 2014-03-17</Notes></Description><Description ID="3" Type="11"><Value>Ordzhonikidze, North Ossetia, Russia</Value><Notes>Legal Basis: 269/2014 (OJ L 78) | Reg Date: 2014-03-17</Notes></Description></Descriptions></Entity></MatchDetails>
        </Match>
        <Match ID="3">
            <EntityName>TOTOONOV, Aleksandr Borisovich</EntityName>
            <EntityScore>100</EntityScore>
            <BestName>TOTOONOV, Aleksandr Borisovich</BestName>
            <BestNameScore>100</BestNameScore>
            <FileName>BANK OF ENGLAND CONSOLIDATED LIST.BDF</FileName>
            <SourceDate>2014-06-27 14:48:55Z</SourceDate>
            <DistributionDate>2014-06-27 19:05:46Z</DistributionDate>
            <ResultDate>2014-06-30 09:50:27Z</ResultDate>
            <EntityUniqueID>LN0000382212</EntityUniqueID>
            <MatchDetails><Entity Type="2"><Number>12917</Number><Date>18/03/2014</Date><CheckSum>14204</CheckSum><Name><Full>Aleksandr Borisovich TOTOONOV</Full><First>Aleksandr Borisovich</First><Last>TOTOONOV</Last></Name><Notes>Regime: Ukraine (Sovereignty) | Last Updated: 03/18/2014</Notes><Descriptions><Description ID="1" Type="4"><Value>03/03/1957</Value></Description><Description ID="2" Type="10"><Value>Member, Committee on Culture, Science and Information, Federation Council</Value></Description><Description ID="3" Type="11"><Value>Ordzhonikidze, North Ossetia</Value></Description></Descriptions></Entity></MatchDetails>
        </Match>
        <Match ID="4">
            <EntityName>TOTOONOV, ALEKSANDR BORISOVICH</EntityName>
            <EntityScore>100</EntityScore>
            <BestName>TOTOONOV, ALEKSANDR BORISOVICH</BestName>
            <BestNameScore>100</BestNameScore>
            <FileName>EPLS.BDF</FileName>
            <SourceDate>2014-06-26 14:37:04Z</SourceDate>
            <DistributionDate>2014-06-27 19:05:46Z</DistributionDate>
            <ResultDate>2014-06-30 09:50:27Z</ResultDate>
            <EntityUniqueID>EPS4MR4DF35</EntityUniqueID>
            <MatchDetails><Entity Type="2"><Number>S4MR4DF35</Number><Reason>Reciprocal</Reason><CheckSum>21546</CheckSum><Name><Full>ALEKSANDR BORISOVICH TOTOONOV</Full><First>ALEKSANDR BORISOVICH</First><Last>TOTOONOV</Last></Name><Notes>Cross-Reference: ALEXANDER B. TOTOONOV | ALEXANDER TOTOONOV</Notes><Addresses><Address ID="1"><Country>XUN</Country></Address></Addresses><Descriptions><Description ID="1" Type="22"><Value>Additional Comments</Value><Notes>PII data has been masked from view</Notes></Description><Description ID="2" Type="22"><Value>CT Code</Value><Notes>03-SDN-01</Notes></Description><Description ID="3" Type="22"><Value>Excluding Agency</Value><Notes>TREAS-OFAC</Notes></Description><Description ID="4" Type="22"><Value>Excluding Program</Value><Notes>Reciprocal</Notes></Description><Description ID="5" Type="22"><Value>Excluding Type</Value><Notes>Prohibition/Restriction</Notes></Description><Description ID="6" Type="22"><Value>Termination Date</Value><Notes>Indefinite</Notes></Description></Descriptions></Entity></MatchDetails>
        </Match>
        <Match ID="5">
            <EntityName>TOTOONOV, Aleksandr Borisovich</EntityName>
            <EntityScore>100</EntityScore>
            <BestName>TOTOONOV, Aleksandr Borisovich</BestName>
            <BestNameScore>100</BestNameScore>
            <FileName>AUSTRALIA DEPT OF FOREIGN AFFAIRS AND TRADE.BDF</FileName>
            <SourceDate>2014-06-27 14:53:13Z</SourceDate>
            <DistributionDate>2014-06-27 19:05:44Z</DistributionDate>
            <ResultDate>2014-06-30 09:50:27Z</ResultDate>
            <EntityUniqueID>LN0000387929</EntityUniqueID>
            <MatchDetails><Entity Type="2"><Number>2715</Number><Date>19/06/2014</Date><Reason>Autonomous (Ukraine)</Reason><CheckSum>22232</CheckSum><Name><Full>Aleksandr Borisovich TOTOONOV</Full><First>Aleksandr Borisovich</First><Last>TOTOONOV</Last></Name><Notes></Notes><Descriptions><Description ID="1" Type="4"><Value>03/03/1957</Value></Description></Descriptions></Entity></MatchDetails>
        </Match>
        <Match ID="6">
            <EntityName>TOTOONOV, Aleksandr Borisovich</EntityName>
            <EntityScore>100</EntityScore>
            <BestName>TOTOONOV, Aleksandr Borisovich</BestName>
            <BestNameScore>100</BestNameScore>
            <FileName>OSFI CONSOLIDATED LIST.BDF</FileName>
            <SourceDate>2014-06-21 14:51:44Z</SourceDate>
            <DistributionDate>2014-06-27 19:26:28Z</DistributionDate>
            <ResultDate>2014-06-30 09:50:27Z</ResultDate>
            <EntityUniqueID>LN0000382281</EntityUniqueID>
            <MatchDetails><Entity Type="2"><Number>12</Number><Date>19/03/2014</Date><Reason>SEMA Russia</Reason><CheckSum>11964</CheckSum><Name><Full>Aleksandr Borisovich TOTOONOV</Full><First>Aleksandr</First><Middle>Borisovich</Middle><Last>TOTOONOV</Last></Name><Descriptions><Description ID="1" Type="12"><Value>Member of the Committee on culture, science, and information of the Federation Council of the Russian Federation</Value></Description></Descriptions></Entity></MatchDetails>
        </Match>
        <Match ID="7">
            <EntityName>TOTOONOV, Aleksandr Borisovich</EntityName>
            <EntityScore>100</EntityScore>
            <BestName>TOTOONOV, Aleksandr Borisovich</BestName>
            <BestNameScore>100</BestNameScore>
            <FileName>OFAC SDN.BDF</FileName>
            <SourceDate>2014-06-26 14:13:19Z</SourceDate>
            <DistributionDate>2014-06-26 19:22:06Z</DistributionDate>
            <ResultDate>2014-06-30 09:50:27Z</ResultDate>
            <EntityUniqueID>LN0000382278</EntityUniqueID>
            <MatchDetails><Entity Type="2"><Number>16674</Number><Reason>UKRAINE2</Reason><CheckSum>22933</CheckSum><Name><Title>Member of the Committee on Culture, Science, and Information, Federation Council of the Russian Federation</Title><Full>Aleksandr Borisovich TOTOONOV</Full><First>Aleksandr Borisovich</First><Last>TOTOONOV</Last></Name><Notes>Program: UKRAINE2</Notes><AKAs><AKA ID="1" Type="1" Category="1"><Full>Alexander B. TOTOONOV</Full><First>Alexander B.</First><Last>TOTOONOV</Last></AKA><AKA ID="2" Type="1" Category="1"><Full>Alexander TOTOONOV</Full><First>Alexander</First><Last>TOTOONOV</Last></AKA></AKAs><Descriptions><Description ID="1" Type="4"><Value>03/03/1957</Value></Description><Description ID="2" Type="11"><Value>Ordzhonikidze, North Ossetia, Russia</Value></Description><Description ID="3" Type="11"><Value>Vladikavkaz, North Ossetia, Russia</Value></Description></Descriptions></Entity></MatchDetails>
        </Match>
    </WatchList>
</Entity>

以及我在现实生活中使用的 PHP:

<?php
    $it = new FilesystemIterator("uploads/");
       foreach ($it as $fileinfo) {
       echo " ";
       }
   echo "<table ID='"findings'">";
   echo "<tr>";
   echo "<th>Name</th>";
   echo "<th>Account ID</th>";
   echo "<th>Notes</th>";
   echo "</tr>";
        $xml=simplexml_load_file($fileinfo) or die("Error: Cannot create object");
        foreach($xml->children() as $i) { 
            echo "<tr>";
            echo "<td>" . $i->GeneralInfo->Name . " </td> ";
            echo "<td>" . $i->GeneralInfo->AccountID . "</td>";
            echo "<td>" . $i->WatchList->Match->MatchDetails->Entity->Notes . "</td>";
            echo "</tr>";
        } 
   echo "</table>";
?>

ID=99>Info一样查看输出,看起来你所看到的实际上是XML本身被浏览器错误地呈现为HTML,就像<Match ID=99>Info一样。 如果您查看浏览器的页面源代码,您可能会在那里找到大部分信息,但没有按预期组织。

让我们稍微备份一下,从根节点进行迭代。不使用children(),迭代节点名称本身。毕竟,这就是 SimpleXML 比 PHP 中其他 XML API 更容易使用的原因。

$xml=simplexml_load_file($fileinfo) or die("Error: Cannot create object");
// Loop over the Entity nodes:
foreach ($xml->Entity as $e) {
  // Write out the name, and all Match nodes in a loop:
  foreach ($e->Watchlist->Match as $m) {
    // The Name will be repeated for each Match since it is from
    // a level higher:
    echo "Name: " . (string)$e->Name . "<br/>";
    // The ID attribute with an array access to $m
    echo "ID: " . (string)$m['ID'] . "<br/>";
    // The <Info> node from $m
    echo "Info: " . (string)$m->Info . "<br/>";
  }
}

在循环内部,您可以重新格式化它们以将它们放入所需的输出中(例如,删除<br/>,或移动Name节点以在内部循环之前打印)。

下面是一个演示: http://codepad.viper-7.com/gCoc9g

另一个修改为使用您的真实 XML。请注意,节点名称区分大小写。所以<Watchlist><WatchList>不一样!