如何使用PHP通过其属性的值显示XML元素

时间:2014-10-03 21:32:41

标签: php xml xpath simplexml

我正在尝试为我拥有的事件Feed运行一些报告。我正在使用我的事件管理器提供的Web服务来使用PHP创建XML文件。这很有效。这是我得到的XML:

<?xml version="1.0" encoding="UTF-8"?>
<ArrayOfTransactionInformation>
  <Transaction Transaction_Id="3126" IsSuccess="true" IsReviewed="true">
    <Column Name="instance_id" Value="65104108-6355-49C2-ACA7-8D5A28C962BD" />
    <Column Name="amount" Value="700.0000" />
    <Column Name="purchasedate" Value="2014-09-22T17:00:13" />
    <Column Name="authorizationcode" Value="Skipped billing - not applicable" />
    <Column Name="custtransnum" Value="Skipped billing - not applicable" />
    <Column Name="retrievalcode" Value="Skipped billing - not applicable" />
    <Column Name="appealcode" Value="" />
    <Column Name="transactionlastupdated" Value="2014-09-22T17:00:13" />
    <Column Name="billingname" Value="John Test" />
    <Column Name="creditcardtype" Value="N/A" />
    <Column Name="lastfourdigits" Value="1111" />
    <Column Name="achbankname" Value="" />
    <Column Name="achbankstate" Value="" />
    <Column Name="achaccountnumlast2" Value="" />
    <Column Name="achroutingnumlast2" Value="" />
    <Column Name="achchecknum" Value="" />
    <Column Name="achaccounttype" Value="" />
    <Column Name="billingemail" Value="john@test.com" />
    <Column Name="billingphone" Value="" />
    <Column Name="billingstreet1" Value="Skipped billing - not applicable" />
    <Column Name="billingstreet2" Value="Skipped billing - not applicable" />
    <Column Name="billingcity" Value="Skipped billing - not applicable" />
    <Column Name="billingstate" Value="N/A" />
    <Column Name="billingzip" Value="N/A" />
    <Column Name="billingcountry" Value="Skipped billing - not applicable" />
    <Column Name="paymenttype" Value="OT" />
    <Column Name="paymentfrequency" Value="" />
    <Column Name="totalscheduledamount" Value="" />
    <Column Name="nextpaymentamount" Value="" />
    <Column Name="nextpaymentdate" Value="" />
    <Column Name="paymentsremaining" Value="" />
    <Column Name="amounttodate" Value="" />
    <Column Name="firstpaymentdate" Value="" />
    <Column Name="initialtransactionid" Value="0" />
    <Column Name="fromfacebook" Value="False" />
    <Column Name="communityid" Value="2" />
    <Column Name="agentid" Value="" />
    <Column Name="agentname" Value="" />
    <Column Name="merchantaccountname" Value="Cyber Source" />
    <Column Name="control_id" Value="2055" />
    <Column Name="control_type" Value="event" />
    <Column Name="control_name" Value="Bowl Game Registration Template" />
    <Column Name="constituent_id" Value="" />
    <Column Name="imod_member_id" Value="280873" />
    <TransactionItems>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10355">
        <Column Name="sku" Value="bus_h2_early_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10356">
        <Column Name="sku" Value="bus_h3_early_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10357">
        <Column Name="sku" Value="bus_h4_early_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10358">
        <Column Name="sku" Value="party_ind_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10359">
        <Column Name="sku" Value="party_table_type_1_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10360">
        <Column Name="sku" Value="tailgate_ind_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10349">
        <Column Name="sku" Value="hotel_4_fn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10350">
        <Column Name="sku" Value="hotel_4_sn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10351">
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10352">
        <Column Name="total" Value="400.0000" />
        <Column Name="quantity" Value="1" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10353">
        <Column Name="total" Value="300.0000" />
        <Column Name="quantity" Value="1" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10354">
        <Column Name="sku" Value="bus_h1_early_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10343">
        <Column Name="sku" Value="hotel_2_fn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10344">
        <Column Name="sku" Value="hotel_2_sn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10345">
        <Column Name="sku" Value="hotel_2_tn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10346">
        <Column Name="sku" Value="hotel_3_fn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10347">
        <Column Name="sku" Value="hotel_3_sn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10348">
        <Column Name="sku" Value="hotel_3_tn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10340">
        <Column Name="sku" Value="hotel_1_fn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10341">
        <Column Name="sku" Value="hotel_1_sn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
      <TransactionItem Imod_Member_Id="280873" Type="CommerceItem" Transaction_Item_Id="Item-10342">
        <Column Name="sku" Value="hotel_1_tn_king_paid" />
        <Column Name="total" Value="0.0000" />
        <Column Name="quantity" Value="0" />
        <Column Name="fairmarketvalue" Value="0.0000" />
      </TransactionItem>
    </TransactionItems>
    <LinkedTransaction>
      <TransactionId>0</TransactionId>
      <Amount>0</Amount>
      <ControlId>0</ControlId>
      <ControlType />
      <ControlName />
      <PaymentType />
      <TotalScheduledAmount>0</TotalScheduledAmount>
    </LinkedTransaction>
  </Transaction>
</ArrayOfTransactionInformation>

它们似乎将每个字段输出到具有相同标记名称的XML。我需要能够拉出每个单独的元素,我需要能够为整个文件执行此操作,例如,当有多个条目时,我需要能够拉出并显示条目的所有名称,这将是是billingname

我可以从中提取元素,但我无法弄清楚如何根据属性值拉取元素。我一直在尝试使用xpath方法和其他几种变体。

这是我拥有的PHP,它将成功地提取<Transaction>元素中的所有元素:

$xml=simplexml_load_file("reporting.xml");
echo $xml->getName() . "<br>";
echo "<table width=700>";

for ($i = 0; ; $i++){
  foreach($xml->Transaction->Column[$i]->attributes() as $a => $b) {
    echo "<tr><td>";
    echo $a,'="',$b,"\"\n";
    echo "</td></tr>";
  }
}

echo "</table>";

有关如何使用PHP使用属性值显示数据的任何想法?或者为了更好地解释,我需要做一个foreach(我认为)来查找billingname的所有实例,然后回显它的值,例如“John Test”。

3 个答案:

答案 0 :(得分:2)

您可以尝试使用XPath选择XML文档的特定部分。在simplexml中,它看起来像这样:

$result = $xml->xpath('//Column[@Name="billingname"]');
foreach($result as $column){
    echo $column['Value'];
}

在XPath之上选择<Column>属性等于name的所有"billingname"

我不是PHP专家,只是引用了手册文档:SimpleXMLElement::xpathBasic SimpleXML usage

答案 1 :(得分:1)

干杯,这是完全有效的例子,有两种不同的方式(https://github.com/Waldz/Examples/tree/master/xml-parsing)。

使用XPath:

$doc = new DOMDocument();
$doc->load('reporting.xml');

$xpath = new DOMXPath($doc);

// If you want to read all Column tags
$columnAttributes = $xpath->query("//ArrayOfTransactionInformation/Transaction/Column");
foreach ($columnAttributes as $columnTag) {
    /** @var \DOMElement $columnTag */
    echo sprintf(
        "%s = %s\n",
        $columnTag->getAttribute('Name'),
        $columnTag->getAttribute('Value')
    );
}

// If you want to read exact attribute
$billingNameAttribute = $xpath->query("//ArrayOfTransactionInformation/Transaction/Column[@Name='billingname']/@Value");
if ($billingNameAttribute->length > 0) {
    echo $billingNameAttribute->item(0)->value;
}

使用SimpleXML:

$xml = simplexml_load_file("reporting.xml");

foreach ($xml->Transaction->Column as $column) {
    echo sprintf(
        "%s = %s\n",
        $column->attributes()->Name,
        $column->attributes()->Value
    );
}

答案 2 :(得分:0)

我使用a solution代替DOM编写了simplexml,因为我发现DOM更容易使用。它的核心是:

foreach($dom->getElementsByTagName('Column') as $column) {
  echo '<tr><td>';
  $name = $column->attributes->getNamedItem('Name')->textContent;
  $value = $column->attributes->getNamedItem('Value')->textContent;
  if ($name === 'billingname') {
      echo "{$name} = {$value}";
  }
  echo '</td></tr>';
}