PHP:使用SimpleXML解析XML
我在尝试解析一个XML文件时遇到问题,我正在使用SimpleXML从另一个站点导入该文件。我需要显示某些物业的租金。但是,我只想显示某些物业的租金。我从未见过这样构造的XML文件: 首先,如何基于PHP:使用SimpleXML解析XML,php,xml,simplexml,Php,Xml,Simplexml,我在尝试解析一个XML文件时遇到问题,我正在使用SimpleXML从另一个站点导入该文件。我需要显示某些物业的租金。但是,我只想显示某些物业的租金。我从未见过这样构造的XML文件: 首先,如何基于 Property->PropertyID->MITS:Identification->MITS:PrimaryID 然后,根据ID回显属性->楼层平面->市场租金(最小值和最大值) TIA // get all properties $properties = $xml->x
Property->PropertyID->MITS:Identification->MITS:PrimaryID
然后,根据ID回显属性->楼层平面->市场租金(最小值和最大值)
TIA
// get all properties
$properties = $xml->xpath('//Property');
// get document namesapces in prefix => uri format
$namespaces = $xml->getNamespaces(true);
foreach($properies as $property)
{
// get namespaced nodes
$identification = $property->PropertyID->children($namespaces['MITS']);
$id = $identification->children($namespaces['MITS'])->PrimaryID;
// do your check id is an expected value or whatever and if so then...
foreach($property->Floorplan as $floorplan){
{
echo $floorplan->MarketRent['min'];
echo $floorplan->MarketRent['max'];
}
}
您可能可以使用xpath查询直接选择具有给定id或id集的属性,这样您就只需对它们进行循环并调用floorplan循环,但我将把这留给您的调查:-)我会说,如果您这样做,您将需要使用xpath注册名称空间。很确定这不是自动的。您是否被名称空间(“MIT:”)甩了?如果是这样的话,不用担心它们,只要把整个事情(“MITS:CompanyInfo”)当作一个节点名来处理就行了。这非常有效!我唯一需要做的更改是$xml->getnamespace(true);谢谢没问题!很高兴你解决了。将使用您为他人利益所做的调整更新答案:-)