1

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.

1
  • note that the item(1) is because artist also has an <image> piece in it. I just removed it for simplicity sake. Commented May 28, 2011 at 15:03

2 Answers 2

1

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

Would it make a difference if the $file_xml is returns from a URL (api) as XML? and the structure is: <results><Details><TotalResults>14214</TotalResults><TotalPages>143</TotalPages><CurrentPage>1</CurrentPage><ResultsPerPage>100</ResultsPerPage></Details><Event>ALL the info</Event></results>
I am also using this currently: $objDOM = new DOMDocument(); $objDOM->load($file_xml); $note = $objDOM->getElementsByTagName("Details");
@user450784: It should work as is with the xml being structured like that.
I just tried your code exactly with the $file_xml being the api url for the data that gets returned as xml, but i get some errors. 1. Entity: line 1: parser error : Start tag expected, '<' not found ---- 2. Warning: simplexml_load_string() [function.simplexml-load-string]: api.perfb.com/api/api.php?api_key=API_KEY&act in ---- 3. Warning: simplexml_load_string() [function.simplexml-load-string]: ^ in ---- 4. Notice: Trying to get property of non-object in ---- 5. Warning: Invalid argument supplied for foreach() in
@user450784: If you want to load the xml directly from a url its needs to be simplexml_load_file() instead. I've updated my answer to reflect this.
|
0

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

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.