关于php保存文件的问题

发布于 2024-10-09 02:46:03 字数 1194 浏览 6 评论 0原文

我使用以下代码在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

短暂陪伴 2024-10-16 02:46:03

您要更改 $XML 的定义方式,只需调用 $XML 上的 load 方法而不是 simplexml_load_file

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

根本没有理由使用 simplexml因为 XSLT 处理都是通过 DOMDocument 完成的。所以只要替换掉那一行,你就可以开始了......

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...

素手挽清风 2024-10-16 02:46:03
$XML = new DOMDocument(); 
$XML = simplexml_load_file("images/upload/source.xml");

首先,将 DOMDocument 存储在 $XML 中,然后将其替换为 SimpleXMLElementDOMDocument 确实有 save 方法,但 SimpleXMLElement 没有。

承认:没有看教程,所以我不知道为什么/它是否有效。

$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.

七秒鱼° 2024-10-16 02:46:03
$XML = new DOMDocument(); 
$XML = simplexml_load_file("images/upload/source.xml");

$XMLDOMDocument 然后在线将其替换为 SimpleXMLElement 2

使用

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

代替

$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

饭团 2024-10-16 02:46:03

问题:

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

您创建一个 DOMDocument,然后用 SimpleXMLElement 对象覆盖它。第一行是死代码。您根本没有使用它,因为您在下一条语句中覆盖了它。

saveDOMDocument 中的一个方法。 asXML($file) 相当于 SimpleXML(或 saveXML($file) ,它是一个别名。

如果你看一下教程,很明显:

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

所以,如果你使用 simplexml_load_file,那么您就没有真正遵循本教程。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文