xml解析到php


I have just started learning xml parsing.
My xml file looks like -

<company>
    <employee>
        <name>Employee <emphasis emph='em01'>John <emphasis emph='em01'>is great</emphasis></emphasis>.</name>
        <age>25</age>
        <dept>Computer</dept>
    </employee>
    <employee>
        <name>Jessica</name>
        <age>25</age>
        <dept>Human Resource</dept>
    </employee>
</company>

I want to display this in browser. I'm able to write code for basic parsing but this tag inside tag seems complicated. Please help. I want the output as -

Employee John is great.
25
Computer
Jessica
25
Human Resource

My basic php code looks like this.

Request help. Thanks.

The XML you provided is invalid :

<employee>
        <name>Employee <emphasis emph='em01'>John <emphasis emph='em01'>is great<emphasis></emphasis>.</name>
        <age>25</age>
        <dept>Computer</dept>
</employee>

Here you have two opened :

<emphasis emph='em01'>John <emphasis emph='em01'>

And a third one here :

<emphasis></emphasis>.</name>

But you close only one and then you close name. You probably just misssed a slash.

Here's a working snippet resulting in your desired format :

<?php
$xml = simplexml_load_string("
<company>
    <employee>
        <name>Employee</name>
        <emphasis>John is great</emphasis>
        <age>25</age>
        <dept>Computer</dept>
    </employee>
    <employee>
        <name>Jessica</name>
        <emphasis></emphasis>
        <age>25</age>
        <dept>Human Resource</dept>
    </employee>
</company>
");

foreach ($xml->employee as $employee){
echo $employee->name . ' <em>' . $employee->emphasis . '</em>';
echo '<br>';
echo $employee->age;
echo '<br>';
echo $employee->dept;
echo '<br>';
}