【问题标题】:SimpleXML: how to get node value with a condition on parse rule?SimpleXML:如何根据解析规则的条件获取节点值?
【发布时间】:2016-12-12 17:53:22
【问题描述】:

我有一个由外部实体生成的 XML 文件,我想使用 SimpleXML 对其进行解析。我的问题是,在客户给出的映射中,我有一些条件来获取我想要的信息。

例如,客户端代码的映射是这样的:E1ADRM1\PARTNER_Q=OSO\E1ADRE1\EXTEND_D,这意味着客户端的代码是 EXTEND_D 标记的值,它嵌套在许多 PARTNER_Q 标记之一中.具有OSO 值的那个。

我今天开始探索 SimpleXML,所以我不知道如何获取此信息。

就我目前所阅读的内容而言,获取节点的信息并访问它的属性非常简单。如果我是一个 PARTNER_Q 并且没有条件,我的 $clientCode 将是 $xml->E1ADRM1->PARTNER_Q->E1ADRE1->EXTEND_D(对吗?)

关于如何在考虑到PARTNER_Q=OSO 条件的情况下获取信息的任何提示?

【问题讨论】:

    标签: php parsing simplexml conditional-statements


    【解决方案1】:

    为了将来的参考,我将在这里留下我如何解决这个问题的代码。 我的代码基于我找到的 well written answer

    $root = $xml->IDOC;
    $shippingPoints = array();
    // Go through the list of OTs       
    foreach($root->E1EDL20 as $ot) {
    
        // Instanciate empty OT
        $otInfo = emptyOt();
        $otInfo["refOt"] = trim($ot->VBELN);
    
        // Go through partner to get the wanted items
        foreach ($ot->E1ADRM1 as $partner) {
                switch ($partner->PARTNER_Q) {
                        case "OSO":
                                $otInfo["codeLoader1"] = trim($partner->E1ADRE1->EXTEND_D);
    
                                break;
                        case "OSP":
                                $otInfo["refShip"] = trim($partner->PARTNER_ID);
                                // get the shipping info if not already fetched
                                if(!isset($shippingPoints[$otInfo["refShip"]])) {
                                        $shippingPoints[$otInfo["refShip"]] = Whouses::getWhouseAddressByCode($otInfo["refShip"]);
                                }
                                // assign values
                                $otInfo["brandNameShip"] = $shippingPoints[$otInfo["refShip"]]["name"];
                                $otInfo["addrShip1"] = $shippingPoints[$otInfo["refShip"]]["address"];
                                $otInfo["cityShip"] = $shippingPoints[$otInfo["refShip"]]["city"];
                                $otInfo["zipShip"] = $shippingPoints[$otInfo["refShip"]]["zipcode"];
                                $otInfo["countryShip"] = $shippingPoints[$otInfo["refShip"]]["country"];
    
                                break;
                        case "WE":
                                $otInfo["refDeliv"] = trim($partner->PARTNER_ID);
                                $otInfo["addrDeliv1"] = trim($partner->STREET1);
                                $otInfo["cityDeliv"] = trim($partner->CITY1);
                                $otInfo["zipDeliv"] = trim($partner->POSTL_COD1);
                                $otInfo["countryDeliv"] = trim($partner->COUNTRY1);
                        default:
                                // do nothing
                                break;
                }
        }
    
        // Go through the dates info to get the wanted items
        foreach ($ot->E1EDT13 as $qualf) {
                switch ($qualf->QUALF) {
                        case "006":
                                $dtDeliv = trim($qualf->NTANF);
                                $timeDeliv = trim($qualf->NTANZ);
                                $otInfo["initialDtDeliv"] = convertToOtDateFormat($dtDeliv, $timeDeliv);
    
                                break;
                        case "007":
                                $initialDtShip = trim($qualf->NTANF);
                                $timeInitialDtShip = trim($qualf->NTANZ);
                                $otInfo["initialDtShip"] = convertToOtDateFormat($initialDtShip, $timeInitialDtShip);
    
                                break;
                        default:
                                // do nothing
                                break;
                }
        }
    }
    

    希望这会对某人有所帮助!

    【讨论】:

      猜你喜欢
      • 2011-08-01
      • 1970-01-01
      • 2011-02-18
      • 2016-10-16
      • 1970-01-01
      • 2012-10-17
      • 1970-01-01
      • 1970-01-01
      • 2012-12-13
      相关资源
      最近更新 更多