randomresult randomresult - 1 month ago 19
PHP Question

Accessing Attributes in xml

i have the following XML :

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3FlexForms>
<data>
<sheet index="Slider01">
<language index="lDEF">
<field index="title">
<value index="vDEF">Das ist die Headline von Slider 01</value>
</field>
<field index="text">
<value index="vDEF">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</value>
<value index="_TRANSFORM_vDEF.vDEFbase">Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.</value>
</field>
<field index="image">
<value index="vDEF">slide01.jpg</value>
</field>
<field index="position">
<value index="vDEF">left</value>
</field>
</language>
</sheet>
</data>




Now i use the following php

$xml = simplexml_load_file('Slider01.xml');
var_dump($xml->data->sheet->language->field[0]);


This gives me the following:

object(SimpleXMLElement)#4 (2) {
["@attributes"]=>
array(1) {
["index"]=>
string(5) "title"
}
["value"]=>
string(34) "Das ist die Headline von Slider 01"
}


Now i want to have a variable which contains the headline " Das ist die Headline von Slider 01" and therefor i need to access the attribute -> title -> value but i cant get it to work.
So how do i access the value of title?

Do you need further informations?

Thx for the help.

Answer

From the output of var_dump you can see that $xml->data->sheet->language->field[0] is an instance of SimpleXMLElement as well as the previous properties (data, sheet, and language). So you can access value in the same way: $xml->data->sheet->language->field[0]->value.

The value returned by $xml->data->sheet->language->field[0]->value is an instance of SimpleXMLElement. The class implements __toString method, which means that you can cast it to string as follows:

$xml = simplexml_load_file('Slider01.xml');
$title = (string)$xml->data->sheet->language->field[0]->value;

The string representation of the instance is a pure text value.