Jump to content

Recommended Posts

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');
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by cmb
Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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)); 
} 
} 

Link to comment
Share on other sites

This thread is more than a year old. Are you sure you have something important to add to it?

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.