ibcmis
asked on
SQL Server 2005 OpenXML - How do you load XML data containing nested elements/tags?
Hi,
Amazingly, I have been unable to easily find an answer to this simple question.
The problem is that there are plenty of blogs/articles on using OpenXml, but the examples use the lamest, most simplistic, single-level XML documents to load.
Ok, for example, I have table Contacts with the following colums:
ContactName
PhoneType
PhoneNum
The XML parameter data looks like:
<XMLDoc>
<Contacts>
<Name>John Doe</Name>
<Phone>
<Type>Cell</Type>
<Number>555-555-5555</Numb er>
</Phone>
</Contacts>
</XML Doc>
So, in the SPROC, to load the input XML, the SQL would resemble:
...
INSERT Contacts(
ContactName,
PhoneType,
PhoneNum
)
SELECT Name,
Type
Number
FROM OPENXML (@XML, '/Contacts',2)
WITH ( Name nvarchar (100),
?
?
)
My question is, how do you specify the data in nested "Phone" element?
Any and all help greatly appreciated.
Tony
Amazingly, I have been unable to easily find an answer to this simple question.
The problem is that there are plenty of blogs/articles on using OpenXml, but the examples use the lamest, most simplistic, single-level XML documents to load.
Ok, for example, I have table Contacts with the following colums:
ContactName
PhoneType
PhoneNum
The XML parameter data looks like:
<XMLDoc>
<Contacts>
<Name>John Doe</Name>
<Phone>
<Type>Cell</Type>
<Number>555-555-5555</Numb
</Phone>
</Contacts>
</XML Doc>
So, in the SPROC, to load the input XML, the SQL would resemble:
...
INSERT Contacts(
ContactName,
PhoneType,
PhoneNum
)
SELECT Name,
Type
Number
FROM OPENXML (@XML, '/Contacts',2)
WITH ( Name nvarchar (100),
?
?
)
My question is, how do you specify the data in nested "Phone" element?
Any and all help greatly appreciated.
Tony
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Just some minor typos in your Xml and the solution provided (please no points):
<XMLDoc>
<Contacts>
<Name>John Doe</Name>
<Phone>
<Type>Cell</Type>
<Number>555-555-5555</Number>
</Phone>
</Contacts>
</XMLDoc>
SELECT Name,
PhoneType,
PhoneNum
FROM OPENXML (@idoc, '/XMLDoc/Contacts',2)
WITH ( Name nvarchar (100),
PhoneType varchar(10) 'Phone/Type',
PhoneNum varchar(10) 'Phone/Number')
Having said that, you may find the given an Xml structure like this:
<XMLDoc>
<Contacts>
<Name>John Doe</Name>
<Phone>
<Type>Cell</Type>
<Number>555-555-5555</Numb er>
</Phone>
<Phone>
<Type>Business</Type>
<Number>666-666-6666</Numb er>
</Phone>
</Contacts>
</XMLDoc>
The following OPENXML query is more generic and will give you all the phones, not just the first one:
<XMLDoc>
<Contacts>
<Name>John Doe</Name>
<Phone>
<Type>Cell</Type>
<Number>555-555-5555</Numb
</Phone>
<Phone>
<Type>Business</Type>
<Number>666-666-6666</Numb
</Phone>
</Contacts>
</XMLDoc>
The following OPENXML query is more generic and will give you all the phones, not just the first one:
SELECT Name,
PhoneType,
PhoneNum
FROM OPENXML (@idoc, '/XMLDoc/Contacts/Phone', 2) WITH (
Name nvarchar (100) '../Name',
PhoneType varchar(10) 'Type',
PhoneNum varchar(10) 'Number')
ASKER
Nativ,
Thank you very much. The snippet you provided was what I needed.
Tony
Thank you very much. The snippet you provided was what I needed.
Tony
ASKER
Short, sucinct, and correct. Couldn't ask for more.
ASKER
acperkins,
Thank you for your additional example...I believe you read my mind.
Although my current project doesn't require it, I was also thinking of just the scenario you astutely addressed in your example. I just didn't want to muddy the waters by bringing it up.
Thanks again for your contribution.
Tony
Thank you for your additional example...I believe you read my mind.
Although my current project doesn't require it, I was also thinking of just the scenario you astutely addressed in your example. I just didn't want to muddy the waters by bringing it up.
Thanks again for your contribution.
Tony
Thank you and I appreciate the comments and the explanation.
ASKER
Thank you for your response.
Your code looks like it should work, and as soon as I try it, I will "accept" it as the solution.
Thanks again.