Jump to content

SaveXML from simpleXML


Recommended Posts

I have added the last line to the example in the tutorial and I get the following error.Catchable fatal error: Argument 1 passed to DOMDocument::saveXML() must be an instance of DOMNode, string given,

	$sxe = simplexml_load_string('<books><book><title>blah</title></book></books>');	if ($sxe === false) {    	echo 'Error while parsing the document';    	exit;	}	$dom_sxe = dom_import_simplexml($sxe);	if (!$dom_sxe) {    	echo 'Error while converting XML';    	exit;	}	$dom = new DOMDocument('1.0');	$dom_sxe = $dom->importNode($dom_sxe, true);	$dom_sxe = $dom->appendChild($dom_sxe);	$dom->saveXML("/sample.xml");

Also is there a variation of var_dump that I could use to show all of $dom?

Link to post
Share on other sites

saveXML() outputs the current DOM tree as a string (pretty much the var_dump variation you're looking for). save() saves it to a file you specify.So replace:

$dom->saveXML("/sample.xml");

with

$dom->save("/sample.xml");

Though to be honest, I think you have more problems than that (logical ones - can't really suggest anything without knowing your goal).

Link to post
Share on other sites
saveXML() outputs the current DOM tree as a string (pretty much the var_dump variation you're looking for). save() saves it to a file you specify.So replace:
$dom->saveXML("/sample.xml");

with

$dom->save("/sample.xml");

Though to be honest, I think you have more problems than that (logical ones - can't really suggest anything without knowing your goal).

I am trying to find an easy way to get an XML file, process it and write it back. simplexml seems to be the easiest way to get and process it. However, there is no way to either write the file back to disk or transform it in simplexml. So I was looking for a way to get from simple back to DOM so that these things could be done.so I would write something like
Link to post
Share on other sites

You're confusing the languages. PHP uses "->" to access class members, not ".". All variables must have "$" in front of their names. There's no "Response.Write()" nor "transformNode()" method. There are just "echo" and "transformTo*()" methods respectively.The simplest way to execute an XSLT transformation and save the result back to file is:

<?php$proc = new XSLTProcessor;$proc->importStylesheet(DOMDocument::load('/XLSFile.xsl'));$proc->transformToURI(DOMDocument::load('/sample.xml'), '/output.xml');?>

Where '/output.xml' is the file in which the result would go. You can of course specify '/sample.xml' instead, and in that case, '/sample.xml' will be overwritten with the result, and executing the transformation again would make XSLT execute over the latest XML.If you want to use SimpleXML to do other transformations first, you can do so, as long as you convert the object into DOMDocument upon transformation. For example:

<?php$sxe = new SimpleXMLElement('/sample.xml');//Do SimpleXML editions here over the $sxe object$proc = new XSLTProcessor;$proc->importStylesheet(DOMDocument::load('/XLSFile.xsl'));$proc->transformToURI(dom_import_simplexml($sxe), '/output.xml');?>

Link to post
Share on other sites
You're confusing the languages. PHP uses "->" to access class members, not ".". All variables must have "$" in front of their names. There's no "Response.Write()" nor "transformNode()" method. There are just "echo" and "transformTo*()" methods respectively.The simplest way to execute an XSLT transformation and save the result back to file is:
<?php$proc = new XSLTProcessor;$proc->importStylesheet(DOMDocument::load('/XLSFile.xsl'));$proc->transformToURI(DOMDocument::load('/sample.xml'), '/output.xml');?>

Where '/output.xml' is the file in which the result would go. You can of course specify '/sample.xml' instead, and in that case, '/sample.xml' will be overwritten with the result, and executing the transformation again would make XSLT execute over the latest XML.If you want to use SimpleXML to do other transformations first, you can do so, as long as you convert the object into DOMDocument upon transformation. For example:

<?php$sxe = new SimpleXMLElement('/sample.xml');//Do SimpleXML editions here over the $sxe object$proc = new XSLTProcessor;$proc->importStylesheet(DOMDocument::load('/XLSFile.xsl'));$proc->transformToURI(dom_import_simplexml($sxe), '/output.xml');?>

And if I just wanted to write the file without doing any transform to it would I then just somehow change it to a DOM and do a $dom.save('output.xml');
Link to post
Share on other sites

Uh, you don't need to. You simply output the contents and save it with file_put_contents, like:

<?php$sxe = new SimpleXMLElement('/sample.xml', null, true);//Do SimpleXML editions on $sxe herefile_put_contents('output.xml', $sxe->asXML());?>

AND PLEASE FORGET ABOUT $dom.save('output.xml')!!!! It's $dom->save('output.xml')!!!

Link to post
Share on other sites
  • 7 months later...

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