Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple OPENXML parsing question

Status
Not open for further replies.

ratinakage

Programmer
Feb 26, 2006
18
ZA
Hey guys,

If you could run the following SQL command:

I am trying to extract two Addendum elements from the xml string.

DECLARE @string VARCHAR (50)

SET @string = '<Request><Addendum>Addendum1</Addendum><Addendum>Addendum2</Addendum></Request>'

DECLARE @xml_xml_ptr INT

EXEC sp_xml_preparedocument @xml_xml_ptr OUTPUT, @string

SELECT
Addendum
FROM
OPENXML (@xml_xml_ptr, '/Request')
WITH
(
Addendum VARCHAR(79) 'Addendum'
)

EXEC sp_xml_removedocument @xml_xml_ptr

The first error I get is: XML parsing error: The following tags were not closed: Request, Addendum. This seems like valid xml to me.

Any help would be much appreciated.

Thanks!
G
 
oops.

OK, here's the example:

The problem is, it only returns Addendum1 and not Addendum2...

Let me know if you can see a solution.

---------------------------

DECLARE @string VARCHAR (200)

SET @string = '<Request><Addendum>Addendum1</Addendum><Addendum>Addendum2</Addendum></Request>'

DECLARE @xml_xml_ptr INT

EXEC sp_xml_preparedocument @xml_xml_ptr OUTPUT, @string

SELECT
Addendum
FROM
OPENXML (@xml_xml_ptr, '/Request')
WITH
(
Addendum VARCHAR(79) 'Addendum'
)

EXEC sp_xml_removedocument @xml_xml_ptr
 
Thanks! Looking foreward to hearing from you...

Let me add the following: If I change my SQL query to the one below, I get a result set of two rows of NULL. In this case, at least it returns two values which I imagine correspond to the two Addendums. Now if I could only get the data inside...

DECLARE @string VARCHAR (150)

SET @string = '<Request><Addendum>Addendum1</Addendum><Addendum>Addendum2</Addendum></Request>'

DECLARE @xml_xml_ptr INT

EXEC sp_xml_preparedocument @xml_xml_ptr OUTPUT, @string

SELECT
Addendum
FROM
OPENXML (@xml_xml_ptr, '/Request/Addendum') -- Added Addendum here...
WITH
(
Addendum VARCHAR(79) -- 'Addendum' -- removed this...
)

EXEC sp_xml_removedocument @xml_xml_ptr
 
Found the answer!! :D - Woo hoo!! No one told me about the '.' - seems like that's what you need. See the corrected SQL below:


DECLARE @string VARCHAR (150)

SET @string = '<Request><Addendum>Addendum1</Addendum><Addendum>Addendum2</Addendum></Request>'

DECLARE @xml_xml_ptr INT

EXEC sp_xml_preparedocument @xml_xml_ptr OUTPUT, @string

SELECT
Addendum
FROM
OPENXML (@xml_xml_ptr, '/Request/Addendum')
WITH
(
Addendum VARCHAR(79) '.'
)

EXEC sp_xml_removedocument @xml_xml_ptr
 
Sorry was in a meeting, this is what I was talking about more or less

Code:
DECLARE @string VARCHAR (150)

SET @string = '<Request><Addendum>Addendum1</Addendum><Addendum>Addendum2</Addendum></Request>'

DECLARE @xml_xml_ptr INT         
       
EXEC sp_xml_preparedocument @xml_xml_ptr OUTPUT, @string

SELECT 
   Addendum
FROM
   OPENXML (@xml_xml_ptr, '/Request/Addendum')  -- Added Addendum here...
WITH 
   (
      Addendum VARCHAR(79)  '../Addendum'     )

EXEC sp_xml_removedocument @xml_xml_ptr

output
Code:
Addendum1
Addendum1

Denis The SQL Menace
--------------------
SQL Server Code,Tips and Tricks, Performance Tuning
Google Interview Questions





 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top