0

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?

Edited by RikTelner

2
Contributors
4
Replies
16
Views
3 Years
Discussion Span
Last Post by RikTelner
1

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.

Votes + Comments
Finally someone who understands that question and answers correctly.
0

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

etc. ?

Edited by RikTelner

1

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

print_r($file);
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.