0

使用以下代码段,我使用 php 简单的 xml 对象提要从 xml 中检索节点,而不是在给定的父项上检索第一个子元素并将其转换为数组。我的问题:我想获取在洞 XML 对象中具有最多子元素的子元素,并且目标应该转换为数组

/*
 * Retrive XML Nodes from Feed
 * 
 */

public function getXMLNodes() {
    $xml = simplexml_load_file($this->config[0]->link);
    switch (strtolower($this->config[0]->affiliate)) {
        case 'case1':
            $nodes = $xml->product[0]; //here stead of first object I should get that one which has most of chlidren
            break;
        case 'case2':
            $nodes = $xml->productItems->productItem[0];
            break;
        default :
            break;
    }
    $nodes = $this->xml2array($nodes);
    return $nodes;
}


/*
 * Convert Simple XML Object to array
 * 
 * @xml SimpleXMLObject
 * 
 */

function xml2array($xml) {
    $arr = array();
    foreach ($xml as $element) {
        $tag = $element->getName();
        $e = get_object_vars($element);
        if (!empty($e)) {
            $arr[$tag] = $element instanceof SimpleXMLElement ? $this->xml2array($element) : $e;
        } else {
            $arr[$tag] = trim($element);
        }
    }
    return $arr;
}

example XML //在这种情况下,第二个拥有最多的孩子

<?xml version="1.0" encoding="UTF-8"?>
<products>
    <product>
        <name></name>
        <productUrl></productUrl>
        <warranty/>
        <availability></availability>
        <inStock/>
        <shippingCost></shippingCost>
        <deliveryTime></deliveryTime>
        <weight/>
        <size/>
        <brand/>
        <model/>
        <ean/>
        <upc/>
        <isbn/>
        <condition></condition>
        <mpn/>
        <techSpecs/>
        <manufacturer></manufacturer>
        <programName></programName>
        <programLogoPath></programLogoPath>
        <programId></programId>
        <fields>
            <AlsoIncludes></AlsoIncludes>
            <Carrier></Carrier>
            <modelID></modelID>
            <Offertype></Offertype>
            <Processor></Processor>
            <Service></Service>
            <ShippingTax></ShippingTax>
            <StandardImage155x155></StandardImage155x155>
            <StandardImage200x200></StandardImage200x200>
            <Systemtype></Systemtype>
        </fields>
    </product>
    <product>
        <name></name>
        <productUrl></productUrl>
        <imageUrl></imageUrl>
        <description></description>
        <price></price>
        <currency></currency>
        <merchantCategoryName></merchantCategoryName>
        <sku></sku>
        <shortDescription/>
        <promoText/>
        <previousPrice></previousPrice>
        <warranty/>
        <availability></availability>
        <inStock/>
        <shippingCost></shippingCost>
        <deliveryTime></deliveryTime>
        <weight/>
        <size/>
        <brand/>
        <model/>
        <ean/>
        <upc/>
        <isbn/>
        <condition></condition>
        <mpn/>
        <techSpecs/>
        <manufacturer>Dell</manufacturer>
        <programName></programName>
        <programLogoPath></programLogoPath>
        <programId></programId>
        <fields>
            <AlsoIncludes></AlsoIncludes>
            <Carrier></Carrier>
            <modelID></modelID>
            <Offertype></Offertype>
            <Processor></Processor>
            <Service></Service>
            <ShippingTax></ShippingTax>
            <StandardImage155x155></StandardImage155x155>
            <StandardImage200x200></StandardImage200x200>
            <Systemtype></Systemtype>
        </fields>
    </product>
</products>
4

1 回答 1

1

此方法/函数将找到具有最多子节点的节点:

public function getNodeWithMostChildren($container)
{
    $countMax = 0;
    $nodeMax = null;
    foreach($container as $node) {
        $c = $node->count(); // get node count
        foreach ($node as $subnode) { // count subnode's children
            $c += $subnode->count();
        }
        if ($c > $countMax) { // is it larger than current maximum
            $countMax = $c; // set as new maximum
            $nodeMax = $node; // set node
        }
    }
    return $nodeMax;
}

这就是如何将它应用到您的代码(这未经测试,但它应该可以工作):

public function getXMLNodes() {
    $xml = simplexml_load_file($this->config[0]->link);
    switch (strtolower($this->config[0]->affiliate)) {
        case 'case1':
            $nodes = $this->getNodeWithMostChildren($xml->product);
            break;
        case 'case2':
            $nodes = $this->getNodeWithMostChildren($xml->productItems->productItem);
            break;
        default :
            break;
    }
    $nodes = $this->xml2array($nodes);
    return $nodes;
}

// 另外,我已经使用您提供的 xml 测试了代码,这就是结果(对我来说看起来不错 - 它包含节点名称和第二个<product>节点的值):

array(31) {
  ["name"]=>
  string(0) ""
  ["productUrl"]=>
  string(0) ""
  ["imageUrl"]=>
  string(0) ""
  ["description"]=>
  string(0) ""
  ["price"]=>
  string(0) ""
  ["currency"]=>
  string(0) ""
  ["merchantCategoryName"]=>
  string(0) ""
  ["sku"]=>
  string(0) ""
  ["shortDescription"]=>
  string(0) ""
  ["promoText"]=>
  string(0) ""
  ["previousPrice"]=>
  string(0) ""
  ["warranty"]=>
  string(0) ""
  ["availability"]=>
  string(0) ""
  ["inStock"]=>
  string(0) ""
  ["shippingCost"]=>
  string(0) ""
  ["deliveryTime"]=>
  string(0) ""
  ["weight"]=>
  string(0) ""
  ["size"]=>
  string(0) ""
  ["brand"]=>
  string(0) ""
  ["model"]=>
  string(0) ""
  ["ean"]=>
  string(0) ""
  ["upc"]=>
  string(0) ""
  ["isbn"]=>
  string(0) ""
  ["condition"]=>
  string(0) ""
  ["mpn"]=>
  string(0) ""
  ["techSpecs"]=>
  string(0) ""
  ["manufacturer"]=>
  string(4) "Dell"
  ["programName"]=>
  string(0) ""
  ["programLogoPath"]=>
  string(0) ""
  ["programId"]=>
  string(0) ""
  ["fields"]=>
  array(10) {
    ["AlsoIncludes"]=>
    string(0) ""
    ["Carrier"]=>
    string(0) ""
    ["modelID"]=>
    string(0) ""
    ["Offertype"]=>
    string(0) ""
    ["Processor"]=>
    string(0) ""
    ["Service"]=>
    string(0) ""
    ["ShippingTax"]=>
    string(0) ""
    ["StandardImage155x155"]=>
    string(0) ""
    ["StandardImage200x200"]=>
    string(0) ""
    ["Systemtype"]=>
    string(0) ""
  }
}
于 2013-08-26T11:22:10.480 回答