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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序,怎么查看客户esp32板子上程序及烧录地址
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址