Exodus00FF Posted April 24, 2008 Share Posted April 24, 2008 Hi all First time Poster Well I'm having a problem with an XML Reader that I made. When I run this XML Reader Class, the $n value keeps getting reset to 0, and by the end of the function when I return the $assoc array it only comes back with one level of data in it. I can't figure out for the life of me why. I'm attaching my Class (excuse all the echo's, they won't be in my final version i'm just using them to see what is happening to my variables as the script runs) My index.php file and a copy of an XML document that I am running this against. Thank you ahead of time for any assistance you may be able to offer me. J <?PHP class xmlfunc { var $n = 0; var $assoc= array(); function xml2assoc($xml){ if (isset($this->$assoc)==false){ echo '<tr><td>Assoc = false </td><td>' . "\n"; } else { echo '<tr><td>Assoc = True </td><td>' . "\n"; } if (isset($this->$n)==false){ echo 'N = False </td><td>' . "\n"; } else{ echo 'N = True </td><td>' . "\n"; } echo $n . '</td></tr>' . "\n"; while($xml->read()){ if($xml->nodeType == XMLReader::END_ELEMENT) break; if($n >= 1000) break; if($xml->nodeType == XMLReader::ELEMENT){ $this->$assoc[$n]['name'] = $xml->name; if($xml->hasAttributes) while($xml->moveToNextAttribute()) $this->$assoc[$n]['atr'][$xml->name] = $xml->value; $this->$assoc[$n]['val'] = self::xml2assoc($xml); echo '<tr><td>' . $this->$assoc[$n]['name'] . '</td><td>'. $this->$assoc[$n]['atr'] . '</td><td>' . $this->$assoc[$n]['val'] . '</td></tr>' . "\n"; $n++; } else if($xml->nodeType == XMLReader::TEXT) $assoc = $xml->value; } return $assoc; } } ?> <?PHP include('xml.class.php'); $filename='XML/BAYLINER_2008_22187_03172008/BAYLINER_2008_103765.xml'; $assoc= NULL; $n = 0; $xmlAcquire = new xmlfunc; $xml = new XMLReader(); $xml->open($filename); echo '<table border=1><tr><th>Name</th><th>attr</th><th>val</th>'; $assoc = $xmlAcquire->xml2assoc($xml); echo '</table>'; $xml->close(); echo count($assoc); ?> <Brochure formname="" modelid="114325" cid="" parent_cid="" bvdid="" getxml="0" hit="" reflink="" tab="" custom="" indid="" isPreview="" lcsm="0" surl=""> <model_info> <cid>22197</cid> <top_cid>88</top_cid> <companyname>Albemarle - 2008</companyname> <fullname>2008 242 Center Console</fullname> <displayname>2008 Albemarle 242 Center Console</displayname> <hasdrawing>1</hasdrawing> <hasphoto>1</hasphoto> <length>23'7" (7.19 m)</length> <make>Albemarle</make> <capacity> </capacity> <modelid>114325</modelid> <modelyear>2008</modelyear> <pricewords>MSRP</pricewords> <productid>846757</productid> <productname>Center Console</productname> <companylogo>http://media.channelblade.com/boat_graphics/logos/logo_22197.jpg</companylogo> <indid>1</indid> </model_info> <keyfeatures> <feature> <name>Casting platform.</name> </feature> <feature> <name>Locking rod storage boxes.</name> </feature> <feature> <name>Optional anodized aluminum leaning post.</name> </feature> <feature> <name>24 gallon livewell.</name> </feature> <feature> <name>40 gallon transom fishbox.</name> </feature> </keyfeatures> <standard_features> <category name="STANDARD EQUIPMENT"> <feature> <name>Automatic Halon® Fire Protection</name> </feature> <feature> <name>Bilge Blower</name> </feature> <feature> <name>Bilge Pumps (2)</name> </feature> <feature> <name>Bronze Flanged Seacocks</name> </feature> <feature> <name>Coast Guard Safety Kit</name> </feature> <feature> <name>Compass</name> </feature> <feature> <name>Electric Trim Tabs</name> </feature> <feature> <name>Electrical System: with Breakers</name> </feature> <feature> <name>Four Flush-Mounted Rod Holders</name> </feature> <feature> <name>Full Instrumentation</name> </feature> <feature> <name>Gas-Assisted Engine Hatch Lift</name> </feature> <feature> <name>Heavy Duty Vinyl Rub Rail: with Stainless Steel Insert</name> </feature> <feature> <name>Helm Chairs: with Cushions</name> </feature> <feature> <name>High-Water Alarm</name> </feature> <feature> <name>Hydraulic Steering</name> </feature> <feature> <name>Large Dash: for Flush-Mounted Electronics</name> </feature> <feature> <name>Livewell</name> </feature> <feature> <name>Lockable Cockpit Storage</name> </feature> <feature> <name>Locking Electronics Box</name> </feature> <feature> <name>Locking Rod Storage Boxes: (in Bow)</name> </feature> <feature> <name>Mini-Dual Air Trumpet Horn</name> </feature> <feature> <name>Molded Fiberglass: Self-Draining Cockpit</name> </feature> <feature> <name>Navigation & Cockpit Lights</name> </feature> <feature> <name>Non-Skid Deck</name> </feature> <feature> <name>Prewiring for Electronics</name> </feature> <feature> <name>Raw Water Washdown</name> </feature> <feature> <name>SeaKey® Install Kit</name> </feature> <feature> <name>Stainless Steel Bow: Stern and Spring Cleats</name> </feature> <feature> <name>Stainless Steel Bow Eye</name> </feature> <feature> <name>Stainless Steel Bow Rail</name> </feature> <feature> <name>Stainless Steel Hawse Pipes</name> </feature> <feature> <name>Stainless Steel Propellers</name> </feature> <feature> <name>Tackle Storage</name> </feature> <feature> <name>Tilt Steering Wheel</name> </feature> <feature> <name>Transom Fishbox</name> </feature> <feature> <name>Windshield</name> </feature> </category> </standard_features> <specifications> <category name="Length w/o Pulpit"> <name>23'7" (7.19 m)</name> <name> </name> </category> <category name="Beam"> <name>8'0" (2.44 m)</name> <name> </name> </category> <category name="Draft"> <name>30" (.76 m)</name> <name> </name> </category> <category name="Dry Weight"> <name>3,900 lbs (1,769.01 kg)</name> <name> </name> </category> <category name="Deadrise"> <name>24 degrees</name> <name> </name> </category> <category name="Livewell"> <name>24 gal. (90.85 L)</name> <name> </name> </category> <category name="Transom Fishbox"> <name>40 gal. (151.42 L)</name> <name> </name> </category> <category name="Fuel Capacity"> <name>118 gal. (444.68 L)</name> <name> </name> </category> <category name="Fuel Range"> <name>243-336 nm</name> <name> </name> </category> <category name="Power"> <name>Single Jackshaft (Gas or Diesel)</name> <name> </name> </category> </specifications> <option_features> <category name="Optional Features"> <name>Bottom Paint</name> <name>Coaming Pads</name> <name>Extra Battery Box & Switch</name> <name>Forward Lounge Cushions</name> <name>Freshwater Cooling</name> <name>Hull Color</name> <name>Leaning Post</name> <name>Soft T-Top: with Taco Outriggers, Light Electronics Box, Rocket Launchers</name> <name>Swim Platform</name> </category> <category name="Power"> <name>Cummins QSD 2.8 230 HP Diesel: with Mercruiser Sea Core Bravo 3</name> <name>MerCruiser 350 MAG-MPI Horizon* 300 HP EFI Sea Core Bravo 3: *Fresh Water cooling is standard on MerCruiser Horizon Engine</name> <name>MerCruiser 5.0 MPI 260 HP EFI Sea Core Bravo 3</name> <name>Volvo 5.0 OSi 270 HP EFI Ocean Series DuoProp Jackshaft</name> <name>Volvo D4 260 HP Diesel DuoProp Jackshaft</name> </category> </option_features> <links> <link link_id="16"> <title>Contact a dealer</title> <href>modelname=2008 Albemarle 242 Center Console&modelid=114325</href> </link> <link link_id="17"> <title>Order a brochure</title> <href>modelname=2008 Albemarle 242 Center Console&modelid=114325</href> </link> <link link_id="88"> <title>Request A Quote</title> <href>modelname=2008 Albemarle 242 Center Console&modelid=114325</href> </link> <link> <title>New to Boating</title> <href>/servlet/modelSearch?formname=modelsearch&cid=22197modelname=2008 Albemarle 242 Center Console&modelid=114325</href> </link> <link link_id="13"> <title>View Photo Gallery</title> <href>pmid=114325&count=0</href> </link> <link link_id="12"> <title>Print Model Info</title> <href>pmid=114325</href> </link> <link link_id="139"> <special_exists>no</special_exists> <title>Specials and Promotions</title> <href>/cgi-bin/specials/specials_redirect.asp?pm_id=114325&modelname=2008 Albemarle 242 Center Console</href> </link> </links> <model_description> <description>The Albemarle 242 is truly one of the finest all-purpose fishing boats under 25 feet on the market today. Featuring our advanced Jackshaft Power System, this unique boat combines fast, fuel efficient, and functional performance, with 360-degree fishability. A wide-open cockpit, soft dry ride, ample storage, and a long list of standard features allows the 242 to perform like no other boat in its class and makes it an unbeatable choice for the fisherman who wants a serious offshore center console that is easy to trailer, yet delivers the absolute best in safety, eliability, and angling adventure.</description> </model_description> <images> <drawings> <drawing id="1562306" size="M"> <file_name>114325_d_t_135x165_image01.gif</file_name> <graphic_seq>1</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>D</graphic_type> <graphic_height>165</graphic_height> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_size_type>M</graphic_size_type> <graphic_width>135</graphic_width> </drawing> <drawing id="1562307" size="L"> <file_name>114325_d_t_270x360_image01.gif</file_name> <graphic_seq>1</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>D</graphic_type> <graphic_height>360</graphic_height> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_size_type>L</graphic_size_type> <graphic_width>270</graphic_width> </drawing> </drawings> <PDFs/> <VRimages/> <Videoimages/> </images> <colors/> <photos> <photo> <image filename="114325_p_t_135x90_image01.jpg" height="90" width="135" size="M" id="1562022"/> <image filename="114325_p_t_90x60_image01.jpg" height="60" width="90" size="S" id="1562043"/> <image filename="114325_p_t_640x480_image01.jpg" height="480" width="640" size="L" id="1562036"/> <image filename="114325_p_t_360x240_image01.jpg" height="240" width="360" size="B" id="1562029"/> <graphic_seq>1</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>P</graphic_type> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_perspective> </graphic_perspective> </photo> <photo> <image filename="114325_p_t_135x90_image02.jpg" height="90" width="135" size="M" id="1562023"/> <image filename="114325_p_t_90x60_image02.jpg" height="60" width="90" size="S" id="1562044"/> <image filename="114325_p_t_640x480_image02.jpg" height="480" width="640" size="L" id="1562037"/> <image filename="114325_p_t_360x240_image02.jpg" height="240" width="360" size="B" id="1562030"/> <graphic_seq>2</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>P</graphic_type> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_perspective> </graphic_perspective> </photo> <photo> <image filename="114325_p_t_90x60_image03.jpg" height="60" width="90" size="S" id="1562045"/> <image filename="114325_p_t_640x480_image03.jpg" height="480" width="640" size="L" id="1562038"/> <image filename="114325_p_t_135x90_image03.jpg" height="90" width="135" size="M" id="1562024"/> <image filename="114325_p_t_360x240_image03.jpg" height="240" width="360" size="B" id="1562031"/> <graphic_seq>3</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>P</graphic_type> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_perspective> </graphic_perspective> </photo> <photo> <image filename="114325_p_t_90x60_image04.jpg" height="60" width="90" size="S" id="1562046"/> <image filename="114325_p_t_135x90_image04.jpg" height="90" width="135" size="M" id="1562025"/> <image filename="114325_p_t_360x240_image04.jpg" height="240" width="360" size="B" id="1562032"/> <image filename="114325_p_t_640x480_image04.jpg" height="480" width="640" size="L" id="1562039"/> <graphic_seq>4</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>P</graphic_type> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_perspective> </graphic_perspective> </photo> <photo> <image filename="114325_p_t_135x90_image05.jpg" height="90" width="135" size="M" id="1562026"/> <image filename="114325_p_t_640x480_image05.jpg" height="480" width="640" size="L" id="1562040"/> <image filename="114325_p_t_360x240_image05.jpg" height="240" width="360" size="B" id="1562033"/> <image filename="114325_p_t_90x60_image05.jpg" height="60" width="90" size="S" id="1562047"/> <graphic_seq>5</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>P</graphic_type> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_perspective> </graphic_perspective> </photo> <photo> <image filename="114325_p_t_640x480_image06.jpg" height="480" width="640" size="L" id="1562041"/> <image filename="114325_p_t_135x90_image06.jpg" height="90" width="135" size="M" id="1562027"/> <image filename="114325_p_t_360x240_image06.jpg" height="240" width="360" size="B" id="1562034"/> <image filename="114325_p_t_90x60_image06.jpg" height="60" width="90" size="S" id="1562048"/> <graphic_seq>6</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>P</graphic_type> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_perspective> </graphic_perspective> </photo> <photo> <image filename="114325_p_t_90x60_image07.jpg" height="60" width="90" size="S" id="1562049"/> <image filename="114325_p_t_135x90_image07.jpg" height="90" width="135" size="M" id="1562028"/> <image filename="114325_p_t_360x240_image07.jpg" height="240" width="360" size="B" id="1562035"/> <image filename="114325_p_t_640x480_image07.jpg" height="480" width="640" size="L" id="1562042"/> <graphic_seq>7</graphic_seq> <graphic_text> </graphic_text> <graphic_title> </graphic_title> <graphic_type>P</graphic_type> <graphic_path>http://media.channelblade.com/boat_graphics/electronic_brochure/company22197</graphic_path> <graphic_perspective> </graphic_perspective> </photo> </photos> <navigation> <standard_features/> <specifications/> <option_features/> <images/> <drawings/> <keyfeatures/> <model_description/> <relatedModels/> </navigation></Brochure> Link to comment https://forums.phpfreaks.com/topic/102661-need-help-with-code/ Share on other sites More sharing options...
monkeytooth Posted April 24, 2008 Share Posted April 24, 2008 Maybe im scanning it to quickly and am missing something.. but from the looks of it, $n is set to 0, but nowhere did I see anything to make $n increase to go onto the next until x is reached... ie: something like $n++ to increase it by one.. again could be wrong and just assuming based on a quick glance.. but if not then worth mentioning.. Link to comment https://forums.phpfreaks.com/topic/102661-need-help-with-code/#findComment-525801 Share on other sites More sharing options...
Exodus00FF Posted April 24, 2008 Author Share Posted April 24, 2008 I have ++$n; on line 35 in my class at the bottom of the below block. while($xml->read()){ if($xml->nodeType == XMLReader::END_ELEMENT) break; if( $n >= 1000) break; if($xml->nodeType == XMLReader::ELEMENT){ $assoc[]['name'] = $xml->name; if($xml->hasAttributes) while($xml->moveToNextAttribute()) $assoc[]['atr'][$xml->name] = $xml->value; $assoc[]['val'] = self::xml2assoc($xml,$n,$assoc); echo '<tr><td>' . $assoc[$n]['name'] . '</td><td>'. $assoc[$n]['atr'] . '</td><td>' . $assoc[$n]['val'] . '</td></tr>' . "\n"; $n++; Link to comment https://forums.phpfreaks.com/topic/102661-need-help-with-code/#findComment-526067 Share on other sites More sharing options...
craygo Posted April 24, 2008 Share Posted April 24, 2008 I may be wrong but I think $n would have to be within your class not on your page. Your loop in the class has no starting point. Ray Link to comment https://forums.phpfreaks.com/topic/102661-need-help-with-code/#findComment-526089 Share on other sites More sharing options...
Exodus00FF Posted April 24, 2008 Author Share Posted April 24, 2008 Well, I've been trying different things. First and foremost I'm used to VB.net, and I'm a new to PHP. One of the things that I did try, was that I passed $n as a variable i.e function xml2assoc ($xml,$n) but I put the & infront so it referenced the original variable. AND THAT WORKS! It will never reset $n to 0. But I tried doing that to the array. But apparently you cannot reference an array in the same manner b/c it gives you a Fatal Error: Cannot use string offset as an array in /home/~username/public_html/nbdev/xml.class.php Is there any way that I create the Variable outside of the class, and instead of the function looking internally for it is there anyway to write it to a variable that available globally? P.S. The code actually comes from the XMLReader function manual page on php.net If you notice after the function first starts, it sets $assoc and $n to 0 and null. So when it calls itself again for the next iteration, it just resets those values. function xml2assoc(&$xml){ $assoc = NULL; $n = 0; while($xml->read()){ if($xml->nodeType == XMLReader::END_ELEMENT) break; if($xml->nodeType == XMLReader::ELEMENT){ $assoc[$n]['name'] = $xml->name; if($xml->hasAttributes) while($xml->moveToNextAttribute()) $assoc[$n]['atr'][$xml->name] = $xml->value; $assoc[$n]['val'] = xml2assoc($xml); $n++; } else if($xml->nodeType == XMLReader::TEXT) $assoc = $xml->value; } return $assoc; } Link to comment https://forums.phpfreaks.com/topic/102661-need-help-with-code/#findComment-526326 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.