如何在php中获取此XML的值和属性?

时间:2018-07-10 13:45:18

标签: php xml

我有这样的php代码:

$xml = @simplexml_load_file('2.xml', 'SimpleXMLElement',LIBXML_NOCDATA); 

print_r($xml);

如何获取以下值: DialingNumber,StartTime,AnswerTime吗?

foreach ($xml as $show)
  {
  echo (string)$show['DialedNumber'];
  echo (string)$show['AnswerNumber'];
  echo (string)$show['WaitDuration'];
  }

不工作!如何获取以下值:DialingNumber,StartTime,AnswerTime?

2 个答案:

答案 0 :(得分:1)

XML文件本身存在问题,只需将某些实体替换为一些虚拟数据即可“纠正”。第二部分是引用要输出的数据的正确路径。

$filename = '2.xml';
$data = file_get_contents($filename);
$data = str_replace(["&rs", "&rc"], "", $data);    // Remove entity references
$xml = simplexml_load_string($data);

foreach ($xml->Tablix1->DialedNumber_Collection->DialedNumber->Details_Collection->Details
        as $details)
{
    echo (string)$details['DialedNumbers'].PHP_EOL;
    echo (string)$details['AnswerNumber'].PHP_EOL;
    echo (string)$details['WaitDuration'].PHP_EOL;
}

答案 1 :(得分:0)

您可以尝试以下方法吗?

foreach ($xml as $show)
  {
  echo (string)$show[0]['DialedNumber'];
  echo (string)$show[0]['AnswerNumber'];
  echo (string)$show[0]['WaitDuration'];
  }