douyan4958 2014-12-07 11:25
浏览 119
已采纳

使用PHP使用ID标记提取XML

I have a relatively complex XML file which looks like the following but with many more 'Match' tags (something like 150 Entities, each containing 100+ 'Match' IDs):

<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>

I have written the following PHP to extract 'Info' from the 'Match' tag for every 'Entity' but it only extracts 'Info' the first of the 'Match' tags i.e. 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 /> ";
} 

What I want to do is extract the 'Info' from all the 'Match' tags for each 'Entity' so I can put them into a HTML table that will show the following:

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

Any help to extract all the ID's would be much appreciated.

** UPDATE **

I tried the following code:

$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;
    }
 } 

This gave some interesting results, it gave me the right number of ID records (154) but it repeated the text in ID=1 'Info' tag i.e. if ID=1 'Info' said "HELLO WORLD!" it repeated this exactly 154 times.

<?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>

And my PHP as used in real life:

<?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>";

?>

展开全部

  • 写回答

1条回答 默认 最新

  • doushifen4060 2014-12-07 12:03
    关注

    Looking at the output like ID=99>Info, It appears that what you're seeing is actually XML itself mis-rendered as HTML by your browser, as in <Match ID=99>Info. If you look at the browser's page source, you'll probably find much of your information there, but not organized as intended.

    Let's backup a little and iterate from the root node. Instead of using children(), iterate on the node names themselves. That is, after all, what makes SimpleXML easier to use than other XML APIs in PHP.

    $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/>";
      }
    }
    

    Internally to the loop, you may reformat these to get them in the output you want (removing <br/> for example, or moving the Name node to print before the inner loop).

    Here's a demonstration: http://codepad.viper-7.com/gCoc9g

    And another one modified to use your real XML. Note that the node names are case-sensitive. So <Watchlist> is not the same as <WatchList>!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部