php - 获取 XML 变量
问题描述
我需要能够从 XML 中检索一个变量,它会告诉我一个属性是否对宠物友好。目前它似乎没有返回正确的数据,因为它嵌套在一个变量中,我正在努力检索它(我在 XML 和 PHP 类方面有点新手)
这是我正在使用的 XML 的相关位:
<property>
<propertycode>####</propertycode><lastupdate>28/08/2019 11:56:52</lastupdate><enabled>1</enabled>
<propertyname>Cottage sleeps 6, Sunday changeover, pet friendly</propertyname><longitude>0</longitude>
<latitude>0</latitude><sleeps adults="6" children="2" infants="1">6</sleeps><clientcode>
</clientcode><ownerid>####</ownerid><prices>
</prices><price/>
<siteid>####</siteid>
<propertygroup>Cottage sleeps 6, Sunday changeover, pet friendly </propertygroup>
<groupID>####</groupID><groupdisplay>grouped</groupdisplay>
<randOrder>242495955</randOrder><propertytown></propertytown>
<country>United Kingdom</country><countryiso>GB</countryiso>
<dateadded>2016-08-02</dateadded><managerID>0</managerID> <managername/><manageremail/><manageraddress/><managerpostcode/><managertelephone/><managertelephonealt/><managertelephonehome/><managertelephonemobile/>
<nobooking>0</nobooking><regionname/><regionname1/><regionname2/><regionname3/><regionname4/><typename typeID="0"></typename>
<propertyurl/><propertyaddress></propertyaddress><propertypostcode/><propertystars>0</propertystars>
<bedrooms_new>2</bedrooms_new><bathrooms_new>2</bathrooms_new><showcapacity>1</showcapacity>
<capacitynotes>1 kingsize bedroom, 1 twin bedroom + pull down double bed in lounge. Maximum occupancy 6 adults/children plus travel cot</capacitynotes>
<maxprice currency="GBP" EUR="958.27" USD="1057.88" GBP="850.00">850.00</maxprice><minprice currency="GBP" EUR="710.25" USD="784.07" GBP="630.00">630.00</minprice><mindaily currency="GBP" EUR="388.95" USD="429.37" GBP="345.00">345.00</mindaily>
<shortdescription>------------
</shortdescription>
<cottageweblocation></cottageweblocation>
<seconddescription></seconddescription><subcaption></subcaption><metatitle></metatitle><metadescription></metadescription><metakeywords></metakeywords><changeover>Sunday</changeover><changeovernotes/><changeovernotes_html/><highlightday>Sunday</highlightday><latediscount>0.00</latediscount><lowoccupancydiscount>0.00</lowoccupancydiscount><multiweeksdiscount>0.00</multiweeksdiscount><seasonaldiscount>0.00</seasonaldiscount><daysdiscount>0.00</daysdiscount><earlydiscount>0.00</earlydiscount><lateavaildiscounts days="" rate="" type=""></lateavaildiscounts><lowoccupancy></lowoccupancy><multiweeks></multiweeks><seasonaldiscountdesc></seasonaldiscountdesc><daysdiscountdesc></daysdiscountdesc><earlydiscountdesc></earlydiscountdesc><discountnotes> </discountnotes><discounts>
</discounts><photos><photocount>3</photocount></photos>
<propertyvariables><propertyvariable id="variableID61839">
<variable id="61839" order="0">No. bedrooms</variable> <variable_trans id="61839" langID=""></variable_trans>
<variablevalue>2</variablevalue><variabledescription>
</variabledescription><variablephoto/><variablethumb/><varcatname heading="0" varcatID="1">No. bedrooms</varcatname><varcatname_trans heading="0" varcatID="1" langID=""></varcatname_trans></propertyvariable>
<propertyvariable id="variableID61849">
<variable id="61849" order="0">Pet friendly</variable>
<variable_trans id="61849" langID=""></variable_trans>
<variablevalue>1</variablevalue>
<variabledescription></variabledescription>
<variablephoto/><variablethumb/>
<varcatname heading="0" varcatID="1">Pet friendly</varcatname>
<varcatname_trans heading="0" varcatID="1" langID="">
</varcatname_trans>
</propertyvariable></propertyvariables>
</property>
这是以前为我编写的 PHP,但它不起作用:
$properties = $xml->property;
$variables = $property->property->variables;
$petfriendly = false;
if($property->property->variables->varcat[1]){
$petfriendly = (bool)$property->property->variables->varcat[1]->varcatitems->varcatitem->variable->attributes()->bool;
}
$property_meta['petfriendly'] = ($petfriendly ? 'yes' : 'no');
解决方案
使用 SimpleXML,您可以轻松地使用 XPath 来检查 Pet Friendly 变量(假设这是一个常量——我希望它是)。然后,如果该值未找到或为 0,则将其设置为,no
否则将其设置为yes
...
$properties = $xml->property;
$petfriendly = $properties->xpath("//propertyvariable[@id='variableID61849']/variablevalue");
if ( count($petfriendly) == 0 || $petfriendly[0] == '0' ){
$property_meta['petfriendly'] = 'no';
}
else {
$property_meta['petfriendly'] = 'yes';
}
推荐阅读
- r - 对于 R 中的 iris 数据集的 R48 图,所有节点都标记为“setosa”
- html - Intellij Ultimate 2020.2 使用大量内存和滞后
- android - Android - 不推荐使用的方法和 minApiLevel
- c - 如何使在每个条件下运行的 if 语句成为 .c 文件(完整源代码)
- visual-studio - 免费配置文件在最新的 Visual Studio 2019 中不可见(适用于 Mac 上的 VS4Mac)
- c# - 在 VPS 上执行某些命令后,带有 Hosting API 的控制台应用程序就会停止
- sql - postgresql split_part() 仅返回由分隔符分隔的唯一子字符串?
- jenkins - Jenkins 管道 - 从 bat 命令到标签的变量,输出显示完整的命令提示符
- javascript - iOS safari - 检查是否授予设备运动权限
- c# - 通过转换器与反序列化转换 Json 字符串