我有以下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:\ xampp \ htdocs \ cr \ services \ create.php中获取非对象的属性
我是新手,在php中访问xml。请帮忙。
答案 0 :(得分:0)
只需简单地将其作为普通对象访问:
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