PHP XML—通过它获取段的所有属性';身份证
我有个新问题: 当我根据xml元素的id选择该元素时,是否有方法获取该元素的所有属性 我得到的xml响应如下所示:PHP XML—通过它获取段的所有属性';身份证,php,xml,simplexml,Php,Xml,Simplexml,我有个新问题: 当我根据xml元素的id选择该元素时,是否有方法获取该元素的所有属性 我得到的xml响应如下所示: <availResponse> <tarifs currency="PLN"> <tarif tarifId="206844566_206844566" adtBuy="167.96" adtSell="167.96" chdBuy="167.96" chdSell="167.96" infBuy="167.96" infSell="167.96"
<availResponse>
<tarifs currency="PLN">
<tarif tarifId="206844566_206844566" adtBuy="167.96" adtSell="167.96" chdBuy="167.96" chdSell="167.96" infBuy="167.96" infSell="167.96" taxMode="INCL" topCar="false" topHotel="false" adtCancel="0.0" chdCancel="0.0" infCancel="0.0" powerPricerDisplay="sell">
<fareXRefs>
<fareXRef fareId="206844566">
<flights>
<flight flightId="1663150500" addAdtPrice="0.0" addChdPrice="0.0" addInfPrice="0.0" extFlightInfo="Lowest Fare">
<legXRefs>
<legXRef legId="1981746874" class="R" cos="E" cosDescription="ECONOMY" fareBaseAdt="LOWCOST"/>
</legXRefs>
</flight>
<flight flightId="1663150499" addAdtPrice="13.0" addChdPrice="13.0" addInfPrice="13.0" extFlightInfo="Lowest Fare">
<legXRefs>
<legXRef legId="1981746874" class="R" cos="E" cosDescription="ECONOMY" fareBaseAdt="LOWCOST"/>
</legXRefs>
</flight>
</flights>
</fareXRef>
</fareXRefs>
</tarif>
</tarifs>
<legs>
<leg legId="1981746939" depApt="WAW" depDate="2014-10-09" depTime="06:20" dstApt="TXL" arrDate="2014-10-09" arrTime="07:45" equip="" fNo="8071" cr="AB" miles="0" elapsed="1.42" meals="no meals" smoker="false" stops="0" eticket="true" ocr="AB"/>
<leg legId="1981747261" depApt="WAW" depDate="2014-10-09" depTime="17:25" dstApt="CPH" arrDate="2014-10-09" arrTime="18:45" equip="CR9" fNo="2752" cr="SK" miles="414" elapsed="1.33" meals="food and beverages for purchase" smoker="false" stops="0" eticket="true" ocr="SK" seats="8"/>
<leg legId="1981747262" depApt="CPH" depDate="2014-10-09" depTime="20:10" dstApt="LHR" arrDate="2014-10-09" arrTime="21:10" equip="320" fNo="1501" cr="SK" miles="594" elapsed="2.0" meals="light lunch" smoker="false" stops="0" eticket="true" ocr="SK" seats="9"/>
<leg legId="1981747267" depApt="LHR" depDate="2014-12-09" depTime="06:40" dstApt="CPH" arrDate="2014-12-09" arrTime="09:30" equip="320" fNo="500" cr="SK" miles="594" elapsed="1.83" meals="light lunch" smoker="false" stops="0" eticket="true" ocr="SK" seats="9"/>
<leg legId="1981746874" depApt="WAW" depDate="2014-10-09" depTime="15:45" dstApt="CDG" arrDate="2014-10-09" arrTime="18:10" equip="319" fNo="1347" cr="AF" miles="0" elapsed="2.42" meals="" smoker="false" stops="0" eticket="true" ocr="AF" seats="9"/>
<leg legId="1981747268" depApt="CPH" depDate="2014-12-09" depTime="15:35" dstApt="WAW" arrDate="2014-12-09" arrTime="16:55" equip="CR9" fNo="2751" cr="SK" miles="414" elapsed="1.33" meals="food and beverages for purchase" smoker="false" stops="0" eticket="true" ocr="SK" seats="9"/>
<leg legId="1981746966" depApt="ZRH" depDate="2014-12-09" depTime="19:20" dstApt="TXL" arrDate="2014-12-09" arrTime="20:45" equip="" fNo="8199" cr="AB" miles="0" elapsed="1.42" meals="no meals" smoker="false" stops="0" eticket="true" ocr="AB"/>
<leg legId="1981747462" depApt="LHR" depDate="2014-12-09" depTime="17:50" dstApt="BRU" arrDate="2014-12-09" arrTime="20:00" equip="AR1" fNo="2096" cr="SN" miles="0" elapsed="1.17" meals="" smoker="false" stops="0" eticket="true" ocr="SN" seats="9"/>
</legs>
</availResponse>
现在是疯狂:)
我从每个tarif->fareXRef->fareXRef->flights->flights->flights->legXRefs->legXRef中获取每个'legId'属性
$counted = $test['cntTarifs'];
for($i=0; $i < $counted; $i++) {
$test4 = $test->tarifs->tarif[$i]->fareXRefs;
$test5 = $test4->fareXRef->flights;
$test6 = $test5->flight->legXRefs->legXRef;
$segment= $test->legs->leg;
//now we're getting every needed thing
foreach($test5 as $keyless) { //sorting each flight element
foreach($test6 as $key) { //takingout every legXRef in each flight element
$id = $key['legId']; //this is what i'm looking for
echo "<br />".$id;
foreach($segment as $seg) { //trying to find leg
if($seg['legId'] == $id){ //if legId in leg equals to $id
echo $seg['dstApt']; // try to get another attribute and that doesn't work
}
}
}
}
}
$counted=$test['cntTarifs'];
对于($i=0;$i<$counted;$i++){
$test4=$test->tarifs->tarif[$i]->fareXRefs;
$test5=$test4->fareXRef->flights;
$test6=$test5->flight->legXRef->legXRef;
$segment=$test->legs->leg;
//现在我们得到了所有需要的东西
foreach($test5作为$keyless){//对每个飞行元素进行排序
foreach($test6作为$key){//提取每个飞行元素中的每个legXRef
$id=$key['legId'];//这就是我要找的
回显“
”$id;
foreach($seg作为$seg){//正在尝试查找leg
if($seg['legId']=$id){//如果leg中的legId等于$id
echo$seg['dstApt'];//尝试获取另一个属性,但这不起作用
}
}
}
}
}
我需要用选定的legId获取leg中存在的每个属性,但我的思维失败了:(在这种情况下,您可以使用xpath。示例:
$legId = '1981746939';
$node = $xml->xpath("//leg[@legId='$legId']"); // target the specific leg with this legId
if(count($node) <= 0) {
echo 'none found'; // exit if none found
exit;
}
$data = array();
foreach($node[0]->attributes() as $att => $val) { // if found, loop all the attributes
$data[$att] = (string) $val;
}
echo $data['dstApt']; // TXL
echo '<pre>';
print_r($data);
$legId='1981746939';
$node=$xml->xpath(//leg[@legId='$legId']);//使用此legId将特定的leg作为目标
如果(将($node)attributes()计数为$att=>$val){//如果找到,则循环所有属性
$data[$att]=(字符串)$val;
}
echo$data['dstApt'];//TXL
回声';
打印(数据);
使用SimpleXML,您必须使用
attributes()
方法检查属性:
$dom = new DOMDocument();
$dom->loadXml($xml);
$xpath = new DOMXpath($dom);
$attributes = [];
foreach ($xpath->evaluate('//legs/leg[@legId = "1981746939"][1]/@*') as $attribute) {
$attributes[$attribute->name] = $attribute->value;
}
var_dump($attributes);
输出:
CPH
SimpleXML是面向元素节点的,要获取属性,您需要在PHP中使用额外的逻辑或使用DOM+Xpath
array(17) {
["legId"]=>
string(10) "1981746939"
["depApt"]=>
string(3) "WAW"
["depDate"]=>
string(10) "2014-10-09"
["depTime"]=>
string(5) "06:20"
["dstApt"]=>
string(3) "TXL"
["arrDate"]=>
string(10) "2014-10-09"
["arrTime"]=>
...
输出:
//legs
Xpath表达式查找legs
元素节点
//legs/leg
$xml = simplexml_load_string($buffer);
$legId = '1981746939';
$attributes = $xml->xpath(sprintf("(//leg[@legId=%d])[1]/@*", $legId));
$data = null;
foreach ($attributes as $attribute) {
$data[$attribute->getName()] = (string)$attribute;
}
print_r($data);
获取它的leg
子级
//legs/leg[@legId = "1981746939"]
将其限制为具有特定属性值的
//legs/leg[@legId = "1981746939"][1]
确保只有一个节点从结果中选择第一个节点
//legs/leg[@legId = "1981746939"][1]/@*
并从该元素节点获取所有属性节点
//legs/leg
$xml = simplexml_load_string($buffer);
$legId = '1981746939';
$attributes = $xml->xpath(sprintf("(//leg[@legId=%d])[1]/@*", $legId));
$data = null;
foreach ($attributes as $attribute) {
$data[$attribute->getName()] = (string)$attribute;
}
print_r($data);
这里是SimpleXML中的另一种方法,它在xpath中支持查询元素和属性 因此,下面的代码所做的是选择
元素的所有属性,这些属性在其legId
属性中具有特定值
然后根据属性名称和值构建$data
数组:
Array
(
[legId] => 1981746939
[depApt] => WAW
[depDate] => 2014-10-09
[depTime] => 06:20
[dstApt] => TXL
[arrDate] => 2014-10-09
[arrTime] => 07:45
[equip] =>
[fNo] => 8071
[cr] => AB
[miles] => 0
[elapsed] => 1.42
[meals] => no meals
[smoker] => false
[stops] => 0
[eticket] => true
[ocr] => AB
)
结果:
@arclite当然很高兴helped@ghost:仅供参考:通过xpath的属性已经有可能了:simplexml xpath也会返回属性节点-