Jump to content

SaveXML from simpleXML


ameliabob

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 comment
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 comment
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 comment
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 comment
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 comment
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 comment
Share on other sites

  • 7 months later...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...