# XML Parsing using PHP {Intermediate}

**URL:** <https://forum.kirupa.com/t/xml-parsing-using-php-intermediate/293236>\
**Category:** Uncategorized\
**Created:** [July 25, 2009, 3:09pm UTC](https://forum.kirupa.com/t/xml-parsing-using-php-intermediate/293236 "2009-07-25T15:09:38Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![whee1206](https://avatars.discourse-cdn.com/v4/letter/w/dec6dc/32.png) [@whee1206](https://forum.kirupa.com/u/whee1206)\
**Post date:** [July 25, 2009, 3:09pm UTC](https://forum.kirupa.com/t/xml-parsing-using-php-intermediate/293236/1 "2009-07-25T15:09:38Z")

</div>

This will probably be an easy fix for someone who is familiar with XML. I have gone through the tutorial from this site (XML Parsing using PHP intermediate). Everything is working except for one problem. Every time I refresh my script in my browser, I get a different amount of results. Most times the script only parses 4 results form the xml file but sometimes it parses all 11 results like it should.

Here is what my code looks like:

#################################  
?php

$xml\_file = “[http://api.votesmart.org/Candidates.getByOfficeState?key=72b64124ada95f14e3581b010e133d9b&officeId=5&stateId=UT&electionYear=2008](http://api.votesmart.org/Candidates.getByOfficeState?key=72b64124ada95f14e3581b010e133d9b&officeId=5&stateId=UT&electionYear=2008)”;

$xml\_id\_key = “_CANDIDATELIST_CANDIDATE_CANDIDATEID";  
$xml\_ln\_key = "CANDIDATELISTCANDIDATE_LASTNAME”;

$story\_array = array();

$counter = 0;  
class xml\_story{  
var $vs\_id, $vs\_ln;  
}

function startTag($parser, $data){  
global $current\_tag;  
$current\_tag .= “\*$data”;  
}

function endTag($parser, $data){  
global $current\_tag;  
$tag\_key = strrpos($current\_tag, ‘\*’);  
$current\_tag = substr($current\_tag, 0, $tag\_key);  
}

function contents($parser, $data){  
global $current\_tag, $xml\_id\_key, $xml\_ln\_key, $counter, $story\_array;  
switch($current\_tag){  
case $xml\_id\_key:  
//$story\_array[$counter] = new xml\_story();  
$story\_array[$counter]-\>vs\_id = $data;  
break;  
case $xml\_ln\_key:  
$story\_array[$counter]-\>vs\_ln = $data;  
$counter++;  
break;

```
}

```

}

$xml\_parser = xml\_parser\_create();

xml\_set\_element\_handler($xml\_parser, “startTag”, “endTag”);

xml\_set\_character\_data\_handler($xml\_parser, “contents”);

$fp = fopen($xml\_file, “r”) or die(“Could not open file”);

$size = filesize($xml\_file);  
$data = fread($fp, 8000) or die(“Could not read file”);

if(!(xml\_parse($xml\_parser, $data, feof($fp)))){  
die("Error on line " . xml\_get\_current\_line\_number($xml\_parser));  
}

xml\_parser\_free($xml\_parser);

fclose($fp);

?\>

\<html\>  
\<head\>  
\<title\>CNT HEADLINE NEWS\</title\>  
\</head\>  
\<body bgcolor="#FFFFFF"\>  
\<?php  
for($x=0;$x\<count($story\_array);$x++){  
echo " \<h2\>" . $story\_array[$x]-\>vs\_id . "\</h2\>  
";  
echo $story\_array[$x]-\>vs\_ln . "  
";  
}

echo “\<br\>\<br\>”;

print\_r($story\_array);  
echo “\<br\>\<br\>”;  
var\_dump($story\_array);  
?\>

\</body\>  
\</html\>

################################
