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


How to get the attributes within a tag within a node with SimpleXML in PHP

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>中,如下=>$county_population = (string) $dataset->phoneInfo->ratecenter->counties->county->attributes()->populationIn2006;

我想获取标签populationIn2006 中的数据

如何做到这一点?

请帮忙!非常感谢您花时间研究此事。

命令已关闭,但缺少operatingCompany节点,并且"ratecenter"拼写错误(需要大写C)。

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

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