matfish Posted June 7, 2011 Share Posted June 7, 2011 Hi there, The usual way I use XML is using: <xml> <item> <id>1</id> <body>content 1</body> </item> <item> <id>2</id> <body>content 2</body> </item> </xml> However I'm having to pull the content from a CMS which is out of my control which uses: <xml> <item id="1" body="content 1" /> <item id="2" body="content 2" /> </xml> Is there a way to parse and loop through this type of XML formatting easily and put into a PHP array? Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/ Share on other sites More sharing options...
Maq Posted June 7, 2011 Share Posted June 7, 2011 Is there a way to parse and loop through this type of XML formatting easily and put into a PHP array? Sure, check out: http://us.php.net/simplexml Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/#findComment-1226528 Share on other sites More sharing options...
matfish Posted June 7, 2011 Author Share Posted June 7, 2011 Hi, thanks, I did have a look at that but does it support the layout I'm having trouble with? I keep getting string errors. Many thanks Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/#findComment-1226542 Share on other sites More sharing options...
Maq Posted June 7, 2011 Share Posted June 7, 2011 Hi, thanks, I did have a look at that but does it support the layout I'm having trouble with? I keep getting string errors. Many thanks Yes, simplexml can handle attributes (id & body), which is perfectly valid XML BTW. Can you show us the problematic code? Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/#findComment-1226566 Share on other sites More sharing options...
matfish Posted June 7, 2011 Author Share Posted June 7, 2011 I think I'm getting my head around it now. I'm currently using: $xmlstr = <<<XML <xml> <item id="1" body="content 1" /> <item id="2" body="content 2" /> </xml> XML; $xml = new SimpleXMLElement($xmlstr); foreach ($xml->item as $data) { echo $data['id'].": ".$data['body']."<br />"; } Many thanks Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/#findComment-1226675 Share on other sites More sharing options...
Maq Posted June 7, 2011 Share Posted June 7, 2011 Yep, looks like you have the basic idea. Did you end up solving your problem? Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/#findComment-1226679 Share on other sites More sharing options...
matfish Posted June 8, 2011 Author Share Posted June 8, 2011 Using the code from my previous post, I've got that working. But another form of XML I'm dealing with is: <xml> <item Mon="9:00am" /> <item Tue="9:30am" /> <item Wed="10:00am" /> </xml> I'm trying to get the node/variable name of "Mon/Tue/Wed" into an array alongside it's time in a loop? Any ideas? Thanks for your help. Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/#findComment-1226843 Share on other sites More sharing options...
salathe Posted June 8, 2011 Share Posted June 8, 2011 You can use attributes() to access the attributes for each item. Link to comment https://forums.phpfreaks.com/topic/238681-php-and-unusual-xml-layout/#findComment-1226890 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.