키/값 쌍을 가져와야하는 경우 몇 가지 기본 TSQL 문자열 구문 분석을 사용할 수 있습니다. 아래 코드를 키/값/유형 테이블을 반환하는 함수로 바꿀 수 있어야합니다. 이 코드를 붙여넣고 실행하면 무슨 뜻인지 알 수 있습니다. [ID], [Key], [Value] 및 [ValueType] 열이있는 테이블을 반환합니다. 단일 plist를 전달하면 빠릅니다.
DECLARE @pList VARCHAR(255)
SET @pList =
'<dict>
<key>BundleSize</key>
<integer>16138240</integer>
<key>DynamicSize</key>
<integer>7569408</integer>
<key>Identifier</key>
<string>com.ea.scrabble.ipad.inc2</string>
<key>Name</key>
<string>Scrabble</string>
<key>Version</key>
<string>1.15.73</string>
</dict>
'
DECLARE @IsKey BIT
DECLARE @ID INT
DECLARE @KeyValue TABLE (
ID INT PRIMARY KEY IDENTITY(1,1) NOT NULL,
[Key] VARCHAR(255) NOT NULL,
[Value] VARCHAR(255) NULL,
[ValueType] VARCHAR(255) NULL
)
SET @IsKey = 1
WHILE LEN(@pList) > 10
BEGIN
IF @IsKey = 1
BEGIN
-- Remove junk at the beginning of the string:
SELECT @pList = SUBSTRING(@pList, CHARINDEX('<key>', @pList) + 5, LEN(@pList))
-- Parse out the first value between the <key></key> tags:
INSERT INTO @KeyValue ([Key])
SELECT LEFT(@pList, CHARINDEX('</', @pList)-1)
SELECT @ID = SCOPE_IDENTITY()
-- Remove new junk at the beginning of the string:
SELECT @pList = LTRIM(SUBSTRING(@pList, CHARINDEX(CHAR(13), @pList)+2, LEN(@pList)))
SET @IsKey = 0
END
ELSE -- Is a value
BEGIN
-- Parse out the ValueType and Value:
UPDATE @KeyValue
SET ValueType = (SELECT SUBSTRING(@pList, 2, CHARINDEX('>', @pList)-2)),
Value = (SELECT SUBSTRING(@pList, CHARINDEX('>', @pList)+1, CHARINDEX('</', @pList) - CHARINDEX('>', @pList)-1))
WHERE ID = @ID
-- Remove new junk at the beginning of the string:
SELECT @pList = LTRIM(SUBSTRING(@pList, CHARINDEX(CHAR(13), @pList)+2, LEN(@pList)))
SET @IsKey = 1
END
END
SELECT *
FROM @KeyValue
DMBS는 무엇을 사용하고 있습니까? Oracle과 SQL Server에는 독점적 인 XML 구문 분석 도구가 있습니다. –
MS SQL Server 2008 – tbone14
약간의 통찰력을 줄 수 있습니다. - http://stackoverflow.com/a/9207497/763026 –