dongmei1911 2014-11-07 15: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-07 16: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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部