在PHP中读取XML文件

问题描述:

I originally had a php file pulling in data from another php file. However, I have to change the second php file to an xml file. My question is, how do I read the string between certain tags in the php file using SimpleXML. For Example, here is my XML file:

<?xml version="1.0" encoding="UTF-8"?>

<XMLExample>
  <Name>ExampleName</Name>
  <Info>ExampleInfo</Info>
  <KMLFile>ExampleKML</KMLFile>
</XMLExample>

I previously had this in my php file:

if(strlen($KMLFile)>0){
#echo "<a id=\"links\" href=\"/$place/Area/$KMLFile\">KML File</a> 
";
echo "<a id=\"links\" href=\"/media/$place/markup/xml/$KMLFile\">KML File</a> 
";
}

What I can't figure out is how to change the php code to read the data from the XML file. Any help would be greatly appreciated! Thanks!

我最初有一个php文件从另一个php文件中提取数据。 但是,我必须将第二个php文件更改为xml文件。 我的问题是,如何使用SimpleXML读取php文件中某些标签之间的字符串。 例如,这是我的XML文件: p>

 &lt;?xml version =“1.0”encoding =“UTF-8”?&gt; 
 
&lt; XMLExample&gt; \  n&lt; Name&gt; ExampleName&lt; / Name&gt; 
&lt; Info&gt; ExampleInfo&lt; / Info&gt; 
&lt; KMLFile&gt; ExampleKML&lt; / KMLFile&gt; 
&lt; / XMLExample&gt; 
  code>  pre> 
  
 

我之前在我的php文件中有这个: p>

  if(strlen($ KMLFile)&gt; 0){
#echo“&lt; a id =  \“links \”href = \“/ $ place / Area / $ KMLFile \”&gt; KML文件&lt; / a&gt; 
“; 
echo”&lt; a id = \“links \”href = \“/ media  / $ place / markup / xml / $ KMLFile \“&gt; KML文件&lt; / a&gt; 
”; 
} 
  code>  pre> 
 
 

我无法想象 out是如何更改php代码以从XML文件中读取数据。 任何帮助将不胜感激! 谢谢! p> div>

Use this code. note.xml is your .xml file.

<?php
    $xml=simplexml_load_file("note.xml") or die("Error: Cannot create object");
    echo $xml->Name.'<br>';
    echo $xml->Info.'<br>';
    echo $xml->KMLFile;
?>