-3

I want to read a value constant from XML in PHP code.

I already tried this: $equipment->constant->name of value but is not working

<const name="IAn" value="500" um="" />
<const name="IBn" value="500" um="" />
<const name="ICn" value="500" um="" />
halfer
  • 19,824
  • 17
  • 99
  • 186
  • You can use `simplexml_load_file()` function to load your xml and the you can access it. `$xmldata = simplexml_load_file(path_of_your_xml_file); $xmldata[0]->IAn` – danish-khan-I May 17 '19 at 04:58

2 Answers2

1

You can read about reading XML here: https://www.php.net/manual/en/book.simplexml.php

$xmlstr = '
    <consts>
        <const name="IAn" value="500" um="" />
        <const name="IBn" value="500" um="" />
        <const name="ICn" value="500" um="" />
    </consts>
';

In your example, you need to realize that the name attribute may not be unique. You could access ALL the elements that have the name attribute value you are looking for. Or maybe you just want the first one.

$consts = new SimpleXMLElement($xmlstr);
foreach($consts as $elem) {
    if ( $elem['name'] == 'IAn' ) {
       echo $elem['value'] . "\n";
    }
}
ryantxr
  • 4,119
  • 1
  • 11
  • 25
  • Hello again, is not working for my application. I have 1 x xml file and in xml i have 2 equipemnts in same power plant. I need to read 2 var (info) from the equipments like this. if type machine is AT, do something if type machine is BC, do something. – Andrei Bădiţă Sep 02 '19 at 07:38
0

There are lots of ways for this.

You can use simplexml_load_string method (if content is a string) or simplexml_load_file(if the content is in a file).

After reading the content you can loop it.