我只是在学习PHP。我有一堆脚本,我已经开始,一个让我卡住了。我正在获取xml并将结果打印到页面。但是我只想要refTypeID = 10的行,并且还需要修剪原因区域中的“DESC:”文本。
我当前的代码
<?php
// Populate the following with your API Data
$vCode = "XXXXXXX";
$keyID = "XXXXXXX";
// Create the URL to the EVE API
$eveAPI = "http://api.eve-online.com/corp/WalletJournal.xml.aspx?keyID=".$keyID."&vCode=".$vCode."";
// Get the xml data
$xml = simplexml_load_file($eveAPI);
// Loop Through Skills
foreach ($xml->result->rowset->row as $value) {
echo "Skill Number:".$value['refTypeID']." -- Skill Points: ".$value['ownerName1']." -- Level: ".$value['reason']."<br />";
};
?>
我在解析什么
<eveapi version="2">
<currentTime>2012-11-12 10:36:35</currentTime>
<result>
<rowset name="entries" key="refID" columns="date,refID,refTypeID,ownerName1,ownerID1,ownerName2,ownerID2,argName1,argID1,amount,balance,reason">
<row date="2012-11-12 10:46:49" refID="6570815512" refTypeID="10" ownerName1="Captain Vampire" ownerID1="159434479" ownerName2="The Condemned and Convicted" ownerID2="98032142" argName1="" argID1="0" amount="5000000.00" balance="13072537.98" reason="DESC: something "/>
<row date="2012-11-10 02:27:48" refID="6561124130" refTypeID="85" ownerName1="CONCORD" ownerID1="1000125" ownerName2="Justin Schereau" ownerID2="90541382" argName1="Unertek" argID1="30002413" amount="42300.00" balance="7972463.03" reason="10015:1,10019:1,11899:1,22822:1,"/>
<row date="2012-11-09 23:27:24" refID="6560673105" refTypeID="85" ownerName1="CONCORD" ownerID1="1000125" ownerName2="Blackcamper" ownerID2="754457655" argName1="Illamur" argID1="30002396" amount="25000.00" balance="7930163.03" reason="11898:1,"/>
</rowset>
</result>
<cachedUntil>2012-11-12 11:03:35</cachedUntil>
</eveapi>
非常感谢任何帮助
由于
答案 0 :(得分:3)
您可以直接使用xpath
,如下所示
$xml = simplexml_load_file($eveAPI);
/* Search for <a><b><c> */
$result = $xml->xpath('//result/rowset/row[@refTypeID=10]');
foreach($result as $value) {
echo $value['reason'] = trim(str_replace('DESC:','',$value['reason']));
echo "Skill Number:".$value['refTypeID']." -- Skill Points: ".$value['ownerName1']." -- Level: ".$value['reason']."<br />";
}
答案 1 :(得分:0)
尝试
// Loop Through Skills
foreach ($xml->result->rowset->row as $value) {
if($value['refTypeID'] == 10){
echo "Skill Number:".$value['refTypeID']." -- Skill Points: ".$value['ownerName1']." -- Level: ".str_replace('DESC:', '', $value['reason'])."<br />";
}
};
答案 2 :(得分:0)
您可以使用continue
跳过不需要的行:
foreach ($xml->result->rowset->row as $value) {
if ($value['refTypeID'] != "10") {
// skip
continue;
}
//etc ...
}
并使用str_replace
删除字符串DESC:
:
$reason = str_replace('DESC: ','',$value['reason']);
注意:这也会删除DESC: