dongmei1911 2014-11-07 23:24
浏览 87
已采纳

如何使用PHP中的SimpleXML获取节点内标记内的属性

The XML is,

<phoneInfo npa="202" nxx="241" blockId="7" lineType="Landline" latitude="38.899904120193"
     longitude="-77.031636354196" centralOfficeName="WASHINGTON" switch="WASLDCRGDS0"
 nxxType="00" nxxDescription="Regular (Plain Old Telephone Service (POTS))">
<operatingCompany ocn="280E" category="C" categoryDescription="competitive" 
name="YMax Communications Corporation - DC" dba="YMax Communications Corporation" 
commonName="Ymax" type="CLEC" address="223 Sunset Ave" city="Palm Beach" state="FL" zip="33480"
 logoWidth="253" logoHeight="47" >
<rateCenter name="WASHINGTON ZONE 1:Washington DC" code="WSHNGTNZN1" type="suburban zone" 
country="US" countryName="U.S.A." state="DC" stateName="District of Columbia" timeZone="ET" 
timeZoneName="Eastern (UTC -5 hours)" dstRecognized="yes">
<lata code="236" state="DC" stateName="District of Columbia" country="US" countryName="U.S.A."
 location="WASHINGTON DC"/>
<bta code="461" name="Washington DC"/>
<mtas>
<mta code="10" country="US" marketName="Washington-Baltimore"/>
</mtas>
<cbsas>
<cbsa code="47900" csa="548" areaName="Washington-Arlington-Alexandria" areaType="Metropolitan"
state="DC-VA-MD-WV"/>
</cbsas>
<counties>
<county fipsCode="11001" name="District of Columbia" type="District" state="DC" 
stateName="District of Columbia" country="US" countryName="U.S.A." landAreaSquareMiles="61" 
populationIn2006="581530"/>
</counties>
<zipCodes>
<zipCode code="20005"/>
<zipCode code="20002"/>
<zipCode code="20004"/>
<zipCode code="20001"/>
</zipCodes>
</rateCenter>
</phoneInfo>

What I am trying to do is get the <county> nested within <counties> like this => $county_population = (string) $dataset->phoneInfo->ratecenter->counties->county->attributes()->populationIn2006;

I want to get the data in the tag populationIn2006

How to do this?

Please help! Thanks much appreciated for taking your time looking into this.

  • 写回答

1条回答 默认 最新

  • dongmaoluan5719 2014-11-08 00:14
    关注

    The command is close but is missing the operatingCompany node and "ratecenter" is misspelled (needs capital C).

    As well the XML you've posted is invalid as it's missing the closing </operatingCompany> tag but I am assuming that is a copy/paste error.

    $county_population = (string) $dataset->phoneInfo->operatingCompany->rateCenter->counties->county->attributes()->populationIn2006;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 一道python难题2
  • ¥15 一道python难题
  • ¥15 用matlab 设计一个不动点迭代法求解非线性方程组的代码
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备