简体   繁体   中英

Parsing a xml string without the root elements using T-SQL

Below is the XML string stored in a column in my database table and I need to parse it using T-SQL

Installation type:Interior

Wiring Length (ft):4

Connector type:RJ45

Location description:basement

WirelessID:1

DevID:1234567

wontTurnOff:true

How do I parse this since they're missing the root and child tags?

Thanks.

It appears that the data format in your table column is not an XML format. It normally looks like the following and the column type will be set as "XML". Note that there is a hierarchy and each item associated with the book has a beginning and end tag. There can be multiple books under catalog.

<catalog>
  <book id="bk101">
     <author>Gambardella, Matthew</author>
     <title>XML Developer's Guide</title>
     <genre>Computer</genre>
     <price>44.95</price>
     <publish_date>2000-10-01</publish_date>
     <description>An in-depth look at creating applications with XML.</description>
  </book>
</catalog>

However, having pointed out the above, I'm assuming that you probably are dealing with something somewhat different that may have initially of been extracted for an XML source and placed into the format that you've provided. If the format and seven items to parse out are consistent (with different values from one record to the next in the table), then here is an alternative that may assist. This is an example that you can copy into a query and test and use as a reference in the final SQL that will probably need to include a conversion to integer for some of your numeric values parsed out. This example will accommodate the differing length of the values associated with each item. However, in order for this to work, the items must follow in the same order as you provided in your example.

DECLARE @string varchar(max)
SET @string = 'Installation type:Interior
Wiring Length (ft):4
Connector type:RJ45
Location description:basement
WirelessID:1
DevID:1234567
wontTurnOff:true'


DECLARE  @Installation_type varchar(50)
    ,@Wiring_Length varchar(10)
    ,@Connector_type varchar(10)
    ,@Location_Description varchar(100)
    ,@WirelessID varchar(10)
    ,@DevID varchar(10)
    ,@wontTurnOff varchar(5)

 SELECT SUBSTRING(@string, charindex('Installation type:', @string)+18, (charindex('Wiring Length (ft):', @string)-2) - (charindex('Installation type:', @string)+18))
 SELECT SUBSTRING(@string, charindex('Wiring Length (ft):', @string)+19, (charindex('Connector type:', @string)-2) - (charindex('Wiring Length (ft):', @string)+19))
 SELECT SUBSTRING(@string, charindex('Connector type:', @string)+15, (charindex('Location description:', @string)-2) - (charindex('Connector type:', @string)+15))
 SELECT SUBSTRING(@string, charindex('Location description:', @string)+21, (charindex('WirelessID:', @string)-2) - (charindex('Location description:', @string)+21))
 SELECT SUBSTRING(@string, charindex('WirelessID:', @string)+11, (charindex('DevID:', @string)-2) - (charindex('WirelessID:', @string)+11))
 SELECT SUBSTRING(@string, charindex('DevID:', @string)+6, (charindex('wontTurnOff:', @string)-2) - (charindex('DevID:', @string)+6))
 SELECT SUBSTRING(@string, charindex('wontTurnOff:', @string)+12, (len(@string)+1) - (charindex('wontTurnOff:', @string)+12))

It is common for data to arrive in all kinds of strange formats and sometimes they are fragmented. If your data is truly XML and needs to be parsed as such, then you assign your values to a temporary varchar(max) variable, perform find and replaces within it for each item (for example convert "WirelessID:1" to "1">) concatenate the entire value in a topmost hierarchy XML Tag like "" used in the initial XML example, convert the entire varchar(max) value to an XML and then apply the XML parse function included with SQL.

Hope this helps. Without seeing your data I can only suggest the two options. I've been doing ETL development for many years and it is common to be required to use every trick in the book to resolve data formatting issues when there is no control over that source.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM