How do you ignore node attribute name when Parsing XML?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Decai
Forum Newbie
Posts: 1
Joined: Tue Apr 22, 2008 3:22 am

How do you ignore node attribute name when Parsing XML?

Post by Decai »

Hi guys, I hope you can help me with a problem I have been having, I know its a bit of a newbie question but its been driving me mad! I am getting an XML product feed and trying to parse it to display the product information onto my site however I am having a great deal of problems with one of the nodes which has an attribute name. This is what it looks like:

<price-with-shipping-max currency="GBP">229.00</price-with-shipping-max>

my parser looks like this:

$this->base_url .= $this->search_url ."/cgi-bin/prfeed.cgi?"
."aid=$this->aid"
."&ip=$this->ip"
."&limit=$this->limit"
."&products=$this->prodid"
;

$this->xml_response = @join ("\n", @file($this->base_url));

$i = 0;
foreach ( explode("</retailer>", $this->xml_response ) as $line) {

$this->listings[$i][redirect_url]
= $this->parse_el("link", $line);
$this->listings[$i][title]
= $this->parse_el("name", $line);
$this->listings[$i][pricewithshippingmin]
= $this->parse_el('price-with-shipping-min', $line);
$this->listings[$i][pricewithshippingmax]
= $this->parse_el('price-with-shipping-max', $line);
$i++;
}
array_pop($this->listings);

if (!$this->meta[tot_count]) {
$this->meta[tot_count] = $i-1;
}

}

function parse_el($el, $str) {
if ( preg_match("/<$el>(.*)<\/$el>/i", $str, $match) ) {
$match[1] = str_replace("<![CDATA[", "", $match[1]);
$match[1] = str_replace("]]>", "", $match[1]);
return trim($match[1]);
}
}


There is a bit more to it but thats where I am trying to parse the XML. If you could help me with this I would be very greatful.

Thanks in advance!
Post Reply