0

I'm parsing a file with simple_xml_load_file(), level by level. Here's the sample structure:

<person name="Joe Smith" ...> <info age="19"> <height val="1.85" /> </info> <info age="19"> <weight val="82" /> </info> <info age="19"> <build val="14" /> </info> </person> ... 

As I am parsing I am not going deep, as I don't need to. I do need the age however, without going through each info tag. I need the variables contained in <person> and only the age. How would I go about getting age without another loop?

$persons=$dom->person; foreach($persons as $person){ $name=$person['name']; $age=???? } 

1 Answer 1

3

This should do the job:

foreach($dom->person as $person){ $name=$person['name']; foreach($person->info as $info) { echo $info['age'] . '<br>'; } } 

Or if you want to get one age at a specific position:

echo $person->info[0]['age']; // Gets age attribute of first <info> node 
Sign up to request clarification or add additional context in comments.

1 Comment

any other way of doing it without another loop? there are tons of info tags and I am afraid it would slow the performance in case of a large file.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.