Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用XML和PHP按条件计算项目?_Php_Xml_Count_Get_Conditional - Fatal编程技术网

使用XML和PHP按条件计算项目?

使用XML和PHP按条件计算项目?,php,xml,count,get,conditional,Php,Xml,Count,Get,Conditional,我只想通过EndedWithSales的sellingstatus->sellingstate获取此文件中的项目计数,还可能获取EnededWithoutSales的计数 <sellingStatus> <currentPrice currencyId="USD">25.0</currentPrice> <convertedCurrentPrice currencyId="USD">25.0</convertedCurrent

我只想通过
EndedWithSales
sellingstatus->sellingstate
获取此文件中的项目计数,还可能获取
EnededWithoutSales
的计数

<sellingStatus>
    <currentPrice currencyId="USD">25.0</currentPrice>
    <convertedCurrentPrice currencyId="USD">25.0</convertedCurrentPrice>
    <bidCount>1</bidCount>
    <sellingState>EndedWithSales</sellingState>
</sellingStatus>

25
25
1.
EndedWithSales
我如何在php中传递这个参数

以下是指向XML示例的链接:


可以有人帮助吗?

首先,您需要访问URL thu<代码>文件名:GETYGETFILATION<代码>,请考虑这个例子:

$url = 'http://developer.ebay.com/DevZone/finding/CallRef/Samples/findCompletedItems_basic_out_xml.txt';

// access the url and get that file
$contents = file_get_contents($url);

// convert it to an xml object
$contents = simplexml_load_string($contents);

$count = 0; // initialize counter

// loop and search for that
foreach($contents->searchResult->item as $key => $value) {

    if(isset($value->sellingStatus->sellingState) && $value->sellingStatus->sellingState == 'EndedWithSales') {
        // if that key exists and is contains EndedWithSales, increment it
        $count++;
    }
}

// at the end of the loop echo it or whatever you wanted to do
echo "<script>alert('You have $count ocurrances of EndedWithSales in this XML');</script>";
$url='1!'http://developer.ebay.com/DevZone/finding/CallRef/Samples/findCompletedItems_basic_out_xml.txt';
//访问url并获取该文件
$contents=file\u get\u contents($url);
//将其转换为xml对象
$contents=simplexml\u load\u字符串($contents);
$count=0;//初始化计数器
//循环并搜索它
foreach($contents->searchResult->item as$key=>$value){
如果(设置($value->sellingStatus->sellingState)&&&$value->sellingStatus->sellingState=='EndedWithSales'){
//如果该键存在且包含EndedWithSales,则将其递增
$count++;
}
}
//在循环结束时,回显它或任何你想做的事情
echo“alert('您在这个XML中有EndedWithSales的$count ocurrances');”;

您可以将XML加载到DOMDocument中,为其创建一个Xpath实例,然后获取计数

$url = 'http://developer.ebay.com/DevZone/finding/CallRef/Samples/findCompletedItems_basic_out_xml.txt';
$dom = new DOMDocument();
$dom->load($url);
$xpath = new DOMXpath($dom);

// register a prefix for the namespace used in the document 
$xpath->registerNamespace('ebay', 'http://www.ebay.com/marketplace/search/v1/services');

var_dump(
   $xpath->evaluate(
     'count(
       //ebay:searchResult[1]
       /ebay:item[
         ebay:sellingStatus/ebay:sellingState = "EndedWithSales"
       ]
      )'
   )
);
输出:

double(2)
Xpaths
count()
函数返回表达式定义的中的节点数

文档中的第一个
searchResult
元素:

//易趣:搜索结果[1]

第一个
搜索结果中的
元素

//ebay:searchResult[1]/ebay:item

带有
sellingState
“EndedWithSales”的项目


//ebay:searchResult[1]/ebay:item[ebay:sellingStatus/ebay:sellingState=“EndedWithSales”]

太好了,谢谢!这完全回答了我的问题。我很感激。