Hi,i have an xml files that looks like this

<?xml version="1.0" encoding="utf-8"?>
<photogallery ID="8" name="Residential">
	<album ID="5" name="Leb" photogallery="Residential" date="29/10/2008" location="Lebanon" architecture="Roman" img="thumbnail_1245677486.jpg">
		<picture ID="Leb" path="thumbnail_1245670921"/>
	</album>
	<album ID="6" name="Leb" photogallery="Residential" date="29/10/2008" location="Lebanon" architecture="Roman" img="thumbnail_1245677486.jpg"/>
		<picture ID="Leb" path="thumbnail_1245670921"/>
		<picture ID="Leb" path="thumbnail_1245670921"/>
	</album>
	<album ID="7" name="Leb" photogallery="Residential" date="29/10/2008" location="Lebanon" architecture="Roman"/>
		<picture ID="Leb" path="thumbnail_1245670921"/>
		<picture ID="Leb" path="thumbnail_1245670921"/>
		<picture ID="Leb" path="thumbnail_1245670921"/>
	<album ID="8" name="Allo" photogallery="Residential" date="2/7/1975" location="Alll" architecture="Lol">
		<picture ID="Allo" path="thumbnail_1245658288"/>
		<picture ID="Allo" path="thumbnail_1245658288"/>
		<picture ID="Allo" path="thumbnail_1245658288"/>
		<picture ID="Allo" path="thumbnail_1245673276"/>
	</album>
</photogallery>

i need to get the pictures of a specific album.
i got managed a way to get to my specific album, but the problem is that im not able to get only this album children! can i do it in php? or i have to use javascript!?
this is my php code

<?php
	$albid=$_GET['ID'];
	
$xmlFile = new DOMDocument();
$xmlFile->load("Residential.xml");

$xml_album = $xmlFile->getElementsByTagName("album");
	$i = 0;
		while ($xml_album->item($i)->getAttribute('ID') != $albid)
				{
					$i += 1;
				}		
				$album = $xml_album->item($i); 
				echo ("pictures in this album are:");//my problem:D
				
				
	?>

thanks.

Recommended Answers

All 6 Replies

So it doesn't matter which language represents the XML document as DOM, it will have the same structure and methods defined by the DOM Specs.

Use var_dump() and get_class_methods() to find out what methods are available on an object.

For example, on your $album object.

It really helps to use teh functions that give you program state, like:

var_dump(), print_r() get_class_methods(), Reflection etc.

Your XML document is not well-format.

<?php
$albid=$_GET['ID'];
	
$xmlFile = new DOMDocument();
$xmlFile->load("Residential.xml");

$xml_album = $xmlFile->getElementsByTagName("album");
$i = 0;
while ($xml_album->item($i)->getAttribute('ID') != $albid)
  {
	$i += 1;
   }		
  $album = $xml_album->item($i); 
  $children=$album->childNodes;
   foreach($children as $child) {
       if($child->nodeType==XML_ELEMENT_NODE) {
            print "<br/>" . $child->tagName;
            print " ID : " . $child->attributes->item(0)->value;
            print " path : " . $child->attributes->item(1)->value;
      }
   }                 
?>

Note that:

while ($xml_album->item($i)->getAttribute('ID') != $albid)
  {
	$i += 1;
   }

can will trigger an error if $albid doesn't have a corresponding value in the XML doc.

a better alternative is:

while ($xml_album->item($i) && $xml_album->item($i)->getAttribute('ID') != $albid)
  {
	$i += 1;
   }

PHP does lazy evaluation. Meaning if the first evaluation is false:

$xml_album->item($i)

Then it doesn't go on to evaluate

&& $xml_album->item($i)->getAttribute('ID') != $albid

That way you break the loop before an error is triggered. Note that if there was not error triggered, you'd actually go into an infinite loop.

Also good to check if you actually found a corresponding node, otherwise you'd run into more errors:

if ($album = $xml_album->item($i)) {
// do stuff

} else {
// error stuff
}

thanks for your reply, it really helped very much.
i used adatapost answer and changed the syntax of "WHILE" as digital-ether adviced:) thanks very much

I also want this kind of output for given Xml below.

<Class class_id="3">
<Title>Cricket</Title>
<Type type_id="883" type_minbet="0.1" type_maxbet="2000.0">
<Title>County Championship</Title>
<Event start_time="2008-08-27 10:30:00" ev_id="1648740">
<Description>Hampshire v Durham</Description>
<Market mkt_typ="Hampshire Innings Runs" mkt_id="2331165">
<Occurrence bet_id="17998496">
<Description>Under 220.5</Description>
</Occurrence>
<Occurrence bet_id="17998497">
<Description>Over 220.5</Description>
</Occurrence>
</Market>
<Market mkt_typ="Next Dismissal (HAM) (1)" mkt_id="2331166">
<Occurrence bet_id="17998472">
<Description>Caught</Description>
</Occurrence>
<Occurrence bet_id="17998473">
<Description>Any Other</Description>
</Occurrence>
</Market>
</Event>
</Type>
<Type type_id="4208" type_minbet="0.1" type_maxbet="2000.0">
<Title>Indian Premier League</Title>
<Event start_time="2012-05-09 15:30:00" ev_id="3067543">
<Description>Indian Premier League 2012</Description>
<Market mkt_typ="Outright Winner" mkt_id="7846988">
<Occurrence bet_id="46111088">
<Description>Kolkata Knight Riders</Description>
</Occurrence>
<Occurrence bet_id="46111116">
<Description>Delhi Daredevils</Description>
</Occurrence>
<Occurrence bet_id="46110744">
<Description>Mumbai Indians</Description>
</Occurrence>
<Occurrence bet_id="46111087">
<Description>Royal Challengers Bangalore</Description>
</Occurrence>
<Occurrence bet_id="46111085">
<Description>Chennai Super Kings</Description>
</Occurrence>
<Occurrence bet_id="46111117">
<Description>Rajasthan Royals</Description>
</Occurrence>
</Market>
<Market mkt_typ="Outright Winner" mkt_id="7846988">
<Occurrence bet_id="46111087">
<Description>Royal Challengers Bangalore</Description>
</Occurrence>
<Occurrence bet_id="46111085">
<Description>Chennai Super Kings</Description>
</Occurrence>
<Occurrence bet_id="46111117">
<Description>Rajasthan Royals</Description>
</Occurrence>
</Market>
</Event>
</Type>
</class>

//for this I made this code 
<?php 

  if(isset($_GET['ev_id'])) 
 {
     $get_event = $_GET['ev_id'];

     $dom = new DomDocument;
     $dom->preserveWhiteSpace = FALSE;
     $xml = file_get_contents("http://localhost/bluesq/clubsq.xml");
     $dom->loadXML($xml);
     $params = $dom->getElementsByTagName('Event'); // Find Sections 
     $i=0;

     while ($params->item($i) && $params->item($i)->getAttribute('ev_id') != $get_event)
     {
         $i += 1;
     }

      $event = $params->item($i); 
      print_r($event);
   }          
?>

In this I didn't get any value in $event.

<?php

if( ! $xml = simplexml_load_file('http://www.mormedyatest.com/mod/bayi/mac/mac.xml'))

    {       
        echo "Xml okunamadı. alpalpa35@gmail.com ile iletişime geçiniz...";     
    } 
    else

    { 

    /*** loop through parent categories ***/ 

    $i = 0;

    foreach($xml->sportTypes as $node) {

        $sportTypes_lastCounter = $xml->sportTypes[$i]->attributes()->lastCounter;
        $sportTypes_total = $xml->sportTypes[$i]->attributes()->total;

         echo "<table width=100% border=1 ><tr><td>    
         - ".$sportTypes_lastCounter." - ".$sportTypes_total."
        </td></tr></table> 
         ";

         $z = 0;


         /*** loop through child categories ***/

             foreach($xml->sportTypes[$i]->sportType as $xml->sportType[$y]->match[$z]) { 


                 $match_homeName = $xml->sportTypes[$i]->sportType[$y]->match[$z]->attributes()->homeName;  
                 $match_awayName = $xml->sportTypes[$i]->sportType[$y]->match[$z]->attributes()->awayName;

                 echo "--- ".$match_homeName." - ".$match_awayName."";

                 $z++;

             }

        $i++;

    }

}
?>

Fatal error: Call to a member function attributes() on a non-object in /home/mormedyt/public_html/mod/bayi/mac/index.php on line 39

My Xml

<xml>
<cal>
<day id="" day="12" date="2012-10-12 00:00:00.0" matchNo="38" dateId="121012" nots="" />
<day id="" day="13" date="2012-10-13 00:00:00.0" matchNo="29" dateId="121013" nots="" />
<day id="" day="14" date="2012-10-14 00:00:00.0" matchNo="27" dateId="121014" nots="" />
<day id="" day="15" date="2012-10-15 00:00:00.0" matchNo="6" dateId="121015" nots="" />
<day id="" day="16" date="2012-10-16 00:00:00.0" matchNo="4" dateId="121016" nots="" />
<day id="" day="17" date="2012-10-17 00:00:00.0" matchNo="0" dateId="121017" nots="" />
</cal>
<sportTypes lastCounter="242" total="38">
   <sportType id="1" name="Futbol">
      <match id="673908" seasonId="2294" groupId="0" fixId="673908" sprotTypeId="1"  homeName="Adelaide United Fc"  homeId="1692"  awayName="Western Sydney"  awayId="16303"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="11:30"  startDateStamp="1210121130"  titleName="2012-2013 Australia A League" timerMin="-" tvId="768643" scoreSet="-" scoreHeader="." />
      <match id="730600" seasonId="1471" groupId="5214" fixId="730600" sprotTypeId="1"  homeName="Czech Republic U21"  homeId="2209"  awayName="Russia U21"  awayId="2846"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="17:00"  startDateStamp="1210121700"  titleName="2013 UEFA U21 Championship - Qualifying Playoff" timerMin="-" tvId="0" scoreSet="-" scoreHeader="." />
      <match id="720688" seasonId="1846" groupId="3676" fixId="720688" sprotTypeId="1"  homeName="Russia"  homeId="1410"  awayName="Portugal"  awayId="673"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="18:00"  startDateStamp="1210121800"  titleName="2014 FIFA World Cup Eliminations - Europe Group F" timerMin="-" tvId="99998701" scoreSet="-" scoreHeader="." />
      <match id="720706" seasonId="1846" groupId="3679" fixId="720706" sprotTypeId="1"  homeName="Finland"  homeId="995"  awayName="Georgia"  awayId="2850"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="18:30"  startDateStamp="1210121830"  titleName="2014 FIFA World Cup Eliminations - Europe Group I" timerMin="-" tvId="770945" scoreSet="-" scoreHeader="." />
      <match id="720670" seasonId="1846" groupId="3673" fixId="720670" sprotTypeId="1"  homeName="Kazakhstan"  homeId="2205"  awayName="Austria"  awayId="2859"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="19:00"  startDateStamp="1210121900"  titleName="2014 FIFA World Cup Eliminations - Europe Group C" timerMin="-" tvId="768667" scoreSet="-" scoreHeader="." />
      <match id="720666" seasonId="1846" groupId="3672" fixId="720666" sprotTypeId="1"  homeName="Czech Republic"  homeId="2853"  awayName="Malta"  awayId="2153"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="19:00"  startDateStamp="1210121900"  titleName="2014 FIFA World Cup Eliminations - Europe Group B" timerMin="-" tvId="770951" scoreSet="-" scoreHeader="." />
      <match id="730602" seasonId="1471" groupId="5214" fixId="730602" sprotTypeId="1"  homeName="Germany U21"  homeId="2836"  awayName="Switzerland U21"  awayId="2834"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="19:00"  startDateStamp="1210121900"  titleName="2013 UEFA U21 Championship - Qualifying Playoff" timerMin="-" tvId="0" scoreSet="-" scoreHeader="." />
      <match id="720681" seasonId="1846" groupId="3675" fixId="720681" sprotTypeId="1"  homeName="Albania"  homeId="2204"  awayName="Iceland"  awayId="2860"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="20:00"  startDateStamp="1210122000"  titleName="2014 FIFA World Cup Eliminations - Europe Group E" timerMin="-" tvId="0" scoreSet="-" scoreHeader="." />
      <match id="730606" seasonId="1471" groupId="5214" fixId="730606" sprotTypeId="1"  homeName="England U21"  homeId="2840"  awayName="Serbia U21"  awayId="7132"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="20:00"  startDateStamp="1210122000"  titleName="2013 UEFA U21 Championship - Qualifying Playoff" timerMin="-" tvId="0" scoreSet="-" scoreHeader="." />
      <match id="720664" seasonId="1846" groupId="3672" fixId="720664" sprotTypeId="1"  homeName="Armenia"  homeId="2816"  awayName="Italy"  awayId="1407"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="20:00"  startDateStamp="1210122000"  titleName="2014 FIFA World Cup Eliminations - Europe Group B" timerMin="-" tvId="771889" scoreSet="-" scoreHeader="." />
      <match id="720677" seasonId="1846" groupId="3674" fixId="720677" sprotTypeId="1"  homeName="Turkey"  homeId="1416"  awayName="Romania"  awayId="678"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="20:30"  startDateStamp="1210122030"  titleName="2014 FIFA World Cup Eliminations - Europe Group D" timerMin="-" tvId="0" scoreSet="-" scoreHeader="." />
      <match id="720705" seasonId="1846" groupId="3679" fixId="720705" sprotTypeId="1"  homeName="Belarus"  homeId="2852"  awayName="Spain"  awayId="669"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:00"  startDateStamp="1210122100"  titleName="2014 FIFA World Cup Eliminations - Europe Group I" timerMin="-" tvId="771963" scoreSet="-" scoreHeader="." />
      <match id="720665" seasonId="1846" groupId="3672" fixId="720665" sprotTypeId="1"  homeName="Bulgaria"  homeId="992"  awayName="Denmark"  awayId="677"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:00"  startDateStamp="1210122100"  titleName="2014 FIFA World Cup Eliminations - Europe Group B" timerMin="-" tvId="771943" scoreSet="-" scoreHeader="." />
      <match id="720701" seasonId="1846" groupId="3678" fixId="720701" sprotTypeId="1"  homeName="Moldova"  homeId="2154"  awayName="Ukraine"  awayId="2857"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:00"  startDateStamp="1210122100"  titleName="2014 FIFA World Cup Eliminations - Europe Group H" timerMin="-" tvId="0" scoreSet="-" scoreHeader="." />
      <match id="720696" seasonId="1846" groupId="3677" fixId="720696" sprotTypeId="1"  homeName="Slovakia"  homeId="2818"  awayName="Latvia"  awayId="1415"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:15"  startDateStamp="1210122115"  titleName="2014 FIFA World Cup Eliminations - Europe Group G" timerMin="-" tvId="771977" scoreSet="-" scoreHeader="." />
      <match id="720675" seasonId="1846" groupId="3674" fixId="720675" sprotTypeId="1"  homeName="Estonia"  homeId="999"  awayName="Hungary"  awayId="993"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:30"  startDateStamp="1210122130"  titleName="2014 FIFA World Cup Eliminations - Europe Group D" timerMin="-" tvId="771989" scoreSet="-" scoreHeader="." />
      <match id="720653" seasonId="1846" groupId="3671" fixId="720653" sprotTypeId="1"  homeName="FYR Macedonia"  homeId="2815"  awayName="Croatia"  awayId="1412"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:30"  startDateStamp="1210122130"  titleName="2014 FIFA World Cup Eliminations - Europe Group A" timerMin="-" tvId="771985" scoreSet="-" scoreHeader="." />
      <match id="720683" seasonId="1846" groupId="3675" fixId="720683" sprotTypeId="1"  homeName="Switzerland"  homeId="2854"  awayName="Norway"  awayId="670"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:30"  startDateStamp="1210122130"  titleName="2014 FIFA World Cup Eliminations - Europe Group E" timerMin="-" tvId="771991" scoreSet="-" scoreHeader="." />
      <match id="720655" seasonId="1846" groupId="3671" fixId="720655" sprotTypeId="1"  homeName="Wales"  homeId="1411"  awayName="Scotland"  awayId="861"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:45"  startDateStamp="1210122145"  titleName="2014 FIFA World Cup Eliminations - Europe Group A" timerMin="-" tvId="768665" scoreSet="-" scoreHeader="." />
      <match id="720671" seasonId="1846" groupId="3673" fixId="720671" sprotTypeId="1"  homeName="Republic of Ireland"  homeId="990"  awayName="Germany"  awayId="671"  score="0-0"  foul="0-0"  status="SCH"  nots="--"  startDate="21:45"  startDateStamp="1210122145"  titleName="2014 FIFA World Cup Eliminations - Europe Group C" timerMin="-" tvId="772029" scoreSet="-" scoreHeader="." />
      </sportType></sportTypes></xml>

Match with php id, homeName, awayName values ??need to read and write the screen.Help me....
Thank you ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.