dourunlao1642 2014-06-07 10:36
浏览 65

使用PHP解析XML并获取嵌套的级元素值和属性值

Hello i am trying to parse xml i have couple of trouble do this.

I am not able to get nested level element.For eg.telephone I am not able to get attribute value.I am not able to get and the third level nested element properly, i tried many code for that also for attribute values of some elements. like here telephone type and commercialListingType

Here is my xml

    <propertyList date="2014-06-02-17:30:33" username="bad" password="dfdfd">

    <business modTime="2014-06-02-12:22:32" status="current">

    <agentID>TEST</agentID>

    <uniqueID>1420648</uniqueID>

    <listingAgent id="1">

    <name>hjon Smith</name>

    <telephone type="BH"></telephone>

    <telephone type="mobile"></telephone>

    <email>bbd@ozemail.com.au</email>

    </listingAgent><listingAgent id="2"></listingAgent>

    <address display="no">

    <subNumber>Yoghurt bbd 4000</subNumber>

    <streetNumber></streetNumber>

    <street></street>

    <suburb display="no">Newy</suburb>

    <state>NSW</state>

    <postcode>2000</postcode>

    <country>London</country>

    </address>

    <price display="yes" plusSAV="no" tax="exclusive">200000</price>

    <priceView></priceView>


    <externalLink href=""/><externalLink href=""/>

    <videoLink href=""/>

    <underOffer value="no"/>

    <commercialListingType value="sale"/>

    <franchise value="yes"/>

    <businessCategory id="1">

    <name>Franchise</name>

    </businessCategory>
    </propertyList>

Here is my code

<?php
$xml=simplexml_load_file("testing.xml");

$data = array();



foreach($xml->business as $business) {


    $business = (array) $business;


    if (!array_key_exists($business['uniqueID'], $data)) {

        $listingAgent = (array) $business['listingAgent'];
        $price = (array) $business['price'];
        $commercialListingType= (array)$business['commercialListingType'];

        print_r($commercialListingType->attributes());

        $data[$business['uniqueID']] = array(
            'agentID' => $business['agentID'],
            'uniqueID' => $business['uniqueID'],
            'name' => (string)$listingAgent[0]->name,
            'email' => (string) $listingAgent[0]->email,
            'price'=>(string) $price[0],
            'telephone' => (string) $listingAgent[0]->telephone[0],
            'mobile' => (string) $listingAgent[0]->telephone[1],
        );
    }



}

echo "<pre>";
print_r($data);

?>  
  • 写回答

2条回答 默认 最新

  • doujiao7679 2014-06-07 11:08
    关注
    $xml = new SimpleXMLElement($string); //variable $string is nothing but your XML content
    $result = $xml->xpath('/propertyList/business/listingAgent/telephone');
    //short form
    //$result = $xml->xpath('////telephone');
    foreach($result as $node) {
      print 'Telephone Atrribute: '.$node->attributes().'<br>';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 如何实现从tello无人机上获取实时传输的视频流,然后将获取的视频通过yolov5进行检测
  • ¥15 WPF使用Canvas绘制矢量图问题
  • ¥15 用三极管设计一个单管共射放大电路
  • ¥15 孟德尔随机化r语言运行问题
  • ¥15 pyinstaller编译的时候出现No module named 'imp'
  • ¥15 nirs_kit中打码怎么看(打码文件是csv格式)
  • ¥15 怎么把多于硬盘空间放到根目录下
  • ¥15 Matlab问题解答有两个问题
  • ¥15 LCD12864中文显示
  • ¥15 在使用CH341SER.EXE时不小心把所有驱动文件删除了怎么解决