Question about saving file in php

I used the following code for XSLT in php:

# LOAD XML FILE $XML = new DOMDocument(); $XML = simplexml_load_file("images/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 pretty simple, we need to download the source xml file and then load the stylesheet, and indeed, it really works.

The code causing the problem is the last line:

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

after starting, in which I received the error "Fatal error: calling the undefined method SimpleXMLElement :: save ()". But, in fact, I followed the tutorial: http://devzone.zend.com/article/1713 .

Maybe I screwed up something, can someone tell me? thanks in advance.

Following your guys' advice, I changed the code as follows:

  # 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 correctly converted XML is displayed in the browser, I tried several ways, but still could not understand how to print this result in a file, and not show in the browser, any help is welcome, thanks in advance.

+4
source share
4 answers

You change the way you define $XML , just call the load method of $XML instead of simplexml_load_file :

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

There is no reason to use simplexml , because XSLT processing is done using DOMDocument. So just replace this line and you should be good to go ...

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

First, you save the DOMDocument to $XML , and then replace it with SimpleXMLElement . DOMDocument has a save method, but SimpleXMLElement does not work.

Input: did not look at the tutorial, so I do not know why / if it works.

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

You say $XML is a DOMDocument , and then you replace it with SimpleXMLElement in line 2

Using

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

instead

+1
source

Problem:

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

A DOMDocument is created, which is then overwritten using the SimpleXMLElement object. The first line is the dead code. You are not using it at all, as you are rewriting it in the following statement.

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

If you look at the tutorial, it’s clear:

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

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

0
source

Source: https://habr.com/ru/post/1333211/


All Articles