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
<products>
<product>
<heading>01elf%2Ejpg</heading>
<text><![CDATA[]]></text>
<image>01elf.jpg</image>
<price>0.00</price>
</product>
</products>
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":
if($data==$theImage){
/************************************************** *
this works - how do I update and save the xml data????
************************************************** **/
echo 'found match=' . $data . '<br>';
}
break;
}
}
}
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
for($i=0;$i<count($theImages);$i++){
if($theImages[$i]!=""){
$insideProduct = false;
$tag='';
$theImage=$theImages[$i];
// 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",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
// Free up memory used by the XML parser
xml_parser_free($xml_parser);
}
}
}
Any thoughts?
Thanks in advance
I haven't done much parsing of xml in php and I'm stuck :)
I have some xml that looks like this
<products>
<product>
<heading>01elf%2Ejpg</heading>
<text><![CDATA[]]></text>
<image>01elf.jpg</image>
<price>0.00</price>
</product>
</products>
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":
if($data==$theImage){
/************************************************** *
this works - how do I update and save the xml data????
************************************************** **/
echo 'found match=' . $data . '<br>';
}
break;
}
}
}
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
for($i=0;$i<count($theImages);$i++){
if($theImages[$i]!=""){
$insideProduct = false;
$tag='';
$theImage=$theImages[$i];
// 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",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
// Free up memory used by the XML parser
xml_parser_free($xml_parser);
}
}
}
Any thoughts?
Thanks in advance