0

値を持つxmlフィールド$xmlstringがあります。

'<result>
  <customerAccount xmlns="http://www.ibm.com>
    <AccountStatus xmlns="">Due</AccountStatus>

    <ComponentCustomerAccount xmlns="">
      <Name>ADSL</Name>
      <CharacteristicValue>
        <Characteristic>
          <Name>Balance</Name>
        </Characteristic>
        <Value>0.0</Value>
      </CharacteristicValue>
      <CharacteristicValue>
        <Characteristic>
          <Name>Date</Name>
        </Characteristic>
        <Value>21.10.1990</Value>
      </CharacteristicValue>
      <AccountStatus>Active</AccountStatus>
    </ComponentCustomerAccount>

    <ComponentCustomerAccount xmlns="">
      <Name>IPTV</Name>
      <CharacteristicValue>
        <Characteristic>
          <Name>Balance</Name>
        </Characteristic>
        <Value>100</Value>
      </CharacteristicValue>
      <CharacteristicValue>
        <Characteristic>
          <Name>Date</Name>
        </Characteristic>
        <Value>21.10.1990</Value>
      </CharacteristicValue>
      <AccountStatus>Active</AccountStatus>
    </ComponentCustomerAccount>


  </customerAccount>
</result>';

テーブル内の HTML フィールドに表示したい:

NAME       STATUS     VALUE

ADSL       ACTIVE     0.0
IPTV       ACTIVE     100

ご覧のとおり、繰り返している要素がいくつかあります。ComponentCustomerAccount は繰り返され、複数の要素を持つことができます。最初の AccountStatus 要素は必要ありませんが、ComponentCustomerAccount 内に AccountStatus 要素が必要です。また、ComponentCustomerAccount 要素内の最初の要素 Name が必要です。

    $xml = new SimpleXMLElement($xmlString);

    $html = "<table>";
    $html .= "<tr><th>Account Name</th><th>Status</th><th>Amount</th></tr>";
    foreach($xml->customerAccount as $cust) {

     $html .= "<tr><th>" . $cust->Name .
 "</th><th>" . $cust->AccountStatus. "</th><th>" . $cust->Value . "</th></tr>";


    }

    $html .= "</table>";

しかし、空白の出力が得られます。何を変更する必要がありますか? ありがとうございました

4

1 に答える 1

2

XMLに引用符がありません<customerAccount xmlns="http://www.ibm.com>。これを修正したらComponentCustomerAccount、ループの内側を繰り返す必要があります。

foreach($xml->customerAccount as $ca) {
    foreach ($ca->ComponentCustomerAccount as $cust) {
        $html .= "<tr><th>" . $cust->Name
        . "</th><th>" . $cust->AccountStatus. "</th><th>"
        . $cust->CharacteristicValue->Value . "</th></tr>";
    }
}
于 2013-03-18T13:35:34.407 に答える