Solved

XML Get values in ColumnA

Posted on 2011-02-17
2
948 Views
Last Modified: 2012-05-11
I am trying to get the TSQL code in SQL Server to parse the XML to show records in ColumnA
declare @XmlDoc xml, 
@iDoc integer 
select @XmlDoc = '<VendorCodesRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="String" TimeStamp="2011-02-17T16:29:05" Version="2003A.TsabreXML1.0.1" SequenceNmbr="1" PrimaryLangID="en-us" AltLangID="en-us">
  <Success xmlns="http://webservices/XML/2003/07" />
  <VendorInfo xmlns="http://webservices/XML/2003/07">
    <Row RPH="1">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB />
      <ColumnC>
        <Text>LetterA</Text>
      </ColumnC>
    </Row>
    <Row RPH="2">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AB</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAB</Text>
      </ColumnC>
    </Row>
    <Row RPH="3">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AC</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAC</Text>
      </ColumnC>
    </Row>
  </VendorInfo>
  <TPA_Extensions xmlns="http://webservices/XML/2003/07">
    <HostCommand>DU*/CLS</HostCommand>
  </TPA_Extensions>
</VendorCodesRS>' 

exec sp_xml_preparedocument @iDoc output, @XmlDoc
select * from openxml
(@iDoc, 'VendorInfo',2) 
WITH 
(
ColumnA varchar(max) 'ColumnA/Text'
)

EXEC sp_xml_removedocument @idoc

Open in new window

0
Comment
Question by:garbertravel
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 37

Accepted Solution

by:
ValentinoV earned 500 total points
ID: 34924044
I found a way to make it work if I remove the namespace definition from the VendorInfo node (see snippet).  But unfortunately I haven't succeeded in getting it to work without any modifications to the XML.

Hopefully this will get you going though?
declare @XmlDoc xml = '<VendorCodesRS xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" EchoToken="String" TimeStamp="2011-02-17T16:29:05" Version="2003A.TsabreXML1.0.1" SequenceNmbr="1" PrimaryLangID="en-us" AltLangID="en-us">
  <Success xmlns="http://webservices/XML/2003/07" />
  <VendorInfo>
    <Row RPH="1">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB />
      <ColumnC>
        <Text>LetterA</Text>
      </ColumnC>
    </Row>
    <Row RPH="2">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AB</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAB</Text>
      </ColumnC>
    </Row>
    <Row RPH="3">
      <ColumnA>
        <Text>A</Text>
      </ColumnA>
      <ColumnB>
        <Text>AC</Text>
      </ColumnB>
      <ColumnC>
        <Text>LetterAC</Text>
      </ColumnC>
    </Row>
  </VendorInfo>
  <TPA_Extensions xmlns="http://webservices/XML/2003/07">
    <HostCommand>DU*/CLS</HostCommand>
  </TPA_Extensions>
</VendorCodesRS>';

select Col.value(N'./Text[1]', N'nvarchar(100)') as ColumnA
from @XmlDoc.nodes(N'//VendorInfo/Row/ColumnA') Tab(Col);

Open in new window

0
 

Author Closing Comment

by:garbertravel
ID: 34929971
The data resides in a SQL field and I am just doing a find replace before loading the data. Thanks!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question