如何在PHP中从XML文件中读取元素?

如何在PHP中从XML文件中读取元素?,php,xml,Php,Xml,我有一个xml提要,需要从中访问元素。然而,我尝试过的方法都不起作用 以下是完整的xml提要: <GeocodeResponse> <status>OK</status> <result> <type>locality</type> <type>political</type> <formatted_address>Liverpool, Merseyside, UK

我有一个xml提要,需要从中访问元素。然而,我尝试过的方法都不起作用

以下是完整的xml提要:

<GeocodeResponse>
<status>OK</status>
<result>
    <type>locality</type>
    <type>political</type>
    <formatted_address>Liverpool, Merseyside, UK</formatted_address>
    <address_component>
        <long_name>Liverpool</long_name>
        <short_name>Liverpool</short_name>
        <type>locality</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>Merseyside</long_name>
        <short_name>Mersyd</short_name>
        <type>administrative_area_level_2</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>England</long_name>
        <short_name>England</short_name>
        <type>administrative_area_level_1</type>
        <type>political</type>
    </address_component>
    <address_component>
        <long_name>United Kingdom</long_name>
        <short_name>GB</short_name>
        <type>country</type>
        <type>political</type>
    </address_component>
    <geometry>
        <location>
            <lat>53.4115400</lat>
            <lng>-2.9901160</lng>
        </location>
        <location_type>APPROXIMATE</location_type>
        <viewport>
            <southwest>
                <lat>53.3049930</lat>
                <lng>-3.2462348</lng>
            </southwest>
            <northeast>
                <lat>53.5178208</lat>
                <lng>-2.7339972</lng>
            </northeast>
        </viewport>
        <bounds>
            <southwest>
                <lat>53.3115426</lat>
                <lng>-3.0191794</lng>
            </southwest>
            <northeast>
                <lat>53.5039071</lat>
                <lng>-2.8115043</lng>
            </northeast>
        </bounds>
    </geometry>
</result>
</GeocodeResponse>

好啊
地点
政治的
利物浦,默西塞德郡,英国
利物浦
利物浦
地点
政治的
默西塞德
梅西德
行政区二级
政治的
英格兰
英格兰
行政区一级
政治的
大不列颠联合王国
国标
国
政治的
53.4115400
-2.9901160
近似
53.3049930
-3.2462348
53.5178208
-2.7339972
53.3115426
-3.0191794
53.5039071
-2.8115043
我正在尝试选择几何->位置->纬度->位置,然后在这个纬度和液化天然气内。

您尝试过还是? 只需加载数据,然后访问所需的节点,如下所示:

$xml = simplexml_load_file('your.xml');
echo $xml->GeocodeResponse->result->geometry->location->lat;
echo $xml->GeocodeResponse->result->geometry->location->lon;

如果有字符串,可以执行以下操作:

$str = "<?xml version='1.0'?> 
            <document> 
                <cmd>login</cmd> 
                <login>Richard</login> 
            </document>";
$xml = simplexml_load_string($str);

print_r($xml);

print_r($xml->document->cmd);

// Remember to type cast it a string if you want the actual value.
// Example:
$myval = (string) $xml->document->cmd;
$str=”
登录
理查德
";
$xml=simplexml\u load\u字符串($str);
打印(xml);
打印($xml->document->cmd);
//如果需要实际值,请记住键入并将其转换为字符串。
//例如:
$myval=(字符串)$xml->document->cmd;

这里有PHP问题吗?你有密码吗?你是怎么读文件的?你说什么都没用-但是你试了什么?可能重复的可能重复的抱歉,我应该更具体一点,我收到了一个错误,但现在看来是这样,因为我的简单xml加载程序不工作。我在php.ini中打开了openssl,一切又恢复正常。]]很抱歉浪费大家的时间,我有所有的评论。thanks@DanielBenzie请用这些信息更新您的问题,如果没有所有适当的细节,我们将无法给出一个好的答案。