帮助在php中访问xml属性

发布于 2024-09-12 12:28:31 字数 6046 浏览 4 评论 0原文

我对 php 和一般编码很陌生。我正在尝试从远程设备解析 xml 并访问特定值数据。例如,我想显示第 9 组探头 1 的值,但我无法让它工作。有什么建议吗?

这是 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>

这是我要在 xml 中读取的 php 代码:

    <?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);显示以下信息:

    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
                                )

                        )

                )

        )

)

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
                                )

                        )

                )

        )

)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

青春有你 2024-09-19 12:28:32

试试这个:

   $myValue = $xml->xpath('//Group[@id="9"]/Probe[@id="1"]/Value');  
   echo $myValue[0]; 

Try this instead:

   $myValue = $xml->xpath('//Group[@id="9"]/Probe[@id="1"]/Value');  
   echo $myValue[0]; 
我们的影子 2024-09-19 12:28:32

您必须从 HTTP 响应中去除 HTTP 标头,否则您将无法获得有效的 XML 文档。根据您的托管环境,您也许能够将 HTTP URL 传递给 simplexml_load_file(),这比您正在做的事情简单得多。

另外,您的 xpath 不起作用,因为 XML 属性和标记名称区分大小写。

$xml = simplexml_load_file("http://192.168.2.106/xmldata");
$myValue = $xml->xpath("//Group[@id='9']/Probe[@id='1']/Value"); 
echo $myValue[0];

XML 源中的所有这些破折号是否只是复制/粘贴问题?

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?

樱娆 2024-09-19 12:28:32
<?php
$device = getDoc();
// iterate over all Group elements that have one or more Probe elements that have one or more Value elements.
foreach( $device->xpath('Group[Probe/Value]') as $group ) {
  echo 'Group id=', $group['id'], "\n";
  foreach( $group->Probe as $probe ) {
    echo '  probe id=', $probe['id'], "\n";
    foreach( $probe->Value as $value ) {
      echo '   value=', $value, "\n";
    }
  }
}

function getDoc() {
  return new SimpleXMLElement('<?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>');
}

打印

Group id=9
  probe id=99
   value=1.0
  probe id=1
   value=86.4
  probe id=2
   value=45.7
  probe id=3
   value=2.9
  probe id=4
   value=1.0

另请参阅:http://docs.php.net/simplexml.examples-basichttp://www.w3.org/TR/xpath/

<?php
$device = getDoc();
// iterate over all Group elements that have one or more Probe elements that have one or more Value elements.
foreach( $device->xpath('Group[Probe/Value]') as $group ) {
  echo 'Group id=', $group['id'], "\n";
  foreach( $group->Probe as $probe ) {
    echo '  probe id=', $probe['id'], "\n";
    foreach( $probe->Value as $value ) {
      echo '   value=', $value, "\n";
    }
  }
}

function getDoc() {
  return new SimpleXMLElement('<?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>');
}

prints

Group id=9
  probe id=99
   value=1.0
  probe id=1
   value=86.4
  probe id=2
   value=45.7
  probe id=3
   value=2.9
  probe id=4
   value=1.0

see also: http://docs.php.net/simplexml.examples-basic and http://www.w3.org/TR/xpath/

孤星 2024-09-19 12:28:32

xml:

<root><item attrname="5"/></root>

php:

$var = $xml->xpath('root/item/@attrname');
echo $var[0];

或 >= php5.3

$var = $xml->xpath('root/item/@attrname')[0];
echo $var;

结果:

5

xml:

<root><item attrname="5"/></root>

php:

$var = $xml->xpath('root/item/@attrname');
echo $var[0];

or >= php5.3

$var = $xml->xpath('root/item/@attrname')[0];
echo $var;

result:

5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文