S
S
Scorpic2019-06-17 15:24:08
PHP
Scorpic, 2019-06-17 15:24:08

How to get the attribute value of an XML element that must also be found by attribute?

Hello!
Tell me how to get from XML

<ElementList>
                                 <Element Isn="2235961003" Name="OSAGO_FR_CARCTG" Value="8244" Save="N"/>
                                 <Element Isn="2235961103" Name="OSAGO_FR_AGRPERIODM" Value="12" Save="N"/>
                                 <Element Isn="2235961203" Name="OSAGO_FR_USEPERIOD" Value="12" Save="N"/>
                                 <Element Isn="2235961303" Name="OSAGO_FR_MULTIDRIVE" Value="0" Save="N"/>
                                 <Element Isn="2235961403" Name="OSAGO_FR_TAXI" Value="1" Save="N"/>
                                 <Element Isn="2235961703" Name="OSAGO_FR_SBJTYPE" Value="N" Save="N"/>
                                 <Element Isn="2264076003" Name="OSAGO_FR_TARIFFZONE" Value="3401214003" Save="N"/>
                                 <Element Isn="2496138003" Name="FR_KE2" Value=".93" Save="Y"/>
                                 <Element Isn="2685741203" Name="KVS_OSAGO" Value=".93" Save="Y"/>
                                 <Element Isn="4655146503" Name="FR_USEWITHTRAILER" Value="1" Save="N"/>
                                 <Element Isn="761850500" Name="TRF_CL_BASE" Value="7399" Save="Y"/>
                                 <Element Isn="761851700" Name="TRF_CL_ZONE" Value="1.8" Save="Y"/>
                                 <Element Isn="761851800" Name="TRF_CL_BONUSMALUS" Value="1" Save="Y"/>
                                 <Element Isn="761854000" Name="TRF_CL_POWER" Value="1" Save="Y"/>
                                 <Element Isn="761854100" Name="TRF_CL_PERIOD" Value="1" Save="Y"/>
                                 <Element Isn="765909400" Name="TRF_CL_ABUSE" Value="1" Save="Y"/>
                                 <Element Isn="816328000" Name="TRF_CL_MULTIDRIVE" Value="1" Save="Y"/>
                              </ElementList>

get the attribute value of the element that you also need to find by the attribute in PHP
Name="TRF_CL_BASE" Value="7399" the value itself is 7399
Where to dig?
Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
AN, 2019-06-17
@Scorpic

Use:
simplexml_​load_​file
simplexml_​load_​string
The array will look like this:

SimpleXMLElement Object
(
    [Element] => Array
        (
            [0] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2235961003
                            [Name] => OSAGO_FR_CARCTG
                            [Value] => 8244
                            [Save] => N
                        )

                )

            [1] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2235961103
                            [Name] => OSAGO_FR_AGRPERIODM
                            [Value] => 12
                            [Save] => N
                        )

                )

            [2] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2235961203
                            [Name] => OSAGO_FR_USEPERIOD
                            [Value] => 12
                            [Save] => N
                        )

                )

            [3] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2235961303
                            [Name] => OSAGO_FR_MULTIDRIVE
                            [Value] => 0
                            [Save] => N
                        )

                )

            [4] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2235961403
                            [Name] => OSAGO_FR_TAXI
                            [Value] => 1
                            [Save] => N
                        )

                )

            [5] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2235961703
                            [Name] => OSAGO_FR_SBJTYPE
                            [Value] => N
                            [Save] => N
                        )

                )

            [6] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2264076003
                            [Name] => OSAGO_FR_TARIFFZONE
                            [Value] => 3401214003
                            [Save] => N
                        )

                )

            [7] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2496138003
                            [Name] => FR_KE2
                            [Value] => .93
                            [Save] => Y
                        )

                )

            [8] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 2685741203
                            [Name] => KVS_OSAGO
                            [Value] => .93
                            [Save] => Y
                        )

                )

            [9] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 4655146503
                            [Name] => FR_USEWITHTRAILER
                            [Value] => 1
                            [Save] => N
                        )

                )

            [10] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 761850500
                            [Name] => TRF_CL_BASE
                            [Value] => 7399
                            [Save] => Y
                        )

                )

            [11] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 761851700
                            [Name] => TRF_CL_ZONE
                            [Value] => 1.8
                            [Save] => Y
                        )

                )

            [12] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 761851800
                            [Name] => TRF_CL_BONUSMALUS
                            [Value] => 1
                            [Save] => Y
                        )

                )

            [13] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 761854000
                            [Name] => TRF_CL_POWER
                            [Value] => 1
                            [Save] => Y
                        )

                )

            [14] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 761854100
                            [Name] => TRF_CL_PERIOD
                            [Value] => 1
                            [Save] => Y
                        )

                )

            [15] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 765909400
                            [Name] => TRF_CL_ABUSE
                            [Value] => 1
                            [Save] => Y
                        )

                )

            [16] => SimpleXMLElement Object
                (
                    [@attributes] => Array
                        (
                            [Isn] => 816328000
                            [Name] => TRF_CL_MULTIDRIVE
                            [Value] => 1
                            [Save] => Y
                        )

                )

        )

)

D
Dmitry Koshelenko, 2019-06-17
@GomelHawk

You can use SimpleXML (it often already comes in dependencies)
Initialization:
Search for the desired element (we take the first one that comes across):
Value (can be replaced with int if you want to use it as an integer value):
$value = (string) $element['Value'];

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question