Jump to content

Counting XML nodes with php


docpepper

Recommended Posts

Hi,

 

I am trying to achieve something here with XML but am a bit stuck, I hope it is just a bit of misplaced code!

 

What I am wanting to do is have some projects in an xml file then echo them out using php ( Managed that successfully) But what I also want to do is get php to automatically count the projects and echo the ordered number next to each....for eg.

 

1. Project 1

2. Project 2

3. Project 3 ... and so on.

 

Code so far XML:

 

<?xml version="1.0" encoding="utf-8"?>
<webprojects>
<project>
<name>Project 1</name>
<date>Jan 2008</date>
<url>work/image1.jpg</url>
<id>P1</id>
<images>image1.jpg</images>
<description>Description Here
</description>
</project>
<project>
<name>Project 2</name>
<date>Jan 2008</date>
<url>work/image2.jpg</url>
<id>P2</id>
<images>image21.jpg</images>
<description>Description Here
</description>
</project><project>
<name>Project 3</name>
<date>Jan 2008</date>
<url>work/image3.jpg</url>
<id>P3</id>
<images>image3.jpg</images>
<description>Description Here
</description>
</project>
</webprojects>

 

PHP so Far:

 

<?php
if (! ($xmlparser = xml_parser_create()) )
{
die ("Cannot create parser");
}

$current = "";
function start_tag($parser, $name, $attribs) {
for($i=0; $i < count($name); $i++) 
global $current;
$current = $name;
if ($name == "WEBPROJECTS") { echo "<ul id=\"webprojects\">"; }
if ($name == "PROJECT") { echo "<li>
					<ul class=\"webnav\"><li class=\"left\">" . $i . "</li>"; }
if ($name == "NAME") { echo "<li class=\"name\"><h4>"; }
if ($name == "DATE") { echo "<li class=\"date\">"; }
if ($name == "URL") { echo "<p class=\"web_header\">
						<a href=\""; }
if ($name == "ID") { echo "<div class=\"web_imgs\" id=\""; }
if ($name == "IMAGES") { echo "<img src=\"work/"; }
if ($name == "DESCRIPTION") { echo "<p class=\"web_comment hide\">"; }
if ($name == "STRONG") { echo "<b>"; }
if ($name == "SPAN") { echo "<span>"; }
}
function end_tag($parser, $name) {
if ($name == "DESCRIPTION") { echo "</p>
<div class=\"web_footer\"><a class=\"btn_comment\" title=\" :: \"></a></div>
					</div>"; }
if ($name == "IMAGES") { echo "\" alt=\"\" width=\"500px\" height=\"300px\" /><br />
						</div>"; }
if ($name == "ID") { echo "\">"; }
if ($name == "URL") { echo "\" rel=\"lightbox\">View larger</a><img alt=\"\" src=\"images/spanner.gif\" />
						</p>"; }
if ($name == "DATE") { echo "</li>
						<li class=\"right\"><!--spanner--></li>
						</ul>
						<div class=\"web_content\">"; }
if ($name == "NAME") { echo "</h4></li>"; }
if ($name == "PROJECT") { echo "</li>"; }
if ($name == "WEBPROJECTS") { echo "</ul>"; }
if ($name == "STRONG") { echo "</b>"; }
if ($name == "SPAN") { echo "</span>"; }
}
function tag_contents($parser, $data) {
global $current;
if ($current == "WEBPROJECTS") { echo $data; }
if ($current == "PROJECT") { echo $data; }
if ($current == "NAME") { echo $data; }
if ($current == "DATE") { echo $data; }
if ($current == "URL") { echo $data; }
if ($current == "ID") { echo $data; }
if ($current == "IMAGES") { echo $data; }
if ($current == "DESCRIPTION") { echo $data; }
if ($current == "STRONG") { echo $data; }
if ($current == "SPAN") { echo $data; }
}

xml_set_element_handler($xmlparser, "start_tag", "end_tag");
?>

<?php
xml_set_character_data_handler($xmlparser, "tag_contents");

?>
<?php
$filename = "xml/data.xml";
if (!($fp = fopen($filename, "r"))) { die("cannot open ".$filename); 
}
?>
<?php 
while ($data = fread($fp, 4096)){
$data = eregi_replace(">"."[[:space:]]+"."< ",">< ",$data);
	if (!xml_parse($xmlparser, $data, feof($fp))) {
		$reason = xml_error_string(xml_get_error_code($xmlparser));
		$reason .= xml_get_current_line_number($xmlparser);
	die($reason);
	}
}
xml_parser_free($xmlparser); 

fclose($fp);
?>

 

I am calling the count with $i but all its generating is the number 1 for each project rather than counting up.

 

I have a feeling its because it is in the first function and only counting the 1 project each time the function is run rathern than counting the whole thing...

 

Any ideas on moving or editing it would be awesome cheers :)

 

Link to comment
https://forums.phpfreaks.com/topic/90208-counting-xml-nodes-with-php/
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.