帮助在PHP访问XML属性 [英] Help accessing xml attribute in php
问题描述
我一般是新来的PHP和编码。我试图从远程设备解析XML和访问特定的值数据。我想显示组例如9探头1值,我无法得到它的工作。任何提示?
下面是XML:
<?XML版本=1.0编码=ISO-8859-1&GT?;
- <设备ID =S10011HB =1935>
<组ID =1/>
<组ID =2/>
<组ID =3/>
<组ID =4/>
<组ID =10/>
<组ID =6/>
<组ID =7/>
<组ID =8/>
- <组ID =9>
- <探测器ID =99>
< VALUE> 1.0 LT; /价值与GT;
< /探针与GT;
- <探测器ID =1>
< VALUE> 86.4< /价值与GT;
< /探针与GT;
- <探测器ID =2>
< VALUE> 45.7< /价值与GT;
< /探针与GT;
- <探测器ID =3>
< VALUE> 2.9 LT; /价值与GT;
< /探针与GT;
- <探测器ID =4>
< VALUE> 1.0 LT; /价值与GT;
< /探针与GT;
< /组>
< /装置>
下面是我的PHP code在XML阅读:
< PHP
//建立一个80端口的连接
$ HTTP =的fsockopen(192.168.2.106,80); //发送到服务器的请求
$ REQ =GET / XMLDATA HTTP / 1.0 \\ r \\ n;
$ REQ =主持人:192.168.2.106 \\ r \\ n。
$ REQ =连接:关闭\\ r \\ n \\ r \\ n。
的fputs($ HTTP,$ REQ); //输出请求的结果
而(!的feof($ HTTP)){
$ xmlstr =与fgets($ HTTP,2048)。
}
//关闭连接
FCLOSE($ HTTP);
$ XML = simplexml_load_string($ xmlstr); 的print_r($ XML); $ myvalue的= $ XML的>的XPath('//集团[@ ID =9] /探头[@ ID =1] /值');
回声$ myvalue的;
?>
一个的print_r($ XML);显示以下信息:
SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => S10011
[HB] => 158221
) [组] =>排列
(
[0] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 1
) [0] => ) [1] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 2
) [0] => ) [2] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 3
) [0] => ) [3] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 4
) [0] => ) [4] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] =>五
) [0] => ) [5] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 6
) [0] => ) [6] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 7
) [0] => ) [7] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 8
) [0] => ) [8] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 9
) [探头] =>排列
(
[0] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 99
) [超值] => 2.0
) [1] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 1
) [超值] => 89.6
) [2] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 2
) [超值] => 42.7
) [3] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 3
) [超值] => 3.9
) [4] => SimpleXMLElement对象
(
[@属性] =>排列
(
[ID] => 4
) [超值] => 1.0
) ) ) ))
您必须剥去HTTP响应HTTP标头,或者你不会得到有效的XML文档。根据您的托管环境中,您可能能够将HTTP URL传递给使用simplexml_load_file()
这比你在做什么简单多了。
此外,您的XPath不工作,因为XML属性和标签名称是区分大小写的。
$ XML =使用simplexml_load_file(http://192.168.2.106/xmldata);
$ myvalue的= $ XML的>的XPath(//集团[@ ID ='9'] /探针[@ ID ='1'] /值);
回声$ myvalue的[0];
是否所有这些破折号XML源在短短的复制/粘贴问题?
I'm new to php and coding in general. I'm trying to parse xml from a remote device and access specific value data. I would like to display group 9 probe 1 value for example and I cannot get it to work. Any tips?
Here is the xml:
<?xml version="1.0" encoding="ISO-8859-1" ?>
- <Device id="S10011" hb="1935">
<Group id="1" />
<Group id="2" />
<Group id="3" />
<Group id="4" />
<Group id="5" />
<Group id="6" />
<Group id="7" />
<Group id="8" />
- <Group id="9">
- <Probe id="99">
<Value>1.0</Value>
</Probe>
- <Probe id="1">
<Value>86.4</Value>
</Probe>
- <Probe id="2">
<Value>45.7</Value>
</Probe>
- <Probe id="3">
<Value>2.9</Value>
</Probe>
- <Probe id="4">
<Value>1.0</Value>
</Probe>
</Group>
</Device>
Here is my php code to read in the xml:
<?php
// Establish a port 80 connection
$http = fsockopen("192.168.2.106",80);
// Send a request to the server
$req = "GET /xmldata HTTP/1.0\r\n";
$req .= "Host: 192.168.2.106\r\n";
$req .= "Connection: Close\r\n\r\n";
fputs($http, $req);
// Output the request results
while(!feof($http)) {
$xmlstr .= fgets($http, 2048);
}
// Close the connection
fclose($http);
$xml = simplexml_load_string($xmlstr);
print_r($xml);
$myValue = $xml->xpath('//Group[@ID="9"]/Probe[@ID="1"]/value');
echo $myValue;
?>
A print_r($xml); shows the following info:
SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => S10011
[hb] => 158221
)
[Group] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[0] =>
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 2
)
[0] =>
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 3
)
[0] =>
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 4
)
[0] =>
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 5
)
[0] =>
)
[5] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 6
)
[0] =>
)
[6] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 7
)
[0] =>
)
[7] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 8
)
[0] =>
)
[8] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 9
)
[Probe] => Array
(
[0] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 99
)
[Value] => 2.0
)
[1] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 1
)
[Value] => 89.6
)
[2] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 2
)
[Value] => 42.7
)
[3] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 3
)
[Value] => 3.9
)
[4] => SimpleXMLElement Object
(
[@attributes] => Array
(
[id] => 4
)
[Value] => 1.0
)
)
)
)
)
You have to strip the HTTP header from the HTTP response or you won't get a valid XML document. Depending on your hosting environment you may be able to pass an HTTP URL to simplexml_load_file()
which is much simpler than what you're doing.
Also your xpath doesn't work because XML attributes and tag names are case sensitive.
$xml = simplexml_load_file("http://192.168.2.106/xmldata");
$myValue = $xml->xpath("//Group[@id='9']/Probe[@id='1']/Value");
echo $myValue[0];
Are all these dashes in the XML source just a copy/paste issue?
这篇关于帮助在PHP访问XML属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!