2

我只是在学习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>

任何帮助将非常感激

谢谢

4

3 回答 3

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 />"; 
}
于 2012-11-12T14:31:48.863 回答
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 />"; 
}
};
于 2012-11-12T14:13:17.483 回答
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:

于 2012-11-12T14:13:30.760 回答