2014-10-20 82 views
-1

我使用simplexml_load_file從外部站點提取數據,並需要將數據轉儲到本地數據庫中。我不知道如何解析數據,以便我可以執行我的sql插入。請告訴我如何從simplexml_load_file結果中獲取列標題和值。因爲我可能並不總是知道標題名稱,理想情況下,我想將變量中的標題和值拉入並將它們作爲值加載到兩列中; column_nam和column_value。下面是數據usnig所述的print_r如何將xml讀取到MySQL數據庫

SimpleXMLElement對象 ( [@屬性] =>數組 ( [會話ID] => p_panda143ccfcf692ede95e的一例| 43413dc74e459ecb | 41b5f89600000000 | 41c18afc4b000000 | )

[OBJECT] => SimpleXMLElement Object 
    (
     [@attributes] => Array 
      (
       [name] => clinicalreport 
       [op] => search_filedelivery 
      ) 

     [reports_viewed] => SimpleXMLElement Object 
      (
      ) 

     [reports_delivered] => SimpleXMLElement Object 
      (
      ) 

     [clinicalreport] => 3006163008 
     [orders_transmitted] => SimpleXMLElement Object 
      (
      ) 

     [report_request_date] => 10/7/2014 9:17 AM 
     [reports_undelivered] => SimpleXMLElement Object 
      (
      ) 

     [person_middle_name] => R 
     [reports_printed] => SimpleXMLElement Object 
      (
      ) 

     [creation_date] => SimpleXMLElement Object 
      (
      ) 

     [ownerid] => 3003154010 
     [reports_count] => SimpleXMLElement Object 
      (
      ) 

     [sponsor_name] => LabCorp Birmingham 
     [receivingorganization] => 3003154010 
     [organization_name] => SimpleXMLElement Object 
      (
      ) 

     [sponsor] => 1502182 
     [document] => SimpleXMLElement Object 
      (
      ) 

     [clearance] => 10 
     [report_subject] => HL7 Order Copy 
     [report_service_date] => 10/7/2014 9:19 AM 
     [reports_forwarded] => SimpleXMLElement Object 
      (
      ) 

     [forwarded] => SimpleXMLElement Object 
      (
      ) 

     [std_orders_created] => SimpleXMLElement Object 
      (
      ) 

     [person_last_name] => Angles 
     [is_viewed] => SimpleXMLElement Object 
      (
      ) 

     [is_latest] => SimpleXMLElement Object 
      (
      ) 

     [is_abnormal] => SimpleXMLElement Object 
      (
      ) 

     [expiration_date] => 4/5/2015 9:19 AM 
     [sequence] => SimpleXMLElement Object 
      (
      ) 

     [is_annotated] => SimpleXMLElement Object 
      (
      ) 

     [originalreport] => SimpleXMLElement Object 
      (
      ) 

     [person_account_number] => 1799 
     [filler_order_number] => SimpleXMLElement Object 
      (
      ) 

     [placer_order_number] => SimpleXMLElement Object 
      (
      ) 

     [is_forwarded] => SimpleXMLElement Object 
      (
      ) 

     [report_priority] => SimpleXMLElement Object 
      (
      ) 

     [person] => SimpleXMLElement Object 
      (
      ) 

     [receiving_cg_fname] => SimpleXMLElement Object 
      (
      ) 

     [creation_datetime] => 10/7/2014 9:19 AM 
     [receiving_cg_lname] => SimpleXMLElement Object 
      (
      ) 

     [is_downloaded] => N 
     [receiving_cg_mname] => SimpleXMLElement Object 
      (
      ) 

     [report_status] => NA 
     [lab_code] => MB 
     [ordering_cg_suffix] => SimpleXMLElement Object 
      (
      ) 

     [ordering_cg_fname] => SimpleXMLElement Object 
      (
      ) 

     [ordering_client_id] => 8001631 
     [person_first_name] => Madeleine 
     [report_type] => ORDER 
     [ordering_cg_lname] => SimpleXMLElement Object 
      (
      ) 

     [ordering_cg_mname] => SimpleXMLElement Object 
      (
      ) 

     [previousreport] => SimpleXMLElement Object 
      (
      ) 

     [orderresult] => SimpleXMLElement Object 
      (
      ) 

     [receiving_cg_suffix] => SimpleXMLElement Object 
      (
      ) 

     [psc_orders_created] => SimpleXMLElement Object 
      (
      ) 

     [receiving_client_id] => 8001631 
     [is_printed] => SimpleXMLElement Object 
      (
      ) 

     [content_expiration_date] => 2/4/2015 9:19 AM 
     [person_suffix] => SimpleXMLElement Object 
      (
      ) 

     [abnormals_count] => SimpleXMLElement Object 
      (
      ) 

    ) 

回答

0

我找到了答案。但首先,爲什麼我的問題有一個'-1'。

解決方案 - 我需要在新函數xml2array中發送simplexml_load_file的結果。這個函數爲我提供了每個名字和值。

$myXML = simplexml_load_file($url); 

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

我發現這裏的功能 http://bookofzeus.com/articles/convert-simplexml-object-into-php-array/