如何使用PHP将XML内容插入到mysql数据库中

时间:2014-02-07 06:12:22

标签: php mysql xml

我需要使用PHP将XML内容插入到mysql数据库中。我已经完成了简单的XML导入。但是这个XML包含许多嵌套节点。 我对如何使用它非常困惑。 所以请帮我插入内容。

这是我的XML

    <?xml version="1.0" encoding="UTF-8"?>
<properties>
  <pagination xmlns:xlink="http://www.w3.org/1999/xlink">
    <current_page rel="current_page">1</current_page>
    <per_page record_type="integer">50</per_page>
    <total_entries record_type="integer">1</total_entries>
    <total_pages record_type="integer">1</total_pages>
  </pagination>
  <property>
    <id>1344982</id>
    <unique_id></unique_id>
    <xml_id></xml_id>
    <third_contact_id>128555</third_contact_id>
    <headline>Test DiY</headline>
    <description>fghfd hdfgh fgh</description>
    <country>Australia</country>
    <deal_type></deal_type>
    <display_address>1</display_address>
    <display_price>1</display_price>
    <display_price_text></display_price_text>
    <latitude>-25.274398</latitude>
    <longitude>133.775136</longitude>
    <price>15082</price>
    <to_price>16000</to_price>
    <property_type>Warehouse</property_type>
    <save_status>Submitted</save_status>
    <state>NSW</state>
    <status>available</status>
    <street_number>12/22 </street_number>
    <street>fasdfa sdf</street>
    <suburb>MOUNT AUSTIN</suburb>
    <town_village>Riverina</town_village>
    <unit_number>1</unit_number>
    <vendor_email></vendor_email>
    <vendor_first_name></vendor_first_name>
    <vendor_last_name></vendor_last_name>
    <vendor_phone></vendor_phone>
    <zipcode>2650</zipcode>
    <sold_on></sold_on>
    <sold_price></sold_price>
    <leased_on></leased_on>
    <leased_price></leased_price>
    <agent_id>12566</agent_id>
    <office_id>13275</office_id>
    <type>ResidentialSale</type>
    <show_price></show_price>
    <rca_id></rca_id>
    <rea_unique_id></rea_unique_id>
    <domain_id></domain_id>
<detail>
  <auction-date nil="true"></auction-date>
  <auction-place></auction-place>
  <auction-price nil="true"></auction-price>
  <auction-time nil="true"></auction-time>
  <bathrooms>2</bathrooms>
  <bedrooms>4</bedrooms>
  <builder-name nil="true"></builder-name>
  <capital-growth></capital-growth>
  <carport-spaces>1</carport-spaces>
  <closing-date nil="true"></closing-date>
  <condo-strata-fee nil="true"></condo-strata-fee>
  <condo-strata-fee-period></condo-strata-fee-period>
  <energy-efficiency-rating>0.5</energy-efficiency-rating>
  <estimate-rental-return nil="true"></estimate-rental-return>
  <estimate-rental-return-period></estimate-rental-return-period>
  <ext-link-1></ext-link-1>
  <ext-link-2></ext-link-2>
  <floor-area>480</floor-area>
  <floor-area-metric></floor-area-metric>
  <forthcoming-auction>false</forthcoming-auction>
  <garage-spaces>1</garage-spaces>
  <half-bathroom>true</half-bathroom>
  <half-bedroom>true</half-bedroom>
  <id>780069</id>
  <land-area>0.80</land-area>
  <land-area-metric>Acres</land-area-metric>
  <land-crossover></land-crossover>
  <land-depth-left>50.0</land-depth-left>
  <land-depth-left-metric>Feet</land-depth-left-metric>
  <land-depth-rear nil="true"></land-depth-rear>
  <land-depth-rear-metric></land-depth-rear-metric>
  <land-depth-right>50.0</land-depth-right>
  <land-depth-right-metric>Feet</land-depth-right-metric>
  <land-frontage>100.0</land-frontage>
  <land-frontage-metric nil="true"></land-frontage-metric>
  <living-area nil="true"></living-area>
  <living-area-metric nil="true"></living-area-metric>
  <method-of-sale nil="true"></method-of-sale>
  <new-construction>false</new-construction>
  <number-of-floors>1</number-of-floors>
  <off-street-spaces>1</off-street-spaces>
  <property-url></property-url>
  <rental-yield></rental-yield>
  <residential-sale-id>1344982</residential-sale-id>
  <tax-rate nil="true"></tax-rate>
  <tax-rate-period></tax-rate-period>
  <virtual-tour></virtual-tour>
  <water-rate nil="true"></water-rate>
  <water-rate-period></water-rate-period>
  <year-built nil="true"></year-built>
</detail>
<features>
  <feature>
    <name>Entertainment Area</name>
  </feature>
  <feature>
    <name>Gym</name>
  </feature>
  <feature>
    <name>Safe</name>
  </feature>
</features>
<primary-contact>
  <first-name>Josh </first-name>
  <id>128555</id>
  <last-name>Tonkes</last-name>
</primary-contact>
<secondary-contact>
  <first-name>Josh </first-name>
  <id>128555</id>
  <last-name>Tonkes</last-name>
</secondary-contact>
<vendor-details>
  <description></description>
  <email>josh@myhouserealty.com.au</email>
  <fax></fax>
  <first-name>Josh </first-name>
  <group>Sales Agent</group>
  <id>128555</id>
  <im-username nil="true"></im-username>
  <last-name>Tonkes</last-name>
  <login>josh@myhouserealty.com.au</login>
  <mobile></mobile>
  <office-id>13275</office-id>
  <phone></phone>
</vendor-details>
    <photos>
      <photo position="0">
        <large>http://img.agentaccount.com/5a60a25faa272cef83d0e2b369c5363acad8922a</large>
        <medium>http://img.agentaccount.com/bd6492caa582b85c1fa921acaba37600f613dfa0</medium>
        <thumb>http://img.agentaccount.com/de6512a4afb33e7ebf9b6159d15b5b0392125eb8</thumb>
        <original>http://img.agentaccount.com/ddefad55c562ad7568525a2144869062e70fee29</original>
      </photo>
    </photos>
    <floorplans>
    </floorplans>
    <brochures>
      <brochure position="0">
        <title></title>
        <large></large>
      </brochure>
      <brochure position="1">
        <title></title>
        <large></large>
      </brochure>
      <brochure position="2">
        <title></title>
        <large></large>
      </brochure>
      <brochure position="3">
        <title></title>
        <large></large>
      </brochure>
    </brochures>
    <audio></audio>
    <property_videos>
    </property_videos>
    <updated_at>2013-12-14 11:49:39 +1100</updated_at>
    <created_at>2013-12-14 11:47:14 +1100</created_at>
  </property>
</properties>

提前致谢..

1 个答案:

答案 0 :(得分:0)

我已将数据检索到数组中并存储在数据库表中。

    <?php
        // load the xml file
        $xml=simplexml_load_file("xxx.xml");
         echo "<pre>";
          //print_r($xml);

         $arrProperty = (array)$xml;
        //  echo "<pre>";
        // print_r($arrProperty);

        // get the property basic details

          $propertyInfo = (array)$arrProperty['property'];

        // print_r($propertyInfo);
        if($propertyInfo['id'] != ''){      // if the property id exist..



            // PROPERTY MASTER SECTION STARTS HERE....
            // insert the basic details to a property master table
            /*
             * the following fields should insert the database...
             * third_contact_id
             * headline
             * description
             * country
             * display_address
             * display_price
             * latitude
             * longitude
             * price
             * to_price
             * property_type
             * save_status
             * state
             * status
             * street_number
             * street
             * suburb
             * town_village
             * unit_number
             * vendor_email
             * zipcode
             * agent_id
             * office_id
             * type
             * and the other fields too.
             */
            echo $propertyInfo['id'];
            // Add the above details to tbl_property_master...
            //............
            // get the last insert id too...
            // $propertyid = mysql_last_insertid();

            // db insertion code ends here
            // PORPERTY MASTER SECTION ENDS..




            // PROPERTY DETAILS SECTION STARTS HERE
            // insert the details into another table tbl_property_details
              print_r($propertyInfo['detail']);
            // PROPERTY DETAILS SECTION ENDS HERE..



            // PROPERTY FETAURES SECTIONS STARTS HERE..
              print_r($propertyInfo['features']);
            // PROPERTY FEATURES SECTION ENDS HERE



            // insert the contact details into table tbl_property_contact
            print_r($propertyInfo['primary-contact']);
            print_r($propertyInfo['secondary-contact']);
            // contact section ends


            // insert the vendor details into table tbl_vendor
            print_r($propertyInfo['vendor-details']);    
            // vendor section ends

        }
    ?>