Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse XML subitems using foreach()

Tags:

foreach

php

xml

I have an XML file that needs parsing in PHP. I am currenlty using simplexml_load_file to load the file like so:

$xml = simplexml_load_file('Project.xml');

Inside that XML file lies a structure like this:

<?xml version="1.0" encoding="ISO-8859-1"?>
<project>
<name>Project 1</name>
<features>
    <feature>Feature 1</feature>
    <feature>Feature 2</feature>
    <feature>Feature 3</feature>
    <feature>Feature 4</feature>
</features>
</project>

What I am trying to do is print the <name> contents along with EACH <feature> element within <features>. I'm not sure how to do this because there is more than 1 element called <feature>. Any help is greatly appreciated.

like image 248
user Avatar asked Dec 22 '22 03:12

user


1 Answers

to print the text, try this :

foreach($xml->features->feature as $key => $value)
{
    echo $value;
}
like image 166
Puaka Avatar answered Jan 11 '23 03:01

Puaka