Question

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. The information is not formatted like a normal XML document, so it's been a little difficult to extract what I need. 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.

If you take a moment to look at this, I would greatly appreciate it!

Was it helpful?

Solution

simplexml_load_string() makes working with XML easy for basic tasks like this:

<?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

OTHER TIPS

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. 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).

<?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. 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'];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top