Jump to content

Use heredoc to form XML?


Recommended Posts

I'm a novice to PHP. What I want to do is add a new customer record to my XML database. It looks as if a simple way to do this would be to create a string using heredoc, which will insert the variable values, then turn it into a simpleXML object:

	$xml=simplexml_load_file("Test data.xml"); //load XML file
	// these $vars are all defined from form input in previous code
$str= <<<EOS	

	$new = new SimpleXMLElement($str);	//turn $str into SimpleXML element
	$xml->addChild("customer",$new);	//add it to DB

My first question is whether DreamWeaver CS5 will allow me to do this. It gives me the "syntax error on line 1." Is it able to recognize heredoc?

Second, am I approaching this the right  way? I'm sure this is something that is done regularly for myriad websites, but I haven't found any clear direction on the best way to handle it.

Finally, if this is the right approach, what am I missing to make it work?

Link to comment
Share on other sites

I guess DreamWeaver doesn't understand the heredoc syntax. Try to find a way to make it ignore the error and then run the code on a PHP server.

I've never used DreamWeaver. For many years it a bad reputation because its preview tool was very wrong, so most developers steered clear of it. I assume by now they've fixed that, but since there are hundreds of free alternatives I don't have a reason to pay for it, especially now that it's a continuous subscription service instead of a one time purchase.

Link to comment
Share on other sites

Okay, I'll hunt for a DW replacement. It has to run on OS10.11.6. Any recommendations?
Meanwhile, I can upload and run the code regardless of the error message, but it doesn't seem to work. I was wondering if you could see a reason why. Is this a reasonable way to approach my goal? Some of the discussions I've read seem to imply I have to insert each tag separately, but I can't tell for sure.

Edited by Jay@TastefulTitles.com
Link to comment
Share on other sites

I usually use DOMDocument instead of SimpleXML, so I would have to read the documentation. I would look up SimpleXML on the manual at php.net to see if they have examples of loading documents from a string.

Link to comment
Share on other sites

This is an example of loading a document from a string using DOMDocument: https://www.php.net/manual/en/domdocument.loadxml.php

It looks like for SimpleXML, you can use simplexml_load_string(): https://www.php.net/manual/en/function.simplexml-load-string.php

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...