如何解析SQL Server表中的XML数据

时间:2018-09-10 10:36:59

标签: sql sql-server xml tsql

我有SQL表:

public class WorldSimGame extends ApplicationAdapter {

    private OrthographicCamera camera;
    private SpriteBatch batch;
    private Texture texture;

    public WorldSimGame() { }

    @Override
    public void create(){
        //Create texture, batch and camera
        texture = new Texture(Gdx.files.internal("badlogic.jpg"));
        batch = new SpriteBatch();
        camera = new OrthographicCamera(60,60);
    }

    @Override
    public void render(){
        //clear screen
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        moveCamera();
        //update camera that he recalculate his position and Matrix
        camera.update();

        batch.setProjectionMatrix(camera.combined); //set batch to draw what the camera sees
        batch.begin();
        batch.draw(texture,0,0); //draw texture
        batch.end();
    }

    private void moveCamera(){
        //move camera
        if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
            camera.translate(4,0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
            camera.translate(-4,0);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.UP)){
            camera.translate(0,4);
        }
        if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
            camera.translate(0,-4);
        }
    }
}

“操作”列包含XML数据。 格式:

Create table
(
ID varchar(50) not null,
Action nvarchar(max) null
)

如何解析此列?结果应该是这样的:

<?xml version="1.0" encoding="UTF-8"?> 
<oo_outbound_order>   
         <oo_master>
              <Code>123</Code>
              <Name>Branan</Name> 
    </oo_master> 
    </oo_outbound_order>

3 个答案:

答案 0 :(得分:3)

有很多关于使用TSQL进行XML解析的教程文章。例如,http://www.sqlserver.info/syntax/parse-xml-with-sql-server/

DECLARE @xml xml
SET @xml = 
'<?xml version="1.0" encoding="UTF-8"?> 
<oo_outbound_order>   
         <oo_master>
              <Code>123</Code>
              <Name>Branan</Name> 
    </oo_master> 
    </oo_outbound_order>'

SELECT 
    n.value('(./Code/text())[1]','int') as CODE
 , n.value('(./Name/text())[1]','Varchar(50)') as NAME
FROM @xml.nodes('/oo_outbound_order/oo_master') as a(n)

答案 1 :(得分:1)

尝试下面的肉类:

DECLARE @XMLData XML = '
<oo_outbound_order>   
    <oo_master>
        <Code>123</Code>
        <Name>Branan</Name> 
    </oo_master> 
</oo_outbound_order>'

SELECT
    [Code] = Node.Data.value('Code', 'INT'),
    [Name] = Node.Data.value('Name', 'NVARCHAR(20)')
    FROM @XMLData.nodes('/oo_outbound_order/oo_master') Node(Data)

答案 2 :(得分:0)

将XML数据存储在字符串列中是一个非常糟糕的主意。

更糟糕的是,您存储的是一个字符串,该字符串声称我被编码为utf-8,但是它存储在NVARCHAR的列中,该列UCS-2(与utf-16几乎相同)。

这意味着,您必须先修复此问题并将字符串转换为XML,然后才能使用它。每当您抓住它时,都必须执行这些昂贵的操作。如果可以更改此设置,则应将XML存储在本机键入的列中。

尽管如此,还是可以做到的。试试这个。

Create table #temp
(
ID varchar(50) not null,
[Action] nvarchar(max) null
)

INSERT INTO #temp(ID,[Action]) VALUES
('test 1',
'<?xml version="1.0" encoding="UTF-8"?> 
<oo_outbound_order>   
         <oo_master>
              <Code>123</Code>
              <Name>Branan</Name> 
    </oo_master> 
    </oo_outbound_order>');

SELECT t.ID
      ,ActionXml.value('(/oo_outbound_order/oo_master/Code/text())[1]','nvarchar(max)') AS CODE
      ,ActionXml.value('(/oo_outbound_order/oo_master/Name/text())[1]','nvarchar(max)') AS [Name]
FROM #temp t
CROSS APPLY(SELECT CAST(REPLACE(t.[Action],'encoding="UTF-8"','encoding="UTF-16"') AS XML) ActionXml) A;

drop table #temp;

提示:如果没有重复的元素,则不需要.nodes(),其他答案表明...