简体   繁体   English

使用$ xpathvar-> query时如何获取单个xml子节点?

[英]How to get individual xml subnodes when using $xpathvar->query?

I try to get the value of name, PreviewUrl, Description, etc when the country name (eg <Country name="US"> ) matches the country of the user from XML results like the one below: 当国家名称(例如<Country name="US"> )与XML结果中的用户所在国家/地区匹配时,我尝试获取name,PreviewUrl,Description等值,如下所示:

<Offers>
   <Offer Id="656">
     <Name>The Hobbit Android</Name>
     <Platform>Android Smartphone & Tablet</Platform>
     <DailyCap>63</DailyCap>
     <PreviewUrl>https://play.google.com/store/apps/details?id=com.kabam.fortress</PreviewUrl>
     <TrackingUrl>http://ads.ad4game.com/www/delivery/dck.php?offerid=656&zoneid=7711</TrackingUrl>
     <Description>The battle for Middle-earth has just begun! PLAY FOR FREE and join thousands worldwide to drive ..</Description>
     <RestrictedTraffic> Social traffic (facebook, etc), Social traffic (facebook, etc) Email MarketingM, Brand Bidding</RestrictedTraffic>
     <Countries>
       <Country name="US">
         <OptIn>SOI</OptIn><Rate>$3.75</Rate>
         <EndDate>2013-09-15 16:47:12</EndDate>
       </Country>
     </Countries>
   </Offer>
 </Offers>

When I try the php code below I get all the Nodes concatenated as one single string. 当我尝试下面的php代码时,我将所有节点连接为一个字符串。 How can I get the name, PreviewUrl, Description, etc. as individual strings when the country name matches? 国家名称匹配时,如何获取名称,PreviewUrl,Description等作为单个字符串?

$xmldoc = new DOMDocument();
        $xmldoc->load('http://traffic.ad4game.com/www/admin/offers-api.php');

        $xpathvar = new Domxpath($xmldoc);



        $queryResult = $xpathvar->query('//Country[@name="US"]/../..');

        foreach($queryResult as $result){
                echo $result->nodeValue;
        }

Edit: As for expected output, I expect that after obtaining the individual strings, I expect it to be something like: 编辑:至于预期的输出,我希望在获得单个字符串之后,我希望它是这样的:

<div id="offer656">
<a href="http://ads.ad4game.com/www/delivery/dck.php?offerid=656&zoneid=7711">
The Hobbit Android</a><br />The battle for Middle-earth has just begun! PLAY FOR FREE and join thousands worldwide to drive .. 
</div>

First, you need to iterate through all child elements of xpath query, so add /* at end. 首先,您需要遍历xpath查询的所有子元素,因此请在末尾添加/*

Second, pass the nodeValue results into an array where you can subscript for individual strings (ie, $value[0] , $value[1] , ...) or run a foreach loop on array: 其次,将nodeValue结果传递到一个数组中,您可以在该数组中为单个字符串下标(即$value[0]$value[1] ,...)或在数组上运行foreach循环:

$xmldoc = new DOMDocument();
$xmldoc->load('http://traffic.ad4game.com/www/admin/offers-api.php');

$xpathvar = new Domxpath($xmldoc);

// ITERATE THROUGH ALL CHILD ELEMENTS 
$queryResult = $xpathvar->query('//Country[@name="US"]/../../*');

$value = [];
foreach($queryResult as $result){
        // PRINTS ALL NODE VALUES OF XPATH AT ONCE
        echo $result->nodeValue;

        // PASSES EACH NODE VALUE OF XPATH INTO $VALUE ARRAY
        $value[] = $result->nodeValue;            
}

// OUTPUT $VALUE ARRAY CONTENTS
var_dump($value);

Use DOMXpath::evaluate() not DOMXPath::query() . 使用DOMXpath::evaluate()而不是DOMXPath::query() The two methods might look the same, but evaluate() supports Xpath expressions that return scalars. 这两个方法可能看起来相同,但是valuate evaluate()支持返回标量的Xpath表达式。 The second argument of the methods is a context. 方法的第二个参数是上下文。 So you can iterate the offer nodes and use additional Xpath expressions to fetch the details: 因此,您可以迭代offer节点并使用其他Xpath表达式来获取详细信息:

$xmldoc = new DOMDocument();
$xmldoc->load('http://traffic.ad4game.com/www/admin/offers-api.php');

$xpath = new Domxpath($xmldoc);
$offers = $xpath->evaluate('//Offer[Countries/Country[@name="US"]]');

foreach($offers as $offer){
  var_dump(
    [
      'name' => $xpath->evaluate('string(Name)', $offer),
      'previewUrl' => $xpath->evaluate('string(PreviewUrl)', $offer)
    ]
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM