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

时间:2014-11-07 23:24:11

标签: php xml simplexml

XML是,

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

我要做的是将<county>嵌套在<counties>内,就像这样=&gt;     $county_population = (string) $dataset->phoneInfo->ratecenter->counties->county->attributes()->populationIn2006;

我想获取标记populationIn2006

中的数据

怎么做?

请帮忙!非常感谢您花时间研究这个。

1 个答案:

答案 0 :(得分:1)

该命令已关闭但缺少operatingCompany节点和&#34; ratecenter&#34;拼写错误(需要资本C)。

您发布的XML也是无效的,因为它错过了结束</operatingCompany>代码,但我认为这是一个复制/粘贴错误。

$county_population = (string) $dataset->phoneInfo->operatingCompany->rateCenter->counties->county->attributes()->populationIn2006;