0

This is my xml file

<CATALOG>
	<category>
		<name>Engineering</name>
		<subject>Civil Works</subject>
      <subject>Water Supply</subject>
      <subject>Street Lighting</subject>
	</category>

<category>
		<name>Public Health</name>
		<subject>Sanitation</subject>
      <subject>Dispensaries</subject>
      <subject>Maternity Services</subject>
	</category>

	<category>
		<name>Town Planing</name>
		<subject>T.P.S</subject>
		<subject>T.P.B.O</subject>
      <subject>Tracer</subject>
      <subject>Chairmen</subject>
	</category>

	<category>
		<name>Administration</name>
		<subject>Ministerial Manager</subject>
      <subject>Revenue R.O.</subject>
      <subject>Accounts Accountant</subject>
	</category>

</CATALOG>

I have to get the all subject names in category spesified by $_GET["category"];
and i wrote the code as follows.

<?php

     
		 $xml = simplexml_load_file("../category.xml");
        
$subjects = 'Hello';
$categoryes=$xml->xpath('/category');
			foreach($categoryes as $name)
			{
				echo $name;
				if($name == $_GET["category"] )
				{
					foreach($xml->chaildren() as $child)
					{
						$subjects = $subjects." ".$child;
					
					}
				}
			}
		
		

		
		echo $subjects;
?>

what wrong with this code?

thank you.

Edited by sagarrayudu: n/a

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by veedeoo
1

Hi,

It is getting really late in my time zone, that I really need to hit the bunk. Anyways, here is the general form for xpath in php..

I am not going to explain it in great detail, but the whole idea is well presented.

<?php
$xml = simplexml_load_file("../category.xml");

$cat_name = $_GET['category'];

## uncomment $cat_name below to test

//$cat_name = "Civil Works";

$result = $xml->xpath("category[subject='".$cat_name."']/name");

foreach($result as $name){

## echo the name of the matched
echo $name."<br/>";
}
?>

If run the script above, it should give you "Engineering" as a match from the nodes. Just expand the script above to your requirements.

Edited by veedeoo: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.