帮助访问php中的xml属性

时间:2010-08-03 19:55:22

标签: php xpath attributes simplexml

我是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;
?> 

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
                                )

                        )

                )

        )

)

4 个答案:

答案 0 :(得分:2)

请改为尝试:

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

答案 1 :(得分:2)

您必须从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源代码中的所有这些破折号都只是一个复制/粘贴问题吗?

答案 2 :(得分:0)

<?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/

答案 3 :(得分:0)

的xml:

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

PHP:

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

或&gt; = php5.3

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

结果:

5