I consider of that but i have built services which have some parameters and mappings so i use a more effective way with paths.For example for a Xml data type i use XPath to access children and for Json i use JPath.Of course there are many ways to parse xml,json but i would like to ask if my approach is ok.
2009/11/9 Miles J <[email protected]> > > Why dont you just use json_encode/json_decode and the built in XML > utility class? > > On Nov 9, 2:08 am, Paulos23 <[email protected]> wrote: > > Hi people, > > I am building my own component for my app needs and i am wondering if > > my idea is right.My app consist of web services where the data may be > > in XML or JSON format.So, i started creating a Component to parse > > Xml,Json returned data using XPath (for Xml) and JPath (for Json).My > > philosophy is that there is an astract class ResponseParser for > > general types of returned data and then 2 sub-classes extends the > > astract class using inheritance.These classes are XmlParser and > > JsonParser.If the data is in Xml format a new object of XmlParser > > class is created and so on.now i am wondering if my approach is > > good.Here is my code: > > > > <?php > > class ResponseParser { > > function modelParse($text,$modelName,$mappings = array(), > > $responseType) { > > > > } > > > > } > > > > //Using X-Path to implement XML parsing > > class XmlParser extends ResponseParser { > > var modelFields = array(array()); > > > > function xmlParse($response,$model,$mappings = array()){ > > $xml = new SimpleXMLElement($response); > > for ($j = 0; $j < > sizeof($xml->xpath('/geonames/'.$model.''); $j += > > 1) { // Loop to access all children > > for ($i = 0; $i < sizeof($mappings); $i += 1) { > // Loop to > > map each child > > $modelField[$j][$i] = > $xml->xpath(''.$mappings[0]['Composition'] > > [$i]['map'].''); > > } > > } > > return($modelField); > > } > > > > } > > > > //Using J-Path to implement JSON parsing > > class JsonParser extends ResponseParser{ > > var modelFields = array(array()); > > > > function jsonParse($response,$model,$mappings = array()){ > > var jp = new JPath($response); > > for ($j = 0; $j < > sizeof(jp.$('/geonames/'.$model.'').json); $j += > > 1) { // Loop to access all children > > for ($i = 0; $i < sizeof($mappings); $i += 1) { > // Loop to > > map each child > > $modelField[$j][$i] = > jp.$(''.$mappings[0]['Composition'][$i] > > ['map'].'').json; > > } > > } > > return($modelField); > > } > > > > } > > > > class ServiceCompositionEngineComponent extends Object { > > > > function startup(&$controller) { > > $this->controller =& $controller; > > } > > > > function getRespType($responseType,$text,$model,$mappings = > array()){ > > if ($responseType == 'XML'){ > > var xmlpsr = new xmlParse($text,$model,$mappings > = array()) > > } else if($responseType == 'JSON'){ > > var jsonpsr = new > jsonParse($text,$model,$mappings = array()) > > } > > > > } > > > > } > > > > ?> > > > > Any help would be nice!! > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "CakePHP" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/cake-php?hl=en -~----------~----~----~----~------~----~------~--~---
