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 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据