开发者

A question about saving a file in php

开发者 https://www.devze.com 2023-02-01 12:34 出处:网络
I\'ve used the following code to do an XSLT in php: # LOAD XML FILE $XML = new DOMDocument(); $XML = simplexml_load_file(\"images开发者_StackOverflow/upload/source.xml\");

I've used the following code to do an XSLT in php:

 # LOAD XML FILE 
 $XML = new DOMDocument(); 
 $XML = simplexml_load_file("images开发者_StackOverflow/upload/source.xml");

 # START XSLT 
 $xslt = new XSLTProcessor(); 
 $XSL = new DOMDocument();  
 $XSL->load( 'xsl/transfer.xsl', LIBXML_NOCDATA); 
 $xslt->importStylesheet( $XSL ); 

 #PRINT   
 print $XML->saveXML();
 print $XML->save("newfile.xml") ;

The code is quite straightforward, we need to load the source xml file and then load up the stylesheet, and indeed it actually works.

The code that causes trouble is the last line:

print $XML->save("newfile.xml") ;

after running which I got error "Fatal error: Call to undefined method SimpleXMLElement::save() ". But, actually ,I was following a tutorial here: http://devzone.zend.com/article/1713.

Maybe I screwed up something, could anybody give me a hint? thanks in advance.

Following your guys' advice, I modified the code like this:

 # LOAD XML FILE 
 $XML = new DOMDocument(); 
 $XML->load("images/upload/source.xml");

 # START XSLT 
 $xslt = new XSLTProcessor(); 
 $XSL = new DOMDocument();  
 $XSL->load( 'xsl/transfer.xsl', LIBXML_NOCDATA); 
 $xslt->importStylesheet( $XSL ); 

 #PRINT    
  print $xslt->transformToXML( $XML );

now the correctly-transformed XML gets shown in the browser, I've tried some ways but still couldn't figure out how to print this result to a file instead of showing in the browser, any help is appreciated, thanks in advance.


You're changing how $XML is defined, simply call the load method on $XML instead of simplexml_load_file:

$XML = new DOMDocument(); 
$XML->load("images/upload/source.xml");

There's no reason at all to use simplexml since the XSLT processing is all done with DOMDocument. So just replace that one line, and you should be good to go...


$XML = new DOMDocument(); 
$XML = simplexml_load_file("images/upload/source.xml");

First you store a DOMDocument in $XML, and then you replace it with a SimpleXMLElement. DOMDocument does have a save method, but SimpleXMLElement does not.

Admission: didn't look at the tutorial, so I don't know why/if that one works.


$XML = new DOMDocument(); 
$XML = simplexml_load_file("images/upload/source.xml");

You're saying that $XML is a DOMDocument and then you replace it with a SimpleXMLElement on line 2

Use

$XML = new DOMDocument(); 
$XML->load("images/upload/source.xml");

instead


Problem:

$XML = new DOMDocument(); 
$XML = simplexml_load_file("images/upload/source.xml");

You create a DOMDocument, which you then overwrite with a SimpleXMLElement object. The first line is dead code. You aren't using it at all, since you overwrite it in the next statement.

save is a method in DOMDocument. asXML($file) is the equivalent for SimpleXML (or saveXML($file) which is an alias.

If you look at the tutorial, it's clearly:

$xsl = new DomDocument();
$xsl->load("articles.xsl");
$inputdom = new DomDocument();
$inputdom->load("articles.xml");

So, if you use simplexml_load_file, then you're not really following the tutorial.

0

精彩评论

暂无评论...
验证码 换一张
取 消