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 ÖеÄÔªËØÃûÊÇÔ¤¶¨ÒåµÄ£¬µ±Á½¸ö²»Í¬µÄÎĵµÊ¹ÓÃÏàͬµÄÔªËØÃûʱ£¬¾Í»á·¢ÉúÃüÃû³åÍ»¡£
Õâ¸ö XML ÎĵµÐ¯´ø×Åij¸ö±í¸ñÖеÄÐÅÏ¢£º
<table>
<tr>
<td>Apples</td>
<td>Bananas</td>
</tr>
</ta ......
page.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="page.xsl" ?>
<list>
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
& ......
ÔںܶàÑÐÌÖ»áºÍÅàѵ°àÉÏÎÒÓöµ½¹ýÐí¶àÈË£¬ËûÃÇ»¹²»Ã÷°×ΪʲôҪʹÓÃXMLÒ²²»ÖªµÀÈçºÎÔÚËûÃǵÄÓ¦ÓÃÖÐʹÓÃXML¡£Ò»Ð©À´×ÔÖîÈçGartner¹«Ë¾µÄ±¨¸æ½¨Òé˵£¬ÉÌÒµ¹«Ë¾²»ÄÜÔÙ×ö¾ÖÍâÈËÁË£¬²»ÄܶÔXMLÖÃÖ®²»Àí¡£Èç¹ûÄ㻹²»Çå³þXMLµ½µ×ÓÐʲôºÃ´¦µÄ»°£¬Äã²¢²»ÊÇΨһµÄÈË¡£
ÎÒ¾ö¶¨°ÑÓëÈËÃǺÍýÌå¹ØÓÚXML»°ÌâµÄ½»Ì¸ÕûÀí³ÉÎÄ£¬ÁгöXMLÔÚÓ¦ÓÃÖ ......
JAVATMÓëXML 2010-05-14
Ò»¡¢¸ÅÄî
l ÈκÎXMLÎĵµ¶ÔÈκÎÀàÐ͵ÄÓ¦ÓÃÒÔ¼°ÕýÈ·µÄ½âÎö¶¼±ØÐë¾ßÓÐÁ¼ºÃµÄ½á¹¹well-formed
l XMLÎĵµ¿ÉÒÔÊÇÓÐЧµØ£¨valid£©£¬µ«²¢·ÇÒ»¶¨ÒªÇóÓÐЧ¡£
l PI£¨processing instruction£©¸ñʽ£º<?target instructions?>¡£XMLÖеĴ¦ÀíÖ¸ÁîÖ»ÄÜÊǽâÎöÆ÷¿ÉÒÔʶ±ðµÄXML±ê×¼´¦ÀíÃüÁ ......