2010-05-09 5 views
0

XmlReader를 사용하여 xml 파일을 구문 분석하려고하지만 xml 파일에서 (커미션) 노드에 대한 반환 값을 받고 있는데 어떤 이유로 빈 SimpleXMLElement 개체가 반환됩니다. 루프, 스위치 또는 구문 분석 설정에서 놓친 뭔가와 관련이 있는지 여부는 알 수 없습니다.SimpleXMLElement 빈 개체

<?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; 

      } 

    } 
    ?> 

결과는 다음과 같습니다이 내 파서

<?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> 

: 반환 단 1 결과가 당신이 볼 수있는

, 나는에서 읽으려고하고있는 XML 파일입니다 :

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

두 번째 개체를 반환하는 이유 : SimpleXMLElement 개체() 그리고 그것을 수정해야 할 필요가 있습니까? 감사.

답변

1

열기 및 닫기 태그에 대해 루프를 두 번 치고 있습니다. 당신은

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

난 당신의 XMLReader, DOM 및 SimpleXML을의 혼합물을 사용하여 달성하려는 모르겠어요, 마찬가지로, nodeType 값을 확인해야합니다. 왜 그냥 스크립트는 기본적으로 그 XMLReader를 큰 피드 더하고 개별 요소의 작은 금액 SimpleXML을 사용한다는, 다른 소스에서입니다

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

예치처럼, SimpleXML을 사용하지 마십시오. 덕분에 제대로 작동하게되었습니다. – Michael

관련 문제