View Full Version : XML in PHP

Billy T
07-09-2005, 08:06 AM
Hi all

I haven't done much parsing of xml in php and I'm stuck :)

I have some xml that looks like this




I need to parse this in php, search for a match with the image name and when I find a match, remove the whole <product> node and save the data back to the db where it came from.

I have everything working except for the removal of the node and saving

here's my code

function startElement($parser, $tagName, $attrs) {
global $insideProduct, $tag;
if ($insideProduct) {
$tag = $tagName;
} elseif ($tagName == "PRODUCT") {
$insideProduct = true;
function characterData($parser, $data) {
global $insideProduct, $tag, $theImage;
if ($insideProduct) {
switch ($tag) {
case "IMAGE":
/************************************************** *
this works - how do I update and save the xml data????
************************************************** **/
echo 'found match=' . $data . '<br>';
function endElement($parser, $tagName) {
global $insideProduct, $tag;
if ($tagName == "PRODUCT") {
$insideProduct = false;
//loop through all galleries
while($gallery = mysql_fetch_array($result)) {
$id = $gallery['galleryID'];
$galleryData = $gallery['galleryData'];
//loop through all images
$insideProduct = false;
// Create an XML parser
$xml_parser = xml_parser_create();

// Set the functions to handle opening and closing tags
xml_set_element_handler($xml_parser, "startElement", "endElement");

// Set the function to handle blocks of character data
xml_set_character_data_handler($xml_parser, "characterData");

// Parse each 4KB chunk with the XML parser created above
xml_parse($xml_parser, $galleryData, true)
// Handle errors in parsing
or die(sprintf("XML error: %s at line %d",
// Free up memory used by the XML parser


Any thoughts?

Thanks in advance

Billy T
07-10-2005, 08:27 AM
never mind

got it going with this - http://www.cs.cityu.edu.hk/~martin/domit/documentation/DOMIT_Document.html