0


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.

rain
  • 3
  • 6
  • 1
    Add the code to the question, not as an image. – M. Eriksson Apr 20 '17 at 20:15
  • You might also want to tell us what the actual issue is. What happens when you run your code? – M. Eriksson Apr 20 '17 at 20:16
  • Not able to add the code, it is getting omitted. – rain Apr 20 '17 at 20:17
  • The code in image gives error because its incomplete. I'm able to display the output if the emphasis tags aren't there. Otherwise not able to understand how to display this output as in second employee tag the emphasis tags are not there. – rain Apr 20 '17 at 20:20
  • 1
    `strip_tags($employee->name->asXml())` - https://eval.in/779841 – splash58 Apr 20 '17 at 20:33
  • Possible duplicate of [How do you parse and process HTML/XML in PHP?](http://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php) – ThW Apr 21 '17 at 08:45

1 Answers1

0

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>';
}
Lou
  • 866
  • 5
  • 14