Re: XML Cdata problem
| From: | Mathieu Dumoulin | Date: | Wed, 30 Jun 2004 16:51:05 +0000 |
| Subject: | Re: XML Cdata problem | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-189310@lists.php.net to get a copy of this message | ||
Forget myt post, its a stupid error from my part because of object
references and stuff like that. I'll fix it myself.
"Mathieu Dumoulin" <mdumoulin@groupimage.com> wrote in message
news:20040630164243.5191.qmail@pb1.pair.com...
> PHP 4.3.7 on Linux Red Hat 7.3 full patch and updates, Expat expat_1.95.2
>
> Problem:
>
> I am reading the XML file below and at path:
>
> advantage/route/origin/status/
>
> The CDATA inside the STATUS node cannot be read (Current value 1900),
there
> isnt much information on how to use the EXPAT library on PHP.Net apart
from
> the extensive definition of all functions so i'm not sure im using EXPAT
the
> correct way. Please tell me if im doing something wrong, here is my code
and
> the XML file is further below:
>
> ************************
> * PHP CODE *
> ************************
>
> //Load the XML like it was a XML reponse
> $xml = implode('', file('xml.xml'));
> echo '<PRE>'.str_replace(array('<', '>'),
> array('<', '>'),
> $xml).'</PRE>';
>
> /*For now all objects are empty shells without data in them, the XML
parser
> will link everything together nicely
> and will add the CDATA for objects if there is some. Later we will add the
> standard options to each class
> and create functions for output.*/
>
> //Contains an advantage server response, very basic object with only one
> thing below
> class advantage_node {
> function advantage_response() {}
> }
>
> //Contains an advantage server data representing a route
> class route_node {
> function route_node() {}
> }
>
> //Contains an advantage server data representing a parsed input data from
> the server
> class origin_node {
> function origin_node() {}
> }
>
> //Contains an advantage server data representing a status
> class status_node {
> function status_node() {}
> }
>
> //Contains an advantage server data representing a location
> class location_node {
> function location_node() {}
> }
>
> //Contains an advantage server data representing a number
> class number_node {
> function number_node() {}
> }
>
> //In charge of making the object tree and handling first level objects
> function xml_to_class_tag_start($parser, $element, $attributes){
> $element_class = $element.'_node';
> if(class_exists($element_class) &&
> isset($GLOBALS['element_levels'][$GLOBALS['element_level']-1])){
> $new_element = new $element_class;
> if(is_array($attributes)){ foreach($attributes as $attribute => $value){
> $new_element->$attribute = $value;
> }}
> if($GLOBALS['element_level']-1 >= 0){
> $GLOBALS['element_levels'][$GLOBALS['element_level']-1]->$element =
> &$new_element;
> }
> $GLOBALS['element_levels'][$GLOBALS['element_level']] =
> &$new_element;
> }
> ++$GLOBALS['element_level'];
> }
>
> //In charge of closing the current object and rolling back to the previous
> one
> function xml_to_class_tag_end($parser, $element){
> if($GLOBALS['element_level'] > 1){
> unset($GLOBALS['element_levels'][$GLOBALS['element_level']]);
> }
> $GLOBALS['element_level']--;
> }
>
> function xml_to_class_tag_data($parser, $data){
> if(trim($data) != ''){
> $GLOBALS['element_levels'][$GLOBALS['element_level']]->cdata = $data;
> }
> }
>
> //Setup the objects
> $element_level = 0;
> $element_levels = array(-1 => '');
>
> //Start the XML parsing
> $parser = xml_parser_create();
> xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 1);
> xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 0);
> xml_set_element_handler($parser, 'xml_to_class_tag_start',
> 'xml_to_class_tag_end');
> xml_set_character_data_handler($parser, 'xml_to_class_tag_data');
> xml_parse($parser, $xml);
> xml_parser_free($parser);
>
> //Output
> echo '<pre>'.print_r($element_levels[0], true).'</pre>';
> ************************* RESULT
> *************************advantage_node Object
> (
> [ROUTE] => route_node Object
> (
> [STATUS] => 0
> [ORIGIN] => origin_node Object
> (
> [COUNT] => 1
> [STATUS] => status_node Object
> (
> )
>
> [LOCATION] => location_node Object
> (
> [NUMBER] => number_node Object
> (
> )
>
> )
>
> )
>
> )
>
> )************************
> * XML DATA *
> ************************
>
> <advantage>
> -
> <route status="0">
> -
> <parameters>
> <clientId>[protected]</clientId>
> <destcountry>ca</destcountry>
> <destpostalcode>h1p 2w8</destpostalcode>
> <origcountry>ca</origcountry>
> <origpostalcode>h1z 2l2</origpostalcode>
> <transaction>route</transaction>
> </parameters>
> -
> <input>
> -
> <origin count="1">
> -
> <location>
> <postalCode>h1z 2l2</postalCode>
> <country>ca</country>
> </location>
> </origin>
> -
> <destination count="1">
> -
> <location>
> <postalCode>h1p 2w8</postalCode>
> <country>ca</country>
> </location>
> </destination>
> </input>
> -
> <origin count="1">
> <status>1900</status>
> -
> <location>
> <number>1</number>
> <city>MONTREAL</city>
> <stateProvince>QC</stateProvince>
> <postalCode>H1Z 2L2</postalCode>
> <country>CA</country>
> <latitude>45.569129</latitude>
> <longitude>-73.601440</longitude>
> <geocodeQuality>ZIP</geocodeQuality>
> <iconId>9191</iconId>
> -
> <map>
> <height>119</height>
> <width>198</width>
> <type>8</type>
> <mapStyle>default</mapStyle>
> -
> <request>
> [useless junk]
> </request>
> </map>
> </location>
> </origin>
> -
> <destination count="1">
> <status>1900</status>
> -
> <location>
> <number>1</number>
> <city>SAINT-LEONARD</city>
> <stateProvince>QC</stateProvince>
> <postalCode>H1P 2W8</postalCode>
> <country>CA</country>
> <latitude>45.598209</latitude>
> <longitude>-73.594200</longitude>
> <geocodeQuality>ZIP</geocodeQuality>
> <iconId>9192</iconId>
> -
> <map>
> <height>119</height>
> <width>198</width>
> <type>9</type>
> <mapStyle>default</mapStyle>
> -
> <request>[useless junk]</request>
> </map>
> </location>
> </destination>
> -
> <overviewMap>
> <height>342</height>
> <width>594</width>
> <type>4</type>
> <zoomLevel>8</zoomLevel>
> <latitude>45.583550</latitude>
> <longitude>-73.597881</longitude>
> <mapStyle>default</mapStyle>
> -
> <request>[useless junk]</request>
> <mapSessionId>40e2d674-003a-02b7-59c5-0ec30b00010f</mapSessionId>
> </overviewMap>
> -
> <maneuvers count="6">
> -
> <maneuver>
> <number>1</number>
> -
> <text>
> Start out going Southwest on BOULEVARD CRÉMAZIE E toward 20E AVE.
> </text>
> <time>6</time>
> <distance units="mi">0.02</distance>
> </maneuver>
> -
> <maneuver>
> <number>2</number>
> <text>Turn RIGHT onto 20E AVE.</text>
> <time>36</time>
> <distance units="mi">0.14</distance>
> </maneuver>
> -
> <maneuver>
> <number>3</number>
> <text>Turn RIGHT onto RUE JARRY E.</text>
> <time>234</time>
> <distance units="mi">1.13</distance>
> </maneuver>
> -
> <maneuver>
> <number>4</number>
> <text>Turn LEFT onto BOULEVARD LACORDAIRE.</text>
> <time>132</time>
> <distance units="mi">0.78</distance>
> </maneuver>
> -
> <maneuver>
> <number>5</number>
> <text>Turn RIGHT onto RUE BELHERBE.</text>
> <time>108</time>
> <distance units="mi">0.43</distance>
> </maneuver>
> -
> <maneuver>
> <number>6</number>
> <text>Turn LEFT onto RUE VAL MARIE.</text>
> <time>24</time>
> <distance units="mi">0.14</distance>
> </maneuver>
> <totalTime>540</totalTime>
> <totalDistance units="mi">2.64</totalDistance>
> </maneuvers>
> </route>
> </advantage>