关于php保存文件的问题
我使用以下代码在 php 中执行 XSLT:
# 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") ;
代码非常简单,我们需要加载源 xml 文件,然后加载样式表,而且它确实有效。
导致麻烦的代码是最后一行:
print $XML->save("newfile.xml") ;
运行后出现错误“致命错误:调用未定义的方法 SimpleXMLElement::save() ”。但实际上,我在这里遵循了一个教程: http://devzone.zend.com/article/1713。
也许我搞砸了一些事情,有人可以给我提示吗?提前致谢。
按照你们的建议,我修改了这样的代码:
# 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 );
现在正确转换的 XML 显示在浏览器中,我尝试了一些方法,但仍然无法弄清楚如何将此结果打印到文件而不是显示在浏览器,感谢任何帮助,提前致谢。
I've used the following code to do an 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 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您要更改
$XML
的定义方式,只需调用$XML
上的load
方法而不是simplexml_load_file
:根本没有理由使用
simplexml
因为 XSLT 处理都是通过 DOMDocument 完成的。所以只要替换掉那一行,你就可以开始了......You're changing how
$XML
is defined, simply call theload
method on$XML
instead ofsimplexml_load_file
: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...首先,将
DOMDocument
存储在$XML
中,然后将其替换为SimpleXMLElement
。DOMDocument
确实有save
方法,但SimpleXMLElement
没有。承认:没有看教程,所以我不知道为什么/它是否有效。
First you store a
DOMDocument
in$XML
, and then you replace it with aSimpleXMLElement
.DOMDocument
does have asave
method, butSimpleXMLElement
does not.Admission: didn't look at the tutorial, so I don't know why/if that one works.
您说
$XML
是 DOMDocument 然后在线将其替换为 SimpleXMLElement 2使用
代替
You're saying that
$XML
is a DOMDocument and then you replace it with a SimpleXMLElement on line 2Use
instead
问题:
您创建一个 DOMDocument,然后用 SimpleXMLElement 对象覆盖它。第一行是死代码。您根本没有使用它,因为您在下一条语句中覆盖了它。
save
是DOMDocument
中的一个方法。asXML($file)
相当于 SimpleXML(或saveXML($file)
,它是一个别名。如果你看一下教程,很明显:
所以,如果你使用
simplexml_load_file
,那么您就没有真正遵循本教程。Problem:
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 inDOMDocument
.asXML($file)
is the equivalent for SimpleXML (orsaveXML($file)
which is an alias.If you look at the tutorial, it's clearly:
So, if you use
simplexml_load_file
, then you're not really following the tutorial.