2010-05-09 105 views
0

我想解析一個XML文件使用XmlReader,但雖然我由於某種原因從(XML)文件返回(委託)節點,我也得到一個空的SimpleXMLElement對象也返回。我不知道它是否與循環,開關或我在解析設置中遺漏的東西有關。SimpleXMLElement空對象

這是我嘗試讀取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()和我需要做什麼來糾正它?謝謝。

回答

1

您打開循環兩次打開和關閉標記。您需要檢查節點類型一樣,

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); 
+0

星爺的腳本是從另一個來源,基本上指出的XMLReader對於大型飼料更好,它使用了SimpleXML的各個元素的量小。感謝它現在正確地工作。 – Michael 2010-05-10 07:41:36