Jump to content

echoing the correct XML tag data


ballhogjoni

Recommended Posts

Hi guys/gals

 

I am having trouble ECHOing specific data and for some reason the links for each sub parent are combined into one link...Let me explain that. The first link is correct and outputs http://example.com/1, but the second link outputs http://example.com/1http://example.com/2

 

Does anyone know why that is? The other problem I have is that I have about 200 sub parents in my xml file and the sub parents represent different categories, for example:

<subparent>
  <category>Master</category>
</subparent>
<subparent>
  <category>Master</category>
</subparent>
<subparent>
  <category>Visa</category>
</subparent>
<subparent>
  <category>Visa</category>
</subparent>

 

How can I get the code below to display the Master category only?

 

<?php
$insidecard = false; 
$tag = ""; 
$creditcard = ""; 
$category = ""; 
$cardtype = "";
$link = "";

function startElement($parser, $tagName, $attrs) { 
global $insidecard, $tag; 
   	if ($insidecard) {
   		$tag = $tagName;
} elseif ($tagName == "CARD") { 
       $insidecard = true; 
   } 
}
function characterData($parser, $data) { 
global $insidecard, $tag, $creditcard, $category, $cardtype, $link;
if ($insidecard) {
	switch ($tag) { 
           case "CREDITCARD": 
           $creditcard .= $data; 
           break; 
           case "CATEGORY": 
           $category .= $data; 
           break; 
           case "CARDTYPE": 
           $cardtype .= $data; 
           break;
	   case "LINK": 
           $link .= $data; 
           break; 
       }
   } 
}
function endElement($parser, $tagName) { 
   	global $insidecard,$tag,$creditcard,$category,$cardtype,$link; 
   	if ($tagName == "CARD") {
	echo "<p><b><a href=\"".strip_tags($link)."\">".strip_tags($creditcard)."</a></b></p>";
	$creditcard = ""; 
       	$category = ""; 
       	$cardtype = "";
   	$insidecard = 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"); 

// Open the XML file for reading 
$fp = fopen("http://www.xxxxxxxxxxxx.com/partners/access/xml/xml.asp?uid=xxxxxxxxxxx","r") 
       or die("Error reading RSS data."); 

// Read the XML file 4KB at a time 
while ($data = fread($fp, 4096)) 
   // Parse each 4KB chunk with the XML parser created above 
   xml_parse($xml_parser, $data, feof($fp)) 
       // 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))); 

// Close the XML file 
fclose($fp); 

// Free up memory used by the XML parser 
xml_parser_free($xml_parser);


?>

Link to comment
https://forums.phpfreaks.com/topic/72241-echoing-the-correct-xml-tag-data/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.