ameliabob 3 Posted September 22, 2008 Report Share Posted September 22, 2008 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? Quote Link to post Share on other sites
boen_robot 107 Posted September 22, 2008 Report Share Posted September 22, 2008 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). Quote Link to post Share on other sites
ameliabob 3 Posted September 22, 2008 Author Report Share Posted September 22, 2008 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 Quote Link to post Share on other sites
boen_robot 107 Posted September 22, 2008 Report Share Posted September 22, 2008 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');?> Quote Link to post Share on other sites
ameliabob 3 Posted September 22, 2008 Author Report Share Posted September 22, 2008 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'); Quote Link to post Share on other sites
boen_robot 107 Posted September 22, 2008 Report Share Posted September 22, 2008 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')!!! Quote Link to post Share on other sites
skaterdav85 12 Posted April 27, 2009 Report Share Posted April 27, 2009 If php5 is installed on a web hosting server, is SimpleXML also installed? Quote Link to post Share on other sites
justsomeguy 1,135 Posted April 27, 2009 Report Share Posted April 27, 2009 The SimpleXML extension is enabled in PHP 5 by default. Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.