gpt4 book ai didi

php - 解析 XML 并回显结果

转载 作者:行者123 更新时间:2023-12-04 05:24:35 27 4
gpt4 key购买 nike

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

任何帮助将不胜感激

谢谢

最佳答案

您可以使用 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 />";
}

关于php - 解析 XML 并回显结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13345184/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com