简体   繁体   中英

Select what data to import from XML to mySQL Table

I need to import a really big XML file into my MySQL table. The problem is that I need to choose what data to input. For example the XML is like this:

<FLIGHT>
<Airline>SAS</Airline>
<FlightNumber>1</FlightNumber>
<CommercialFlightNumber>SK1</CommercialFlightNumber>
<From>LLA</From>
<To>ARN</To>
<PreparedAt>2015/01/30 00:00</PreparedAt>
<Aircraft>LN-RGI</Aircraft>
<AircraftType>0</AircraftType>
<ClimbMode>1</ClimbMode>
<CruiseMode>100600</CruiseMode>
<DescentMode>1</DescentMode>
<InitAlt>MAX</InitAlt>
<StepClimb>-1</StepClimb>
<MTOW>-1</MTOW>
<MLW>-1</MLW>
<STD>2015/1/19 06:00</STD>
<STA>2015/1/19 07:30</STA>
<MaxPax>0</MaxPax>
<MaxCargo>0</MaxCargo>
<RandomPayload>0</RandomPayload>
<Type>0</Type>
<Repetative>1</Repetative>
<IsMaster>1</IsMaster>
<BeginDate>2015/02/04 00:00</BeginDate>
<EndDate>2015/04/30 23:59</EndDate>
<Days>1</Days>
<Baggage>-1</Baggage>
<Payload>-1</Payload>
<Notes>7388</Notes>
</FLIGHT>

But I only need the "CommercialFlightNumber", "From", "To", "Aircraft" and "Days". I had a look at this answer and tried to put my information in a php file but the file got to big and the program crashed.. Is there anyway to do this? Thank you!

Look into SQL Server's nodes() :

The nodes() method is useful when you want to shred an xml data type instance into relational data. It allows you to identify nodes that will be mapped into a new row. [ MSDN ]

Example for inserting only particular XML elements into a table :

declare @table TABLE(CommFlightNo varchar(10), 
                     [From] varchar(3), [To] varchar(3), 
                     Aircraft varchar(10), Days int)
declare @xml xml = '...xml posted in question....'


INSERT INTO @table
SELECT F.x.value('CommercialFlightNumber[1]', 'varchar(10)') as 'ComFlightNo'
      ,F.x.value('From[1]', 'varchar(3)') as 'From'
      ,F.x.value('To[1]', 'varchar(3)') as 'To'
      ,F.x.value('Aircraft[1]', 'varchar(10)') as 'Aircraft'
      ,F.x.value('Days[1]', 'int') as 'Days'
FROM @xml.nodes('//FLIGHT') as F(x)

SQL Fiddle

I did this by inserting the whole XML file into the table with the code

 LOAD XML LOCAL INFILE '/path/to/file.xml' INTO MY_TABLE

and then I just deleted the columns that I didn't needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM