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


How to access nodes in xml in php

我有下面的xml模式,但我不知道如何在php中访问它。请帮我访问它。

这是的样本结构

<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>

php中的

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

在上面的行中,它给了我错误通知:试图在第3行上的C:''examplep''htdocs''cr''services''create.php中获取非对象的属性

我是php中访问xml的新手。请帮忙。

只需将其作为一个普通对象进行简单访问,如下所示:

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

输出:

Agent Details: Agent Name One


如果你想查看变量(对象)的结构,只要这样做,你就会看到如何访问它:

print_r($xml);

有关simplexml_load_file()的更多信息,请参阅手册:http://php.net/manual/en/function.simplexml-load-file.php