Jump to content

Use form to parse varying link XML? [Drupal-based site]


Ivaked

Recommended Posts

http://myhawk.org/action/action.html?handle=*****&key=yallobzff7

 

The above link is my vital part when I seem to be struggling. Basically, I'm using the Drupal CMS for my website (www.warhawkinfo.com) and have recently acquired an XML feed for which I can parse the XML data and display the results on my site but I'm dumbfounded with PHP - it's something I've never tried to use before, and I've been stuck since the beginning.

 

In the link you will see "*****". This variable can be any username which is associated with the game this XML feed is for. The value of which is to be defined using a form on my site e.g. if I put "Pot-Nut" into the form and click search the link would be "http://myhawk.org/action/action.html?handle=Pot-Nut&key=yallobzff7" which would then be parsed and the data returned to the user.

 

The question is, how do I do this? I've tried a couple of methods with help from some friends on other forums but I have still not managed to get this working on my site. I'll be honest... I'm BEGGING for help with this as it's one of the most vital parts of my website which I need and any help anyone can give would not be thanked enough.

 

Also, it will be my starting point for learning PHP!

 

Very many thanks to any help in advance, it will be greatly appreciated.

 

~Liam

Link to comment
Share on other sites

  • 2 weeks later...

I hope i'm not too late to help you! I had a real problem with XML parsing myself, but i think i've got everything you need to get the data out of the XML in this script:

 


function  unhtmlspecialchars( $string )
    {
$string=preg_replace("/(&#)([0-9]*)(;)/esiU","chr(intval('\\2'))",$string);
        $string = str_replace ( '&', '&', $string );
        $string = str_replace ( ''', '\'', $string );
        $string = str_replace ( '"', '\"', $string );
        $string = str_replace ( '<', '<', $string );
        $string = str_replace ( '>', '>', $string );
        return $string;
    }

//'" Highlighting fix

function checkamend($xmlitem , $count , $data){
//Deals with the parser breaking up a tag into multiple items as XML_parse breaks with special characters
		if (isset($xmlitem[$count])){

		$xmlitem[$count].=$data;

		} elseif (!isset($xmlitem[$count])){

		$xmlitem[$count]=$data;

	}

return $xmlitem[$count];
}

function startElement($parser, $name, $attrs) {

global $curTag;
//track the tag we are currently in
$curTag .= "^$name";

}

// XML Parser element end function
function endElement($parser, $name) {

global $curTag;

// remove the tag we are ending from the "tag tracker"
$caret_pos = strrpos($curTag,'^');

$curTag = substr($curTag,0,$caret_pos);

}


// get the xml information first

global $curTag, $itemCount;

global $item, $item2;

/* Assuming the XML looks like:

<Element>
   <Element1>Info</Element1>
   <Element1>Info</Element2>
</Element>

Then the route is:*/

$root = "^ELEMENT^";

  $item=$root."ELEMENT1"; //must be in capitals to work, no idea why
  $item2=$root."ELEMENT2";

if ($curTag == $item) {

	$item[$itemCount]=checkamend ($item , $itemCount , $data);

} elseif ($curTag == $itemCountryKey) {

	$item2[$itemCount]=checkamend ($item2, $itemCount , $data);

	$itemCount++; //make sure this is in the last elseif
}


}
/*For each piece of data you want to pull out you have to go to the correct element and add an extra elseif statement.

I had to use a seperate array for each because i was losing data and it was not being transferred properly

Create the parser and parse the file*/
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

$uFile = "URL to parse here";

if (!($fp = fopen($uFile,"r"))) {
die ("could not open file for Input");
}
// Read the XML file 4KB at a time

while ($data = fread($fp, 4096)) {

	 // Parse each 4KB chunk with the XML parser created above
	if (!xml_parse($xml_parser, $data, feof($fp))) {
		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);

// To output the data all you need to do is go through the arrays of items with a "for" loop

for ($i=0;$i<$itemCount;$i++) {

    echo $item[$i];
    echo $item2[$i];
}

 

I hope this helps/works, i've editted it down a bit so it would be shorter to copy and paste. If you can give me the structure of the XML document I will be able to help more :)

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.