Reading xml child node data

  • declare @x xml

    set @x = '<ROOT>

    <Createstudent>

    <id>30</id>

    <classId>1</classId>

    <lastname>ln</lastname>

    <midname>mn</midname>

    <firstname>fn</firstname>

    <degree>ms</degree>

    <address>

    <city>false</city>

    <state>false</state>

    <zipcode>false</zipcode>

    </address>

    </Createstudent>

    </ROOT>'

    select

    T.c.value('id[1]', 'int') as id,

    T.c.value('classId[1]', 'int') as classid,

    T.c.value('lastname[1]', 'varchar(50)') as lastname,

    T.c.value('midname[1]', 'varchar(50)') as midname,

    T.c.value('firstname[1]', 'varchar(50)') as firstname,

    T.c.value('degree[1]', 'varchar(50)') as degree,

    T.c.value('city[1]', 'varchar(50)') as city,

    T.c.value('state[1]', 'varchar(50)') as state,

    T.c.value('zipcode[1]', 'varchar(50)') as zipcode

    from

    @x.nodes('//Createstudent') T(c)

    in the above code I am able to parse the data until the node degree. But not able to parse the data for the child node 'address'.

    How can I do this?

    Thanks.

  • Add address node as below

    //--------------------

    declare @x xml

    set @x = '<ROOT>

    <Createstudent>

    <id>30</id>

    <classId>1</classId>

    <lastname>ln</lastname>

    <midname>mn</midname>

    <firstname>fn</firstname>

    <degree>ms</degree>

    <address>

    <city>false</city>

    <state>false</state>

    <zipcode>false</zipcode>

    </address>

    </Createstudent>

    </ROOT>'

    select

    T.c.value('id[1]', 'int') as id,

    T.c.value('classId[1]', 'int') as classid,

    T.c.value('lastname[1]', 'varchar(50)') as lastname,

    T.c.value('midname[1]', 'varchar(50)') as midname,

    T.c.value('firstname[1]', 'varchar(50)') as firstname,

    T.c.value('degree[1]', 'varchar(50)') as degree,

    T.c.value('address[1]/city[1]', 'varchar(50)') as city,

    T.c.value('address[1]/state[1]', 'varchar(50)') as state,

    T.c.value('address[1]/zipcode[1]', 'varchar(50)') as zipcode

    from

    @x.nodes('//Createstudent') T(c)

  • Thanks for the help. It worked for me.

  • Hi I am able to parse the xml file when it has less than 1000 records. But some of the files have more than 40000 rows. So when I try to parse them I am getting memory issue and not able to parse the file.

    Any ideas ?

    Thanks.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply