如果您只需拉取鍵/值對,則可以使用一些基本的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 –