Resource: http://www.w3schools.com/php/php_xml_simplexml.asp
I wasn't able to Google it since I don't know how to form question.

So lets say the xml file is

<?xml version="1.0" ?>
    <major>
        <point>Tower</point>
    </major>
</xml>

I can retrieve it by using PHP:

<?php
    $file = simplexml_load_file("file.xml");
    echo "Well, the point in on ". $file->point .".";
?>

But how do I retrieve it when xml is:

<?xml version="1.0" ?>
    <major>
        <id>1</id>
        <point>Tower</point>
    </major>
    <major>
        <id>2</id>
        <point>Castle</point>
    </major>
</xml>

How to say give me $file->point with id 1?

Recommended Answers

All 4 Replies

The second xml file is not correct, here's the fixed version:

<?xml version="1.0" ?>
<sys>
    <major>
        <id>1</id>
        <point>Tower</point>
    </major>
    <major>
        <id>2</id>
        <point>Castle</point>
    </major>
</sys>

Now, you basically get an object array, so:

echo $file->major[0]->id . ' ' . $file->major[0]->point;

And if you loop it:

foreach($file as $key => $value)
{
    if($value->id == 1) echo $value->point;
}

You get the same results.

commented: Finally someone who understands that question and answers correctly. +0

major[0] defines which?
0 means first
1 means second
2 means third

etc. ?

Yes, in the arrays you start to count from zero, you can see all the structure by using print_r():

print_r($file);

Thank you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.