DOMDocument - 从php:// input直接加载XML文件

时间:2013-08-11 10:50:05

标签: php

目前我有一个PHP文件,它读取发布的XML,然后将其转换/输出到JSON。该文件如下所示:

<?php 

file_put_contents('myxmlfile.xml', file_get_contents('php://input'));
$xmldoc = new DOMDocument();
$xmldoc->load("myxmlfile.xml");
$xpathvar = new DOMXPath($xmldoc);

// Etc etc, for the purpose of my question seeing the rest isn't necessary
// After finishing the conversion I save the file as a JSON file.

file_put_contents('myjsonfile.json', $JSONContent);

?>

我收到的数据是XML格式。要转换它,我目前正在将其保存为XML文件,然后在创建新的DOMDocument()并加载后立即将其保存。我的问题是,有什么方法可以切断中间人并加载XML直接使用file_get_contents()

理想情况下会是这样(不起作用):

$xmldoc->load(file_get_contents("php://input"));

如果有人能帮助我这样做,我真的很感激!

由于

1 个答案:

答案 0 :(得分:1)

要从字符串加载而不是文件名,请使用loadXML方法。

$xmldoc->loadXML(file_get_contents("php://input"));