简体   繁体   English

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

[英]Using PHP to extract information from attributes in XML document

I was unable to find an answer to my question after browsing some promising titles so I figured I would try my luck! 浏览一些有前途的标题后,我无法找到问题的答案,所以我认为我会尝试运气的!

I'm attempting to extract information from XML files generated from a back-end we use at work. 我正在尝试从工作中使用的后端生成的XML文件中提取信息。 The information is not formatted like a normal XML document, so it's been a little difficult to extract what I need. 信息的格式不像普通的XML文档,因此提取所需信息有点困难。 I'm hoping that I can eventually put this information in a table, but I'll be happy if I can at least extract the values. 我希望最终可以将这些信息放在表中,但是如果我至少可以提取这些值,我会很高兴。

An example string is as follows: 示例字符串如下:

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

And I'm trying to get: 我正在尝试获得:

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

I'm assuming I need to use substr, but I've never dealt with a string like this. 我假设我需要使用substr,但是我从未处理过这样的字符串。

If you take a moment to look at this, I would greatly appreciate it! 如果您花一点时间看一下,我将不胜感激!

simplexml_load_string() makes working with XML easy for basic tasks like this: 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 实际观看

Or try a regex: 或尝试使用正则表达式:

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

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
)

Below is a simple example I've come up with which uses XPath to return all matching Event elements and outputs the data as an HTML table ( view output here ) and uses the attribute names as column headings. 下面是我想到的一个简单示例,该示例使用XPath返回所有匹配的Event元素,并将数据输出为HTML表( 在此处查看输出 ),并将属性名称用作列标题。 I added in an extra Event element to demonstrate iterating over multiple elements (and wrapped the Event elements within an Events element as valid XML documents must have a single root element). 我在额外添加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';
}

?>

I think, SimpleXml can do this. 我认为,SimpleXml可以做到这一点。 Just add Some like this: 像这样添加一些:

 $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'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM