使用TSQL解析Apple plist

时间:2012-06-15 14:33:44

标签: java sql-server tsql plist

我正在使用SQL Server 2008数据库,该数据库将Apple plists存储在一列中。我正在寻找一种快速而肮脏的方式来提取一些信息。

我知道SQL Server can do XML parsing。由于plist不是真正的XML,有什么可以帮助我查询/操作数据吗?

或者,如果有人建议使用轻量级Java库来解析plists,我也可以选择那条路径。

<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>

1 个答案:

答案 0 :(得分:1)

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