0

I'm trying to return the value of the typeName key inside this object (xml).

[geometricProperty] => Array
    (
        [0] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [typeName] => localizacao  // < - - - This is the value I need.
                    )

                [Point] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [ID] => swrefVgetZredeVcollX757678785
                                [srs] => ut_cm
                            )

                        [coordinates] => 38871,739716 
                    )

            )

        [1] => SimpleXMLElement Object
            (
                [@attributes] => Array
                    (
                        [typeName] => limite
                    )

                [Polygon] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [ID] => swrefVgeometryV08945038
                                [srs] => utm_23_cm
                            )

                        [outerBoundaryIs] => SimpleXMLElement Object
                            (
                                [LinearRing] => SimpleXMLElement Object
                                    (
                                        [coordinates] => 318950,7399585 31981,39650 31826,73990 316956,79750 
                                    )

                            )

                    )

            )

...

I've tried the following but it returns NULL

echo "<p>This object has ".(count($n->Feature->geometricProperty))." items.</p>";
$c=0;
foreach($n->Feature->geometricProperty as $k){
    $c++;
    echo "<p>$c)".(gettype($k))."</p>";

    foreach(array_keys(get_object_vars($k)) as $o){ 
        echo "<p>$o</p>";

        switch($o){
            case "@attributes": var_dump($k->{$o}["typeName"]); $tipo=$k->{$o}["typeName"]; break;
            case "Point": $value=$k->{$o}->coordinates; break;
            case "Polygon": $value=$k->{$o}->outerBoundaryIs->LinearRing->coordinates; break;
            case "Line...": break;
        }
    }

    echo "<p>TIPO: $tipo</p><p>VALOR: $value</p>";

}

OUTPUT:

This object has 3 items.

1)object

@attributes
NULL

Point

TIPO:

VALOR: 31869871,739796106

2)object

@attributes
NULL

Polygon

TIPO: 

Search and found this overflow question and can't guess what am I doing wrong.

1 Answer 1

1

You can use SimpleXmlElement::attributes() to get an array of attributes. So then you would just loop over them and display them or use implode or something... depending on the output you want.

Sign up to request clarification or add additional context in comments.

2 Comments

Nice! So the code should be $tipo=$k["typename"], since it's a direct attribute of the object. Note that I couldn't find any reference that @attribute is a sort of reserved keyword on simpleXML.
Yes but i beleive there are gotchas there if the namespace of the element is different from the namespace of the attribute. Doesnt seem you have xml using namespaces though but keep that in mind. Also if you dont know the attributes or they vary then you may need to use the function.

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.