Jump to content
Jay@TastefulTitles.com

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	
	<!---->
	<customer>
		<cId>$Customer</cId>
		<organization>$organization</organization>
		<website>$URL</website>
		<contact>$name</contact>
		<email>$email</email>
		<phone>$phone</phone>
		<addr>$address</addr>
		<city>$city</city>
		<state>$state</state>
		<zip>$zip</zip>
	</customer>
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?

Share this post


Link to post
Share on other sites

What is on line 1? The heredoc isn't on line 1.

Share this post


Link to post
Share on other sites

"Syntax error on line 1" is apparently how DW CS5 reports a syntax error anywhere in a PHP doc. The only thing on line 1 is "<?php". The error could be in line 12,345, and fixing it will make the error report go away. In this case, if I comment out the heredoc (lines 138-155), it reports "No syntax errors."

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...

×
×
  • Create New...