sqlserver FOR XML PATH Óï¾äµÄÓ¦ÓÃ
´úÂëÈçÏÂ:
DECLARE
@TempTable table(UserID int , UserName nvarchar(50));
insert into
@TempTable (UserID,UserName) values (1,'a')
insert into @TempTable
(UserID,UserName) values (2,'b')
select UserID,UserName
from @TempTable FOR XML PATH
ÔËÐÐÕâ¶Î½Å±¾£¬½«Éú³ÉÈçϽá¹û£º
¸´ÖÆ´úÂë
´úÂëÈçÏÂ:
<row>
<UserID>1</UserID>
<UserName>a</UserName>
</row>
<row>
<UserID>2</UserID>
<UserName>b</UserName>
</row>
´ó¼Ò¿ÉÒÔ¿´µ½Á½ÐÐÊý¾ÝÉú³ÉÁËÁ½¸ö½Úµã£¬ÐÞ¸ÄÒ»ÏÂPATHµÄ²ÎÊý£º
select
UserID,UserName from @TempTable FOR XML PATH('lzy')
ÔÙ´ÎÔËÐÐÉÏÊö½Å±¾£¬½«Éú³ÉÈçϵÄ
½á¹û£º
¸´ÖÆ´úÂë
´úÂëÈçÏÂ:
<lzy>
<UserID>1</UserID>
<UserName>a</UserName>
</lzy>
<lzy>
<UserID>2</UserID>
<UserName>b</UserName>
</lzy>
¿ÉÒÔ¿´µ½½Úµã±ä³É£¬ÆäʵPATH()
À¨ºÅÄڵIJÎÊýÊÇ¿ØÖƽڵãÃû³ÆµÄ£¬ÕâÑùµÄ»°´ó¼Ò¿ÉÒÔ¿´Ò»ÏÂÈç¹ûÊÇ¿Õ×Ö·û´®£¨²»ÊÇûÓвÎÊý£©»áÊÇʲô½á¹û?
select
UserID,UserName from @TempTable FOR XML PATH('')
Ö´ÐÐÉÏÃæÕâ¶Î½Å±¾½«Éú³É½á¹û£º
¸´ÖÆ´úÂë
´úÂëÈçÏÂ:
<UserID>1</UserID>
<UserName>a</UserName>
<UserID>2</UserID>
<UserName>b</UserName>
ÕâÑù¾Í²»ÏÔʾÉϼ¶½ÚµãÁË£¬´ó¼ÒÖªµÀÔÚ PATH ģʽÖУ¬ÁÐÃû»òÁбðÃû±»×÷Ϊ XPath
±í´ïʽÀ´´¦Àí£¬Ò²¾ÍÊÇ˵£¬ÊÇÁеÄÃû×Ö£¬ÕâÑù´óµ¨ÊÔÑéһϲ»¸øÖ¸¶¨ÁÐÃûºÍ±ðÃû»áÊÇÔõôÑù£¿
select CAST(UserID AS
varchar) + '',UserName + '' from @TempTable FOR XML PATH('')
ÔËÐÐÉÏÃæÕâ¾ä½«
Éú³É½á¹û
1a2b
ËùÓÐÊý¾Ý¶¼Éú³ÉÒ»ÐУ¬¶øÇÒ»¹Ã»ÓÐÁ¬½Ó×Ö·û£¬ÕâÑùµÄÊý¾Ý¿ÉÄܶԴó¼ÒûÓÐÓô¦£¬»¹¿ÉÒÔÔٱ仯һÏ£º
select
CAST(UserID AS varchar) + ',',UserName + '',';' from @TempTable FOR XML
PATH('')
Éú³É½á¹û
1,a;2,b;
´ó¼ÒÏÖÔÚÃ÷°×ÁË°É£¬¿ÉÒÔͨ¹ý¿ØÖƲÎÊýÀ´Éú³É×Ô¼ºÏëÒªµÄ½á¹û£¬
ÀýÈ磺
select '{' + CAST(UserID AS varchar) + ',','"' +UserName +
'"','}' from @TempTable FOR XML PATH('')
Éú³É½á¹û
{1,"a&q
Ïà¹ØÎĵµ£º
×÷ÕߣºÖì½ð²Ó
À´Ô´£ºhttp://blog.csdn.net/clever101/
ÏÖÔÚÎÒ·¢ÏÖʹÓÃVisual StudioµÄ×ÊÔ´±à¼Æ÷½øÐбà¼×ÊÔ´ÓÐ×ÅÖî¶àµÄ²»±ã£ºÊ×ÏÈÊÇÈκÎ×ÊÔ´µÄ±ä¶¯Ò»°ã±ä¶¯´úÂ룬²»ÀûÓÚϵͳά»¤£¬Æä´ÎVisual StudioµÄ×ÊÔ´±à¼Æ÷µÄ±¾ÉíµÄ¹¦ÄÜÓÐÏÞ£¬Ò²²»ÀûÓÚ½çÃæÃÀ»¯£¬ÈýÊDz»ÀûÓÚÈËÔ±·Ö¹¤£¬¿ª·¢ÈËÔ±¼ÈҪæ ......
×î½ü¿´µ½ÁËÒ»¸öÏîÄ¿½âÎöxmlµÄ·½Ê½£¬¾õµÃÒ²²»´í£¬²âÊÔϸú´ó¼Ò·ÖÏíÏ£¡ÑÛϽâÎöxmlµÄ·½Ê½ºÜ¶à£¬ÕâÀï²»ÌáÁË£¡Ö»Ì¸Ì¸digesterµÄʹÓã¬Ç³Ì¸°É£¡Ï£ÍûÒÔºóÄãÃÇÓöµ½ÁËÄÜÓеã°ïÖú°É£¡
Ê×ÏÈÔÚʹÓÃDigester½âÎöxmlʱҪµ¼Èë2¸öjar°ü£¬ÓеÄÈË˵ÊÇ4¸ö£ ......
XML DOM½éÉÜ
Ò»¡¢XML DOM¼ò½é
DOM£¨Document Object Model£¬Îĵµ¶ÔÏóÄ£ÐÍ£©ÊÇÒ»ÖÖÓ¦ÓóÌÐò½Ó¿Ú£¨API£©µÄÓ¦Óã¬Ëü½«Îĵµ£¨ÈçXMLÎĵµ£¬HTMLÎĵµµÈ£©¿´³ÉÊÇÒ»¸öÎĵµ¶ÔÏó£¬È»ºóͨ¹ý³ÌÐòÓïÑÔ£¨ÈçJavaScriptµÈ½Å±¾ÓïÑÔ£¬C++µÈ£©µ÷ÓøÃÎĵµ¶ÔÏ󣬶ÔÎĵµÖеÄÊý¾Ý½øÐдæÈ¡£¬²¢ÀûÓóÌÐò¶Ô»ñÈ¡µÄÊý¾Ý½øÐиú½øÒ»²½µÄ´¦Àí¡£
X ......
XML ÃüÃû¿Õ¼ä¿ÉÌṩ±ÜÃâÔªËØÃüÃû³åÍ»µÄ·½·¨¡£
ÃüÃû³åÍ»
ÓÉÓÚ XML ÖеÄÔªËØÃûÊÇÔ¤¶¨ÒåµÄ£¬µ±Á½¸ö²»Í¬µÄÎĵµÊ¹ÓÃÏàͬµÄÔªËØÃûʱ£¬¾Í»á·¢ÉúÃüÃû³åÍ»¡£
Õâ¸ö XML ÎĵµÐ¯´ø×Åij¸ö±í¸ñÖеÄÐÅÏ¢£º
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</ta ......
Ò»¡¢Îĵµ¶ÔÏóÄ£ÐÍ£¨DOM£©
¡¡¡¡DOMÊÇDocument Object Model£¨Îĵµ¶ÔÏóÄ£ÐÍ£©µÄ¼ò³Æ£¬ÊǶÔXMLÎĵµ½øÐÐÓ¦Óÿª·¢¡¢±à³ÌµÄÓ¦ÓóÌÐò½Ó¿Ú£¨API£©¡£×÷ΪW3C¹«²¼µÄÒ»ÖÖ¿çƽ̨¡¢ÓëÓïÑÔÎ޹صĽӿڹ淶£¬DOMÌṩÁËÔÚ²»Í¬»·¾³ºÍÓ¦ÓÃÖеıê×¼³ÌÐò½Ó¿Ú£¬¿ÉÒÔÓÃÈκÎÓïÑÔʵÏÖ¡£
¡¡¡¡DOM²ÉÓöÔÏóÄ£ÐͺÍһϵÁеĽӿÚÀ´ÃèÊöXMLÎĵµµÄÄÚÈݺͽ ......