Solved

XML Get values in ColumnA

Posted on 2011-02-17
2
952 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I have a large data set and a SSIS package. How can I load this file in multi threading?
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

632 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