magic07 Posted March 5, 2011 Share Posted March 5, 2011 Hi, I have two problems with reading a XML file with SimpleXML and the foreach loop: Problem number one: If I have an item with only one "segment" child, I get the error "Fatal error: Call to a member function attributes() on a non-object in /var/www/readxml.php on line 28". My way of using a $segnum variable which I increment is maybe not necessary since it is a foreach loop... But what would be the right way? Problem number two: My code only sums up the size of the first two segments, but it should sum up all. What's the problem here? My xml file for testing looks like this: <?xml version="1.0" encoding="iso-8859-1" ?> <xml> <file poster="John Doe <[email protected]>" date="1301234567" subject="bla bla "filename1.zip" yEnc (1/5)"> <groups> <group>a.b.blabla</group> <group>a.b.dadada</group> </groups> <segments> <segment bytes="100000" number="1">[email protected]</segment> <segment bytes="200000" number="2">[email protected]</segment> <segment bytes="300000" number="3">[email protected]</segment> <segment bytes="400000" number="4">[email protected]</segment> <segment bytes="50000" number="5">[email protected]</segment> </segments> </file> <file poster="John Doe <[email protected]>" date="1301234678" subject="bla bla "filename2.zip" yEnc (1/5)"> <groups> <group>a.b.blabla</group> <group>a.b.dadada</group> </groups> <segments> <segment bytes="300000" number="1">[email protected]</segment> <segment bytes="400000" number="2">[email protected]</segment> <segment bytes="500000" number="3">[email protected]</segment> <segment bytes="600000" number="4">[email protected]</segment> <segment bytes="50000" number="5">[email protected]</segment> </segments> </file> <file poster="John Doe <[email protected]>" date="1301234789" subject="bla bla "filename3.zip" yEnc (1/1)"> <groups> <group>a.b.blabla</group> <group>a.b.dadada</group> </groups> <segments> <segment bytes="200000" number="1">[email protected]</segment> </segments> </file> </xml> My PHP code: <?php // read the xml file $xml = simplexml_load_file('demo.xml'); // initialize helper variables $filenum = 0; // loop through all files foreach($xml as $files){ // get the posting date $timestamp = (int)$xml->file[$filenum]->attributes()->date; echo date("Y-m-d H:i:s", $timestamp); echo " - "; // get the filename $subject = $xml->file[$filenum]->attributes()->subject; preg_match("#.*\"(.*)\".*#iu", $subject, $matches); echo $matches[1]; echo " - "; // loop through all segments of file to get total size $segnum = 0; $totalbytes = 0; foreach($xml->file[$filenum] as $segments){ $totalbytes += $xml->file[$filenum]->segments->segment[$segnum]->attributes(); $segnum++; } // end segments echo $totalbytes; echo "<br />\n"; $filenum++; } // end files ?> Current output: 2011-03-27 16:02:47 - filename1.zip - 300000 2011-03-27 16:04:38 - filename2.zip - 700000 2011-03-27 16:06:29 - filename3.zip - Fatal error: Call to a member function attributes() on a non-object in /var/www/readxml.php on line 28 Thank you very much for your help! Link to comment https://forums.phpfreaks.com/topic/229696-problems-with-simplexml-and-foreach/ Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.