如何将xml读入mysql db

时间:2014-10-20 18:59:44

标签: php arrays xml parsing object

我使用simplexml_load_file从外部站点提取数据,需要转储到我的本地数据库。我不知道如何解析数据,以便我可以执行我的SQL插件。请告诉我如何从simplexml_load_file结果中获取列标题和值。因为我可能并不总是知道标题名称,所以我希望将标题和值作为变量拉出来并将它们作为值加载到sql作为两列; column_nam和column_value。以下是使用print_r

的数据示例

SimpleXMLElement对象 (     [@attributes] =>排列         (             [sessionid] => 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
            (
            )

    )

1 个答案:

答案 0 :(得分: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/