SQL Server table is converted into the xml file using query

Answered SQL Server table is converted into the xml file using query

  • Wednesday, January 02, 2013 5:37 AM
     
     

    Hi,

    i want how to sql server table record is converted into the xml file using sql script or ssis package.

    Here i have mentioned the tables below.

    Table name is organization

    want this table record is converted into the xml file.

    i want this same type of xml file. Here maintained the hierarchy level, hierarchy is very important.

    <?xml version="1.0" encoding="UTF-8"?>
    -<Root>
    -<Node
    Address="London, 120 Hanover Sq." Branch="TopManagement" Position="CEO" Phone="(205) 555 - 9898" Email="afuller@contoso.com" LastName="Fuller" FirstName="Andrew">

    -<Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="President QA" Phone="(205) 555 - 9888" Email="jboather@contoso.com" LastName="Boather" FirstName="Jeremy">

    -<Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="VP QA" Phone="(205) 555 - 9887" Email="adodsworth@contoso.com" LastName="Dodsworth" FirstName="Anne">

    -<Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="Team Lead Team1" Phone="(205) 555 - 9886" Email="atuckings@contoso.com" LastName="Tuckings" FirstName="Alexander">

    <Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="Senior QA" Phone="(205) 555 - 9885" Email="bsmith@contoso.com" LastName="Smith" FirstName="Brenda"/>
    </Node>

    <Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="Team Lead Team2" Phone="(205) 555 - 9885" Email="mbird@contoso.com" LastName="Bird" FirstName="Mary"/>
    </Node>
    </Node>

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="President Dev Dept." Phone="(205) 555 - 9897" Email="sbuchanan@contoso.com" LastName="Buchanan" FirstName="Steven">

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="VP Dev Dept." Phone="(205) 555 - 9896" Email="rking@contoso.com" LastName="King" FirstName="Robert">

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Team Lead Team1" Phone="(205) 555 - 9892" Email="lcallahan@contoso.com" LastName="Callahan" FirstName="Laura">

    <Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9872" Email="jroland@contoso.com" LastName="Roland" FirstName="Jason">
    </Node>
    </Node>

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Team Lead Team2" Phone="(205) 555 - 9882" Email="edanstin@contoso.com" LastName="Danstin" FirstName="Eric">

    <Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9862" Email="elincoln@contoso.com" LastName="Lincoln" FirstName="Elizabeth">
    </Node>

    <Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9852" Email="mpeacock@contoso.com" LastName="Peacock" FirstName="Margaret">
    </Node>
    </Node>
    </Node>
    </Node>
    </Node>
    </Root>

    i want above mentioned the xml file..

    the table is converted into the above mentioned the xml file using sql query.

    i try this query below mentioned query but this come same hierarchy structure but close node not come that means </node> this tag comes some lines u check above mentioned xml file.

    SELECT s.Address AS [@Address],s.Branch as [@Branch],s.Position as [@Position],s.Phone as [@Phone],
    s.Email as [@Email],S.LastName as [@LastName],s.FirstName as [@FirstName]

    FROM ( SELECT Address,Branch,Position,Phone,Email,FirstName,LastName  FROM Organization
           --GROUP BY Address
          ) AS s
    FOR XML PATH('Node'),
            ROOT('Root')

    this query result comes  this format --

    <Root>
      <Node Address="London, 120 Hanover Sq." Branch="TopManagement" Position="CEO" Phone="(205) 555 - 9898" Email="afuller@contoso.com" LastName="Fuller" FirstName="Andrew" />
      <Node Address="London, 120 Hanover Sq." Branch="QA" Position="President QA" Phone="(205) 555 - 9888" Email="jboather@contoso.com" LastName="Boather" FirstName="Jeremy" />
      <Node Address="London, 120 Hanover Sq." Branch="QA" Position="VP QA" Phone="(205) 555 - 9887" Email="adodsworth@contoso.com" LastName="Dodsworth" FirstName="Anne" />
      <Node Address="London, 120 Hanover Sq." Branch="QA" Position="Team Lead Team1" Phone="(205) 555 - 9886" Email="atuckings@contoso.com" LastName="Tuckings" FirstName="Alexander" />
      <Node Address="London, 120 Hanover Sq." Branch="QA" Position="Senior QA" Phone="(205) 555 - 9885" Email="bsmith@contoso.com" LastName="Smith" FirstName="Brenda" />
      <Node Address="London, 120 Hanover Sq." Branch="QA" Position="Team Lead Team2" Phone="(205) 555 - 9885" Email="mbird@contoso.com" LastName="Bird" FirstName="Mary" />
      <Node Address="London, 120 Hanover Sq." Branch="Development" Position="President Dev Dept." Phone="(205) 555 - 9897" Email="sbuchanan@contoso.com" LastName="Buchanan" FirstName="Steven" />
      <Node Address="London, 120 Hanover Sq." Branch="Development" Position="VP Dev Dept." Phone="(205) 555 - 9896" Email="rking@contoso.com" LastName="King" FirstName="Robert" />
      <Node Address="London, 120 Hanover Sq." Branch="Development" Position="Team Lead Team1" Phone="(205) 555 - 9892" Email="lcallahan@contoso.com" LastName="Callahan" FirstName="Laura" />
      <Node Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9872" Email="jroland@contoso.com" LastName="Roland" FirstName="Jason" />
      <Node Address="London, 120 Hanover Sq." Branch="Development" Position="Team Lead Team2" Phone="(205) 555 - 9882" Email="edanstin@contoso.com" LastName="Danstin" FirstName="Eric" />
      <Node Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9862" Email="elincoln@contoso.com" LastName="Lincoln" FirstName="Elizabeth" />
      <Node Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9852" Email="mpeacock@contoso.com" LastName="Peacock" FirstName="Margaret" />
    </Root>

    i want this structure and some </node> close node tag also i will come. i want  mentioned above first xml file </node> this is mentioned in bold letter. this is i want

    Regards,
    Abdul Khadir.

All Replies

  • Wednesday, January 02, 2013 6:28 AM
     
      Has Code

    Hi ,

                is this what u need?    

    DECLARE @Organization TABLE (FirstName VARCHAR(20),
    LastName VARCHAR(20),
    Email VARCHAR(20),
    Phone BIGINT,
    Position VARCHAR(10),
    Branch VARCHAR(10),
    [Address] VARCHAR(100))
    INSERT @Organization SELECT 'sunny','sara','sunny@mail.com','99990000','CEO','Cananda','Sunny st'
    INSERT @Organization SELECT 'hansi','sat','sat@mail.com','99991234','PM','India','sat st'   
    SELECT [Address],Branch,Position,Phone,Email,LastName,FirstName
    FROM @Organization AS [Organization] FOR XML AUTO ,ELEMENTS


    Thanks & Regards, sathya

  • Wednesday, January 02, 2013 6:47 AM
     
      Has Code

    Hi ,

                is this what u need?    

    DECLARE @Organization TABLE (FirstName VARCHAR(20),
    LastName VARCHAR(20),
    Email VARCHAR(20),
    Phone BIGINT,
    Position VARCHAR(10),
    Branch VARCHAR(10),
    [Address] VARCHAR(100))
    INSERT @Organization SELECT 'sunny','sara','sunny@mail.com','99990000','CEO','Cananda','Sunny st'
    INSERT @Organization SELECT 'hansi','sat','sat@mail.com','99991234','PM','India','sat st'   
    SELECT [Address],Branch,Position,Phone,Email,LastName,FirstName
    FROM @Organization AS [Organization] FOR XML AUTO ,ELEMENTS


    Thanks & Regards, sathya

    Hi,

    i want this xml xml file. same file i need,

    <?xml version="1.0" encoding="UTF-8"?>
    -<Root>
    -<Node
    Address="London, 120 Hanover Sq." Branch="TopManagement" Position="CEO" Phone="(205) 555 - 9898" Email="afuller@contoso.com" LastName="Fuller" FirstName="Andrew">

    -<Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="President QA" Phone="(205) 555 - 9888" Email="jboather@contoso.com" LastName="Boather" FirstName="Jeremy">

    -<Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="VP QA" Phone="(205) 555 - 9887" Email="adodsworth@contoso.com" LastName="Dodsworth" FirstName="Anne">

    -<Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="Team Lead Team1" Phone="(205) 555 - 9886" Email="atuckings@contoso.com" LastName="Tuckings" FirstName="Alexander">

    <Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="Senior QA" Phone="(205) 555 - 9885" Email="bsmith@contoso.com" LastName="Smith" FirstName="Brenda"/>
    </Node>

    <Node
    Address="London, 120 Hanover Sq." Branch="QA" Position="Team Lead Team2" Phone="(205) 555 - 9885" Email="mbird@contoso.com" LastName="Bird" FirstName="Mary"/>
    </Node>
    </Node>

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="President Dev Dept." Phone="(205) 555 - 9897" Email="sbuchanan@contoso.com" LastName="Buchanan" FirstName="Steven">

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="VP Dev Dept." Phone="(205) 555 - 9896" Email="rking@contoso.com" LastName="King" FirstName="Robert">

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Team Lead Team1" Phone="(205) 555 - 9892" Email="lcallahan@contoso.com" LastName="Callahan" FirstName="Laura">

    <Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9872" Email="jroland@contoso.com" LastName="Roland" FirstName="Jason">
    </Node>
    </Node>

    -<Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Team Lead Team2" Phone="(205) 555 - 9882" Email="edanstin@contoso.com" LastName="Danstin" FirstName="Eric">

    <Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9862" Email="elincoln@contoso.com" LastName="Lincoln" FirstName="Elizabeth">
    </Node>

    <Node
    Address="London, 120 Hanover Sq." Branch="Development" Position="Senior Dev" Phone="(205) 555 - 9852" Email="mpeacock@contoso.com" LastName="Peacock" FirstName="Margaret">
    </Node>
    </Node>
    </Node>
    </Node>
    </Node>
    </Root>

    Regards,

    Abdul Khadir.

  • Wednesday, January 02, 2013 7:25 AM
     
     

    Hi

    SQL script

    http://ask.sqlservercentral.com/questions/7158/output-sql-data-as-xml-and-export-to-xml-file.html

    using SSIS. 

    http://blog.jontav.com/post/7612017013/import-xml-into-sql-server-using-ssis


    Ahsan Kabir Please remember to click Mark as Answer and Vote as Helpful on posts that help you. This can be beneficial to other community members reading the thread. http://www.aktechforum.blogspot.com/

  • Wednesday, January 02, 2013 6:24 PM
    Answerer
     
     Answered

    I thought I answered this query pretty thoroughly here:

    http://social.msdn.microsoft.com/Forums/en-US/sqlxml/thread/4f793bf6-1096-4716-8fb9-0e1e56936cab

    Have you worked through that?