0

i have some data in xml file agent.xml

<?xml version="1.0" encoding="ISO-8859-1"?>
<agents>
    <agent>
    <image> img/sara.jpg</image>
    <id>1</id>
    <name>Tommy Jenkin</name>
    <company>CJenkins Insurance</company>
    <street>Insurance150 S State Stree</street>
    <city>Linkend</city>
    <phone>(773) 561-4331</phone>
    </agent>
    <agent>
    <image> img/primary-nav-logo.png</image>
    <id>2</id>
    <name>saka</name>
    <company>LIC Insurance</company>
    <street>Insurance150 S State Stree</street>
    <city>Linkend</city>
    <phone>(773) 561-4331</phone>
    </agent>
</agents>

Now i have one variable called id in php like below

$id = 1

Now i am displaying this xml data with following code

 <?php
                $xml = simplexml_load_file("agent.xml") or die("Error: Cannot create object");
                foreach($xml as $agent){
                function processXML($node){
                    foreach($node->children() as $agent => $data){
                        $agent= trim($agent);   
                        if($agent=='image')
                        {
                            echo '<div><img src="'.$data.'" ></div>';
                            echo '<div>';
                            echo '</div>';
                        }
                        elseif($agent=='id')
                        {
                             echo '<div class = "Left">';
                             echo '<input type = "button" name="Agent" id = "'.$data.'" class = "subs-btn" value = "Select this Agent" OnClick = Selected(this.id);>';
                             echo '</div>';
                             echo '<br/>';
                             echo '<br/>';  
                        }
                        else
                        {
                            echo '<div class = "inline1">';
                            echo $data;
                            echo '</div>';
                            echo '<br/>';

                        }
                            processXML($data);
                        }           

                    }  
                processXML($xml);
                }
            ?>

But need a data from xml file having id = 1 so how can i compare it and display it please help

1 Answer 1

1

use xpath to select the <agent> with the desired <id>:

$agent = $xml->xpath("/agents/agent[id='1']");

if (isset($agent[0])) $agent = $agent[0]; else $agent = NULL;
echo $agent->name;
Sign up to request clarification or add additional context in comments.

2 Comments

Can you please tell me in my code where and how can i use it because i had try your code but it will get blank page
@SanjayRathod Sorry, I can't write or review your code. SO is for getting ansers to specific questions, not for code review. Why not try for yourself? If specific questions come up, feel free to write a question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.