Getting Data from inside a node in XMl with PHP

2020-03-30 04:18发布

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.

2条回答
2楼-- · 2020-03-30 04:47

try this

$xml = simplexml_load_file("books.xml") 
   or die("Error: Cannot create object");

  foreach($xml->children() as $books){
foreach($books->children() as $book => $data){
  echo $data->id;
  echo $data->title;
  echo $data->author;
  echo "<br />";
}

}

查看更多
我想做一个坏孩纸
3楼-- · 2020-03-30 04:59

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;
}
查看更多
登录 后发表回答