开发者

file get contents extracting certain parts?

I have an xml document that is generated based on what the parametres are in the URL

for example:

menu.php?category=clothing

This wi开发者_开发技巧ll generate an xml page.

Now I want to display this in a formatted way on the menu, after having a look it seems that:

file_get_contents() seems to be the best option.

But I was just wondering how I can place elements and attributes found in this xml into the html code?

Any tips/help would be hugely appreciated!


One way to do it is to use PHP's SimpleXML:

Tutorial on SimpleXML


Simple load as a string or a file:

$source = 'mydata.xml';

// load as string
$xmlstr = file_get_contents($source);
$parseXML = new SimpleXMLElement($xmlstr);
print($parseXML);

// load as file
$parseXMLFile = new SimpleXMLElement($source,null,true);

print_r($parseXMLFile);

Let's say this is your xml file:

<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
 </movie>
</movies>

Doing this would give you the title:

$parseXMLFile = new SimpleXMLElement($source,null,true);
echo $parseXMLFile->movie[0]->title;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜