Jump to content

xml parser help!


becu

Recommended Posts

Let say I have this xml file:

 

<Url>
http://www.pagesoftheheart.net/gallery/data/500/3660Love_each_other.jpg
</Url>
<Thumbnail>
<Url>http://mud.mm-a2.yimg.com/image/2218795354</Url>
<Thumbnail>

 

all I need is get and display the info inside <Url> element inside Thumbnail.

 

What should I do? because the code I wrote, everytime, I encounter an opening tag <URL>, it will put the data in between the tags into the variable.

 

thanks.

Link to comment
Share on other sites

<?php
$file = "http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=.hLnSDPV34ETvZalCRwGE5j7.4C8rh7u0bHBMN_wsC3LZAJC8pXBLn2ZnjvWDzaF0w--&query=love&results=50&start=1";

$insideitem = false;
$url = "";
$thumb = "";
$col = 0;

function startElement($parser, $name, $attrs)
{
    global $insideitem, $url, $thumb, $col;
    if ($name == "URL")
        $insideitem = true;
    else $insideitem = false;
}

function characterData($parser, $data)
{
global $insideitem, $tag, $url, $thumb, $col;
if ($insideitem)
{
	$m = $col % 2;
	switch ($m) {
	   case "0":
	   $url = $data;
	   break;
	   case "1":
	   $thumb = $data;
	   break;
	}
   }
}

function endElement($parser, $name)
{
    global $insideitem, $url, $thumb, $col;
   	if ($name == "URL") {
   	   	$re = $col % 2;
   		if ($re == "0")
   		{
		printf("{$open}<td onMouseOver=this.style.backgroundColor='{$bg}'; onMouseOut=this.style.backgroundColor=''><div align=\"center\"><a href='%s'><img src='%s' width='120'></a></div></td>{$close}", $url, $thumb);
                }
       }
        $col++;
}
?>

 

As you can see above, I try to get rid of one of the url, but the result turns out an image of one thumbnail having the url of the next image. And I bet this is not the best possible solution. There gotta be a way to get just the URL inside THUMBNAIL.

 

thanks.

Link to comment
Share on other sites

my code, every time it sees an opening "<Url>" tag, it will get the data in between those tags. In this case, there are 2 <Url> tags in one xml file and I only need to get data in between <Url> </Url> tags in <Thumbnail>. Is there a way to know that we're in the <Url> tag of Thumbnail not the other <Url> tag

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.