简体   繁体   English

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

[英]How to access nodes in xml in php

I have the following xml schema but i don't know how to access this in php. 我有以下xml架构,但我不知道如何在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 在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 在上面的行中,它给我错误提示:尝试在第3行的C:\\ xampp \\ htdocs \\ cr \\ services \\ create.php中获取非对象的属性

I am new in accessing xml in php. 我是在php中访问xml的新手。 please help. 请帮忙。

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 另外,有关simplexml_load_file()的更多信息,请参见手册: http : //php.net/manual/en/function.simplexml-load-file.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM