小弟太菜了,我想获取<type>=express,<dests>=530000时的<price>的值,那位大哥给写个详细的代码啊?数据代码如下:
<?xml version="1.0" encoding="UTF-16" standalone="no"?>
<postage_get_response>
       <postage>
              <ems_increase>8.0</ems_increase>
              <ems_price>22.0</ems_price>
              <express_increase>3.0</express_increase>
              <express_price>8.0</express_price>
              <name>快递</name>
              <postage_id>41864647</postage_id>
              <postage_modes list="true">
                     <postage_mode>
                            <dests>440000</dests>
                            <id>181030917</id>
                            <increase>3.0</increase>
                            <price>10.0</price>
                            <type>ems</type>
                     </postage_mode>
                     <postage_mode>                  <dests>130000,360000,500000,460000,370000,530000,520000,340000,510000,420000,450000,410000,110000,120000,430000,350000</dests>
                            <id>181030918</id>
                            <increase>5.0</increase>
                            <price>15.0</price>
                            <type>express</type>
                     </postage_mode>
              </postage_modes>
       </postage>
</postage_get_response> 

解决方案 »

  1.   

    页面并没有看到<dests>=530000的标签。。以下只是输出全部结构。你自己遍历判断一下就可以得到结果。。
      
    $xml=<<<xml
    <?xml version="1.0" encoding="UTF-16" standalone="no"?>
    <postage_get_response>
      <postage>
      <ems_increase>8.0</ems_increase>
      <ems_price>22.0</ems_price>
      <express_increase>3.0</express_increase>
      <express_price>8.0</express_price>
      <name>快递</name>
      <postage_id>41864647</postage_id>
      <postage_modes list="true">
      <postage_mode>
      <dests>440000</dests>
      <id>181030917</id>
      <increase>3.0</increase>
      <price>10.0</price>
      <type>ems</type>
      </postage_mode>
      <postage_mode> <dests>130000,360000,500000,460000,370000,530000,520000,340000,510000,420000,450000,410000,110000,120000,430000,350000</dests>
      <id>181030918</id>
      <increase>5.0</increase>
      <price>15.0</price>
      <type>express</type>
      </postage_mode>
      </postage_modes>
      </postage>
    </postage_get_response>
    xml;
    $con=simplexml_load_string($xml);
    print_r($con);输出:
    SimpleXMLElement Object
    (
        [postage] => SimpleXMLElement Object
            (
                [ems_increase] => 8.0
                [ems_price] => 22.0
                [express_increase] => 3.0
                [express_price] => 8.0
                [name] => 快递
                [postage_id] => 41864647
                [postage_modes] => SimpleXMLElement Object
                    (
                        [@attributes] => Array
                            (
                                [list] => true
                            )                    [postage_mode] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [dests] => 440000
                                        [id] => 181030917
                                        [increase] => 3.0
                                        [price] => 10.0
                                        [type] => ems
                                    )                            [1] => SimpleXMLElement Object
                                    (
                                        [dests] => 130000,360000,500000,460000,370000,530000,520000,340000,510000,420000,450000,410000,110000,120000,430000,350000
                                        [id] => 181030918
                                        [increase] => 5.0
                                        [price] => 15.0
                                        [type] => express
                                    )                        )                )        ))
      

  2.   

    上面的xml是api获取的 我写的代码是:
    $yunfei2 = $api->Send('get','xml')->getArrayData();
    $xml_file = 'yunfei2.xml';
    $my_xml = simplexml_load_file($xml_file);
    $postage_price = array();
    $current = $my_xml->postage->postage_modes;
    for ($i=0; isset($current->postage_mode->$i); $i++) {
       if ($current->postage_mode->$i->type == 'express' && preg_match ("/\b530000\b/", $current->postage_mode->$i->dests)) {
          $postage_price[] = $current->postage_mode->$i->price;
       }
    }
    print_r($postage_price);但是我调用$postage_price时并不能获取到数据,很郁闷