从simplexml_load_file获取属性

时间:2018-03-21 13:11:20

标签: php xml simplexml

我正试图弄清楚如何从以下XML加载变量location,time和val。

XML看起来像这样:

 <android.support.v4.widget.NestedScrollView
    android:id="@+id/bodyScroller"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:scrollbars="vertical"
    android:fillViewport="false">

<com.user.consulting_implantologist.MyText2
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="16dp"
    android:layout_marginLeft="10dp"
    android:layout_marginRight="10dp"
    android:id="@+id/DrDetail"
    android:text="Detail"
    android:layout_marginBottom="15dp"
    android:textColor="@color/colorApp" />
   </android.support.v4.widget.NestedScrollView>

Parsed XML看起来像这样:

<root xmlns="">
     <sns id="1" name="Senzor A" type="1" status="0" unit="0" val="4.5" w-min="" w-max=""/>
     <status level="2" location="AAA" time="03/21/2018 14:09:08"/>
</root>

我很难弄清楚如何导航,所以如果有人能给我一些指示,我将不胜感激。

1 个答案:

答案 0 :(得分:3)

可以使用$xml->tag来完成使用simpleXML访问属性。

可以使用$xml->tag->subtag$xml = '<root xmlns=""> <sns id="1" name="Senzor A" type="1" status="0" unit="0" val="4.5" w-min="" w-max=""/> <status level="2" location="AAA" time="03/21/2018 14:09:08"/> </root>'; $xml = simplexml_load_string($xml); // or simplexml_load_file("file.xml"); echo $xml->status['location']; echo $xml->status['time']; echo $xml->sns['val']; 来访问元素。

AAA
03/21/2018 14:09:08
4.5

输出:

let username = UsernameTextField.text
相关问题