【问题标题】:SimpleXMLElement empty objectSimpleXMLElement 空对象
【发布时间】:2010-05-09 21:15:22
【问题描述】:

我正在尝试使用 XmlReader 解析一个 xml 文件,但是尽管出于某种原因我从 xml 文件中获得了(commission)节点的返回值,但我也得到了一个空的 SimpleXMLElement 对象。我不知道它是否与 while 循环、切换或我在解析设置中遗漏的东西有关。

这是我试图读取的 xml 文件,如您所见,只返回 1 个结果:

<?xml version="1.0" encoding="UTF-8"?>
<cj-api>
    <commissions total-matched="1">
        <commission>
            <action-status>
                new
            </action-status>
            <action-type>
                lead
            </action-type>
            <aid>
                10730981
            </aid>
            <commission-id>
                1021015513
            </commission-id>
            <country>
            </country>
            <event-date>
                2010-05-08T08:08:55-0700
            </event-date>
            <locking-date>
                2010-06-10
            </locking-date>
            <order-id>
                345007
            </order-id>
            <original>
                true
            </original>
            <original-action-id>
                787692438
            </original-action-id>
            <posting-date>
                2010-05-08T10:01:22-0700
            </posting-date>
            <website-id>
                3201921
            </website-id>
            <cid>
                2815954
            </cid>
            <advertiser-name>
                SPS EurosportBET
            </advertiser-name>
            <commission-amount>
                0
            </commission-amount>
            <order-discount>
                0
            </order-discount>
            <sid>
                0
            </sid>
            <sale-amount>
                0
            </sale-amount>
        </commission>
    </commissions>
</cj-api>

这是我的解析器:

   <?php

    // read $response (xml feed)
    $file = "datafeed.xml";
    $xml = new XMLReader;

    $xml->open($file);

    // loop to read in data

    while ($xml->read()) {

            switch ($xml->name) {

            // find the parent node for each commission payment
                case 'commission':
            // initalise xml parser
                    $dom = new DomDocument(); 
                    $dom_node = $xml ->expand();
                    $element = $dom->appendChild($dom_node); 
                    $dom_string = $dom->saveXML($element); 
                    $commission = new SimpleXMLElement($dom_string);

                    // read in data

                    $action_status = $commission->{'action-status'};
                    $action_type = $commission->{'action-type'};
                    $aid = $commission->{'aid'};
                    $commission_id = $commission->{'commission-id'};
                    $country = $commission->{'country'};
                    $event_date = $commission->{'event-date'};
                    $locking_date = $commission->{'locking-date'};
                    $order_id = $commission->{'order-id'};
                    $original = $commission->{'original'};
                    $original_action_id = $commission->{'original_action-id'};
                    $posting_date = $commission->{'posting-date'};
                    $website_id = $commission->{'website-id'};
                    $cid = $commission->{'cid'};
                    $advertiser_name = $commission->{'advertiser-name'};
                    $commission_amount = $commission->{'commission-amount'};
                    $order_discount = $commission->{'order-discount'};
                    $sid = $commission->{'sid'};
                    $sale_amount = $commission->{'sale-amount'};


                print_r($aid);
                    break;

            }

    }
    ?>

结果是:

SimpleXMLElement Object ( [0] => 10730981 ) SimpleXMLElement Object ( )

为什么它返回第二个对象:SimpleXMLElement Object (),我需要做些什么来纠正它?谢谢。

【问题讨论】:

    标签: php xml parsing


    【解决方案1】:

    您为打开和关闭标签打了两次循环。你需要检查nodeType,比如,

      if ($xml->name == 'commission' && $xml->nodeType == XMLReader::ELEMENT) {
         // Process the node
      }
    

    我不知道你想通过混合使用 XMLReader、DOM 和 SimpleXML 来完成什么。你为什么不直接使用 SimpleXML,比如

       $xml = simplexml_laod_file($file);
       $commission = $xml->commissions[0]->commission[0];
       $aid = $commission->{'aid'};
       print_r($aid);
    

    【讨论】:

    • 是的,脚本来自另一个来源,基本上说明 xmlreader 更适合大型提要,并且它使用 simplexml 来处理少量单个元素。感谢它现在可以正常工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    相关资源
    最近更新 更多