简体   繁体   English

如何将xml文件附加到php变量

[英]How to attach an xml file to php variable

i have been created an xml file and named it 'input.xml'. 我已经创建了一个xml文件,并将其命名为“ input.xml”。 When i'm trying to add a file (attach to string) in php, i get 500 browser error. 当我尝试在php中添加文件(附加到字符串)时,出现500个浏览器错误。


This is how i importing my xml to variable (working way): 这就是我将xml导入变量的方式(工作方式):

 $xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>Movie 1</title>
 </movie>

  <movie>
  <title>Movie 2</title>
 </movie>

 <categories>
  <category>
    <name>Example category</name>
  </category>
 </categories>
</movies>
XML;

This is how i want to import my xml (from input.xml file) (not working way): 这就是我要导入XML(从input.xml文件)的方式(不起作用):

 $xmlstr = simplexml_load_file("input.xml");

This is how my php code looks like: 这就是我的php代码的样子:

$movies = new SimpleXMLElement($xmlstr);

foreach ($movies->movie as $mov) {
   echo "We found a movie!<br>";
}

foreach ($movies->categories as $cat) {
    echo "We found a category!<br>";
}

And this is my input.xml file content: 这是我的input.xml文件的内容:

<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>Movie 1</title>
 </movie>

  <movie>
  <title>Movie 2</title>
 </movie>

 <categories>
  <category>
    <name>Example category</name>
  </category>
 </categories>
</movies>

simplexml_load_file already returns a SimpleXMLElement for you. simplexml_load_file已经为您返回了SimpleXMLElement So, there's no need to use 因此,无需使用

$movies = new SimpleXMLElement($xmlstr);

Just: 只是:

$movies = simplexml_load_file("input.xml");

foreach ($movies->movie as $mov) {
   echo "We found a movie!<br>";
}

foreach ($movies->categories as $cat) {
    echo "We found a category!<br>";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM