cmb Posted October 14, 2012 Share Posted October 14, 2012 In searching how to wright cdata tags into my xml document using simpleXML i came across this example and when i try and use it Dreamweaver shows that lines 4-7 are wrong i try and run the code anyway and i get this error syntax error, unexpected T_VARIABLE on line 4 <?php class SimpleXMLExtended extends SimpleXMLElement{ public function addCData($cdata_text){ $node= dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata_text)); } } $xml_doc = new SimpleXMLExtended('save.xml',NULL,TRUE); $email = $xml_doc->addChild('email'); $email->addAttribute('id',$id); $email->from = NULL; $email->from->addCData($from); $xml_doc->asXML('save.xml'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/ Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2012 Share Posted October 14, 2012 What do you get as output from the following php code - <?php echo 'Current PHP version: ' . phpversion(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385081 Share on other sites More sharing options...
cmb Posted October 14, 2012 Author Share Posted October 14, 2012 (edited) 5.3.5 Edited October 14, 2012 by cmb Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385083 Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2012 Share Posted October 14, 2012 If I remember correctly, that error occurs when you use php5 OOP syntax under php4. If you remove the public keyword and that php syntax error goes a way, then it means that where or how you are running that php file is actually using php4. If you ran that php code 'inside' your IDE, then it is probably using the command line version of php on your computer, not the installation of php that your web server is using. Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385086 Share on other sites More sharing options...
cmb Posted October 14, 2012 Author Share Posted October 14, 2012 I tried removing public and it still didn't work. I then uploaded it to my server and tried it there with public and without public and it still didn't work Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385132 Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2012 Share Posted October 14, 2012 Telling us something didn't work is kind of pointless because we are not standing right next to you and don't know what you saw in front of you. In programming, you have to be specific about what did occur, even if what occurred was a blank page, because that helps to pin down what execution path the code took or didn't take. I tried your code too, using php5.4.x and it worked for me, after I created a save.xml file and defined $id and $email variables for it to use. AFAIK, there's nothing that is php5.4 specific in that code, so it should work under php5.3. Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385134 Share on other sites More sharing options...
cmb Posted October 14, 2012 Author Share Posted October 14, 2012 (edited) I got the same syntax error, unexpected T_VARIABLE. This is my full code with most of it commented out at the moment because i'm trying to get this to work <?php $is_ajax = 1;//$_REQUEST['is_ajax']; if(isset($is_ajax) && $is_ajax){ $id = "test3";//$_REQUEST['id']; $from = "cmbswim@gmail.com";//"<![CDATA[".$_REQUEST['from']."]]>" ; $to = "";//"<![CDATA[".$_REQUEST['to']."]]>"; $bcc = "test1@localhost,test2@localhost";//"<![CDATA[".$_REQUEST['bcc']."]]>" ; $subject = "test 3";//$_REQUEST['subject'] ; $msg = "this is the third test";//"<![CDATA[".$_REQUEST['msg']."]]>" ; class SimpleXMLExtended extends SimpleXMLElement{ public function addCData($cdata_text){ $node= dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata_text)); } } $xml_doc = new SimpleXMLExtended('save.xml',NULL,TRUE); $email = $xml_doc->addChild('email'); $email->addAttribute('id',$id); $email->from = NULL; $email->from->addCData($from); //$email->addChild('from', $from); // $email->addChild('to', $to); // $email->addchild('bcc', $bcc); // $email->addChild('subject', $subject); // $email->addChild('msg', $msg); $xml_doc->asXML('save.xml'); //echo "Success!!!"; } ?> Edited October 14, 2012 by cmb Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385138 Share on other sites More sharing options...
Barand Posted October 14, 2012 Share Posted October 14, 2012 (edited) I ran it too - no errors Current PHP version: 5.3.10. Started with <?xml version="1.0" encoding="utf-8"?> <data> </data> Ran <?php class SimpleXMLExtended extends SimpleXMLElement{ public function addCData($cdata_text){ $node= dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata_text)); } } $id = 99; $from = 'me@gmail.com'; $xml_doc = new SimpleXMLExtended('save.xml',NULL,TRUE); $email = $xml_doc->addChild('email'); $email->addAttribute('id',$id); $email->from = NULL; $email->from->addCData($from); $xml_doc->asXML('save.xml'); ?> Ended with <?xml version="1.0" encoding="utf-8"?> <data> <email id="99"> <from><![CDATA[me@gmail.com]]></from> </email> </data> Edited October 14, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385141 Share on other sites More sharing options...
cmb Posted October 14, 2012 Author Share Posted October 14, 2012 I removed the indention on the lines and now it works class SimpleXMLExtended extends SimpleXMLElement{ public function addCData($cdata_text){ $node= dom_import_simplexml($this); $no = $node->ownerDocument; $node->appendChild($no->createCDATASection($cdata_text)); } } Quote Link to comment https://forums.phpfreaks.com/topic/269440-simplexml/#findComment-1385142 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.