doushi3322 2015-02-09 05:00
浏览 30
已采纳

如何在php中访问xml中的节点

I have the following xml schema but i don't know how to access this in php. please help me in accessing it.

here is the samples structure

<PropertyDetails>
   <ListingID>listingid</ListingID>
   <AgentDetails ID="13245">
      <Name>Agent Name One</Name>
      <Phones>
         <Phone ContactType="Business" PhoneType="Telephone">123-45678</Phone>

      </Phones>
      <Office ID="65">
         <Name>BROKER</Name>
         <Address>
            <StreetAddress>17 Z STREET</StreetAddress>
            <AddressLine1>1 T STREET</AddressLine1>
            <City>BE</City>
            <PostalCode>142001</PostalCode>
         </Address>
         <Phones>
            <Phone ContactType="Business" PhoneType="Telephone">123-45678</Phone>
            <Phone ContactType="Business" PhoneType="Fax">123-45678</Phone>
         </Phones>
         <Websites>
            <Website ContactType="Business" WebsiteType="Website">www</Website>
         </Websites>
      </Office>
      <Designations>
         <Designation>BRD</Designation>
      </Designations>
   </AgentDetails>
   <AgentDetails ID="163">
      <Name>Agent Name Two</Name>
      <Phones>
         <Phone ContactType="Business" PhoneType="Telephone">(103) 321-134</Phone>
         <Phone ContactType="Business" PhoneType="Fax">(603) 132-1222</Phone>
      </Phones>
      <Office ID="27">
         <Name>HRC, BR</Name>
         <Address>
            <StreetAddress>1 N STREET</StreetAddress>
            <AddressLine1>7 P STREET</AddressLine1>
            <City>BE</City>
            <PostalCode>142001</PostalCode>
         </Address>
         <Phones>
            <Phone ContactType="Business" PhoneType="Telephone">123 6578</Phone>
            <Phone ContactType="Business" PhoneType="Fax">321-134</Phone>
         </Phones>
      </Office>
      <Designations>
         <Designation>BR</Designation>
      </Designations>
   </AgentDetails>
   </PropertyDetails>

in php

$xml = simplexml_load_file("seven.xml") or die("Error: Cannot create object");
echo 'Agent Details'.$xml['AgentDetails']->Name;

at above line it gives me error Notice: Trying to get property of non-object in C:\xampp\htdocs\cr\services\create.php on line 3

I am new in accessing xml in php. please help.

  • 写回答

1条回答 默认 最新

  • dongmang3961 2015-02-09 05:07
    关注

    Just simple access it as a normal object like this:

    echo 'Agent Details: '.$xml->AgentDetails[0]->Name;
    

    Output:

    Agent Details: Agent Name One
    


    If you want to see the structure of your variable (object) just do this and you will see how to access it:

    print_r($xml);
    

    Also for more information about simplexml_load_file() see the manual: http://php.net/manual/en/function.simplexml-load-file.php

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?
  • ¥15 关于#vue.js#的问题:修改用户信息功能图片无法回显,数据库中只存了一张图片(相关搜索:字符串)
  • ¥15 texstudio的问题,
  • ¥15 spaceclaim模型变灰色
  • ¥15 求一份华为esight平台V300R009C00SPC200这个型号的api接口文档
  • ¥15 字符串比较代码的漏洞
  • ¥15 欧拉系统opt目录空间使用100%
  • ¥15 ul做导航栏格式不对怎么改?