XML to SQL table

Hi, Have a problem getting the data from the nodes "ItemID"
It looks like this:

<Body>
        <Line ID="10">
            <ItemID Type="Techdata">2869909</ItemID>
            <ItemID Type="Manufacturer">F7N060B2C00</ItemID>
            <ItemID Type="EAN">722868996157</ItemID>
            <ItemID Type="CommodityCode">4202918090</ItemID>
            <CountryOrigin>TW</CountryOrigin>
            <ItemText>Cinema Stripe Cover Blacktop iPad Air</ItemText>
            <ItemQty Type="delivered">5</ItemQty>
            <ItemWeight Type="Net" Unit="kg">1.6</ItemWeight>
            <ItemReference>
                <CustLine>1</CustLine>
            </ItemReference>
        </Line>

And I need to extract these data:
            <ItemID Type="Techdata">2869909</ItemID>
            <ItemID Type="Manufacturer">F7N060B2C00</ItemID>
            <ItemID Type="EAN">722868996157</ItemID>
            <ItemID Type="CommodityCode">4202918090</ItemID>

How can I get the data in these lines?

Any help is very much appreciated.

Petter.
Petter A. HalsethAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
E.g.

 
DECLARE @Xml XML;
SET @Xml = N'
<Body>
	<Line ID="10">
		<ItemID Type="Techdata">2869909</ItemID>
		<ItemID Type="Manufacturer">F7N060B2C00</ItemID>
		<ItemID Type="EAN">722868996157</ItemID>
		<ItemID Type="CommodityCode">4202918090</ItemID>
		<CountryOrigin>TW</CountryOrigin>
		<ItemText>Cinema Stripe Cover Blacktop iPad Air</ItemText>
		<ItemQty Type="delivered">5</ItemQty>
		<ItemWeight Type="Net" Unit="kg">1.6</ItemWeight>
		<ItemReference>
		<CustLine>1</CustLine>
		</ItemReference>
	</Line>
</Body>
';

SELECT  ItemID.query('.') AS ItemIDXml
FROM    @Xml.nodes('/Body/Line/ItemID') A ( ItemID );

SELECT  Line.value('@ID', 'INT') AS LineID ,        
        ItemID.value('@Type', 'NVARCHAR(255)') AS ItemIDType ,
        ItemID.value('.', 'NVARCHAR(255)') AS ItemID
FROM    @Xml.nodes('/Body/Line') A ( Line )
        CROSS APPLY Line.nodes('ItemID') B ( ItemID );

Open in new window


Please be careful when working with XML. It's case-sensitive and it's necessary that it's well-formed. Your XML is invalid.
Prakash SamariyaIT ProfessionalCommented:
You check if you required as below:

If ItemID -> attribute "Type" has [column name] like
<ItemID TYpe=[ColumnName]>[ColumnValue]</ItemID>  for  <ItemID Type="Techdata">2869909</ItemID>

You can apply below query to get the data:
DECLARE @xmlData XML;
SET @xmlData = N'<Body>
    <Line ID="10">
        <ItemID Type="Techdata">2869909</ItemID>
        <ItemID Type="Manufacturer">F7N060B2C00</ItemID>
        <ItemID Type="EAN">722868996157</ItemID>
        <ItemID Type="CommodityCode">4202918090</ItemID>
        <CountryOrigin>TW</CountryOrigin>
        <ItemText>Cinema Stripe Cover Blacktop iPad Air</ItemText>
        <ItemQty Type="delivered">5</ItemQty>
        <ItemWeight Type="Net" Unit="kg">1.6</ItemWeight>
        <ItemReference>
            <CustLine>1</CustLine>
        </ItemReference>
    </Line>
</Body>';

SELECT
COALESCE([Table].[Column].value('ItemID[@Type="Techdata"][1]', 'int'),0) as 'Techdata',
[Table].[Column].value('ItemID[@Type="Manufacturer"][1]', 'varchar(20)') as 'Manufacturer',
[Table].[Column].value('ItemID[@Type="EAN"][1]', 'varchar(20)') as 'EAN',
[Table].[Column].value('ItemID[@Type="CommodityCode"][1]', 'varchar(50)') as 'CommodityCode'
 FROM @xmlData.nodes('/Body/Line') as [Table]([Column])

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.