使用PHP从XML文档中的属性中提取信息

时间:2014-03-20 13:51:24

标签: php xml xpath simplexml xml-attribute

在浏览了一些有前途的标题后,我无法找到我的问题的答案,所以我想我会试试运气!

我正在尝试从我们在工作中使用的后端生成的XML文件中提取信息。信息的格式不像普通的XML文档,因此提取我需要的东西有点困难。我希望我最终可以将这些信息放在一张表中,但如果我能至少提取这些值,我会很高兴。

示例字符串如下:

<Event Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"/>

我正试图:

Event Name:
Venue Name:
Event Date:
Direct Link:

我假设我需要使用substr,但我从未处理过这样的字符串。

如果你花点时间看一下,我会非常感激!

4 个答案:

答案 0 :(得分:3)

simplexml_load_string()可以轻松地使用XML来完成这样的基本任务:

<?php
$event= simplexml_load_string('<Event Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"/>');
?>

Event Name: <?= $event['Name']; ?><br>
Venue Name: <?= $event['VenueName']; ?><br>
Event Date: <?= $event['EventDate']; ?><br>
Direct Link: <?= $event['DirectLink']; ?>

See it in action

答案 1 :(得分:1)

或尝试使用正则表达式:

$str = '<Event Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"/>';

preg_match('/Name="([^"]*)".+VenueName="([^"]*)".+EventDate="([^"]*)".+DirectLink="([^"]*)"/',$str,$output);

echo '<pre>';

print_r($output);

输出:

Array
(
    [0] => Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"
    [1] => Last Reef
    [2] => Theater
    [3] => 2014-03-21 15:00:00
    [4] => https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259
)

答案 2 :(得分:1)

下面是我提出的一个简单示例,它使用XPath返回所有匹配的Event元素,并将数据作为HTML表格(view output here)输出,并使用属性名称作为列标题。我添加了一个额外的Event元素来演示迭代多个元素(并将Event元素包装在Events元素中,因为有效的XML文档必须只有一个根元素。)

<?php

function FormatData($data, $linkText = 'Link') {
  if (substr($data, 0, 4) === 'http') {
    if ($linkText === '') $linkText = $data;
    return sprintf('<a href="%s">%s</a>', $data, $linkText);        
  } else {
    return $data;
  }
}    

$xmlData = simplexml_load_string('<Events><Event Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"/><Event Name="Hidden Universe" VenueName="Theater" EventDate="2014-03-22 13:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45695"/></Events>');

// do a global search to retrieve all "Event" elements
$events = $xmlData->xpath('//Event');

if (count($events) > 0) {

  // get attribute names of first "Event" element
  foreach($events[0]->attributes() as $attribute) {
    $attributeNames[] = $attribute->getName(); 
  }
  // output as table column headings
  echo '<table border="1"><tr><th>' . implode('</th><th>', $attributeNames) . '</th></tr>';

  // iterate through each "Event" element and access each attribute value
  foreach($events as $event) {
    echo '<tr>';
    foreach($attributeNames as $attributeName) {
      echo '<td>' . FormatData($event[$attributeName]) . '</td>';
    }
    echo '</tr>';   
  } 
  echo '</table>';

} else {
  echo 'No matching elements found';
}

?>

答案 3 :(得分:0)

我认为,SimpleXml可以做到这一点。只需添加以下内容:

 $xml_string = '<Event Name="Last Reef" VenueName="Theater" EventDate="2014-03-21 15:00:00" DirectLink="https://tickets.ctsciencecenter.org/Public/loader.asp?target=hall.asp?event=45259"/>';  
 $xml        = simplexml_load_string('<?xml version="1.0"?> '.$xml_string);
 $attributes =$xml->attributes();
 echo 'Event Name:'.$attributes['Name'];
 echo 'Venue Name:'.$attributes['VenueName'];
 echo 'Event Date:'.$attributes['EventDate'];
 echo 'Direct Link:'.$attributes['DirectLink'];