且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 PHP 从 XML 中的节点内部获取数据

更新时间:2023-11-24 20:39:34

假设你的 Event 节点有一个父节点(即 ......</Event></Events>):

$xml = simplexml_load_file($file_xml);foreach($xml->Event as $event) {$idAttr = 'ID';$event_id = (string) $event->attributes()->$idAttr;$venue_id = (string) $event->Venue->attributes()->$idAttr;$venue_img_2 = (string) $event->Venue->Image->Url;}

So im not sure what or how to really describe what i need but hopefully someone will understand.

single element of the total xml file looks like this:

for ( $counter = 1; $counter load($file_xml); //make sure path is correct
  $note = $objDOM->getElementsByTagName("Event");

  // Loop through XML feed
  $i = 0;
  foreach( $note as $value )
  {
    $event_id       = $value->getAttribute('ID');
    # venue id
    $VenueID        = $value->getElementsByTagName("Venue");
    $venue_id       = $VenueID->item(0)->getAttribute("ID");
  }
}

arrgggg its not really copying he code exactly. geez.

the top is the iteration of the for loop. inside the foreach statement I have the insert statement; which is where I am trying to insert the image URL

<Event ID="IDIDIDIDID">
   <EventName>EVENT NAME</EventName>
   <Artist>
      <ArtistName>ARTIST NAME</ArtistName>
   </Artist>

   <Venue ID="IDIDIDID">
    <VenueName>VENUE NAME</VenueName>
       <Image>
           <Url>IMAGE_URL</Url>
           <Width>205</Width>
           <Height>115</Height>
        </Image>
      </Venue>
 </Event>

I am able to get the rest of the imageofmation I need, except for the image URL. i have tried to break it up as an array and get the first value but that didnt work. I saw another post here and tried to use preg_match but that didnt work. does anyone know how i could:

get the URL of the image inside the individual "Event" row?

this is the beginning of what I have:

$Image          = $value->getElementsByTagName("Image");
$venue_img_2    = $Image->item(1)->nodeValue;

this will return IMAGE_URL 205 115

Note: the "space" between the IMAGE_URL and 205 and 115 doesn't seem to actually be a "space"

thanks in advance.

Assuming your Event nodes have one parent (ie <Events><Event>...</Event><Event>...</Event></Events>):

$xml = simplexml_load_file($file_xml);

foreach($xml->Event as $event) {
    $idAttr = 'ID';

    $event_id = (string) $event->attributes()->$idAttr;
    $venue_id = (string) $event->Venue->attributes()->$idAttr;

    $venue_img_2 = (string) $event->Venue->Image->Url;
}