如果您只需要提取键/值对,则可以使用一些基本的 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