Jump to content

parsing xml


blinks

Recommended Posts

I'm trying to parse some XML code. It is working OK at the parent level (e.g. orderby), but not at the child level (communities, PIDs). I'm sure the answer is really simple, but I'm just not seeing it, so I'd appreciate some guidance.

 

Here's the XML -

<?php
$xmlstr = <<<XML
<?xml version="1.0" encoding="UTF-8" ?> 
<mypids>
<orderby>PID</orderby>
<communities>
	<community>278</community>
	<community>788</community>
</communities>
<PIDs>
	<pid>7888</pid>
	<pid>7889</pid>
</PIDs>
</mypids>
XML;
?>

 

and here's the php -

 <?php 

include_once("example2.php"); 

$xml = new SimpleXMLElement($xmlstr); 

//read xml into variables
$orderby = (string) $xml->orderby;
echo $orderby.'<br />';

for($i=0;$i<sizeof($xml->communities->community);$i++){
   foreach($xml->communities->community[$i] as $xcommunity){
      echo $xcommunity."<br/>";
   }
}

for($i=0;$i<sizeof($xml->PIDs->pid);$i++){
   foreach($xml->PIDs->pid[$i] as $xpids){
      echo $xpids."<br/>";
   }
}
?> 
  

Link to comment
https://forums.phpfreaks.com/topic/182721-parsing-xml/
Share on other sites

OK, got it -

<?php
include_once("example2.php"); 
$xml = new SimpleXMLElement($xmlstr); 
//read xml into variables
$orderby = (string)$xml->orderby;
echo $orderby.'<br />';
foreach ($xml->communities->community as $comm) {
echo $comm.'<br />';
}

foreach ($xml->PIDs->pid as $ids) {
echo $ids.'<br />';
}
?> 

 

Link to comment
https://forums.phpfreaks.com/topic/182721-parsing-xml/#findComment-964442
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.