用XML显示相关数据库

在SQL Server将表格类型数据转换成XML格式的数据,只需要在常规SQL语句中附加FOR XML<mode>子句。

1. FOR XML RAW :可以获得一个以属性为中心的XML数据视图,每一行包裹在一个<row>元素中。但没有加入document元素,因此它仅是一个XML片段。

例如:

 SELECT [PurchaseOrderID],[RevisionNumber],[Status],[EmployeeID],[VendorID],[ShipMethodID],[OrderDate],[ShipDate],[SubTotal],[TaxAmt],[Freight],[TotalDue],[ModifiedDate]
  FROM [Purchasing].[PurchaseOrderHeader]
  WHERE [TotalDue] > 300000
  FOR XML RAW;
查询返回的结果集为:
<row PurchaseOrderID="4007" RevisionNumber="10" Status="2" EmployeeID="164" VendorID="102" ShipMethodID="3" OrderDate="2004-04-01T00:00:00" ShipDate="2004-04-26T00:00:00" SubTotal="554020.0000" TaxAmt="44321.6000" Freight="11080.4000" TotalDue="609422.0000" ModifiedDate="2005-09-12T12:25:46.407" />
<row PurchaseOrderID="4008" RevisionNumber="16" Status="2" EmployeeID="244" VendorID="95" ShipMethodID="3" OrderDate="2004-05-23T00:00:00" ShipDate="2004-06-17T00:00:00" SubTotal="396729.0000" TaxAmt="31738.3200" Freight="7934.5800" TotalDue="436401.9000" ModifiedDate="2005-09-12T12:25:46.420" />
<row PurchaseOrderID="4012" RevisionNumber="5" Status="2" EmployeeID="231" VendorID="29" ShipMethodID="3" OrderDate="2004-07-25T00:00:00" ShipDate="2004-08-19T00:00:00" SubTotal="997680.0000" TaxAmt="79814.4000" Freight="19953.6000" TotalDue="1097448.0000" ModifiedDate="2005-09-12T12:25:46.483" />

FOR XML RAW,ELEMENTS : 返回是以元素为中心的视图。

例如:

 SELECT [PurchaseOrderID],[ModifiedDate]
  FROM [Purchasing].[PurchaseOrderHeader]
  WHERE [TotalDue] > 300000
  FOR XML RAW,ELEMENTS;
查询返回的结果集为:
<row>
  <PurchaseOrderID>4007</PurchaseOrderID>
  <RevisionNumber>10</RevisionNumber>
  <Status>2</Status>
  <EmployeeID>164</EmployeeID>
  <VendorID>102</VendorID>
  <ShipMethodID>3</ShipMethodID>
  <OrderDate>2004-04-01T00:00:00</OrderDate>
  <ShipDate>2004-04-26T00:00:00</ShipDate>
  <SubTotal>554020.0000</SubTotal>
  <TaxAmt>44321.6000</TaxAmt>
  <Freight>11080.4000</Freight>
  <TotalDue>609422.0000</TotalDue>
  <ModifiedDate>2005-09-12T12:25:46.407</ModifiedDate>
</row>

FOR XML RAW,ELEMENTS,ROOT('orders') : 为文档添加一个根元素,使用ROOT指明根元素名称。

例如:

SELECT [PurchaseOrderID],ROOT('orders');

查询返回的结果集为:

<orders> <row> <PurchaseOrderID>4007</PurchaseOrderID> <RevisionNumber>10</RevisionNumber> <Status>2</Status> <EmployeeID>164</EmployeeID> <VendorID>102</VendorID> <ShipMethodID>3</ShipMethodID> <OrderDate>2004-04-01T00:00:00</OrderDate> <ShipDate>2004-04-26T00:00:00</ShipDate> <SubTotal>554020.0000</SubTotal> <TaxAmt>44321.6000</TaxAmt> <Freight>11080.4000</Freight> <TotalDue>609422.0000</TotalDue> <ModifiedDate>2005-09-12T12:25:46.407</ModifiedDate> </row> <!-- more row elements --> </orders> 可以修改元素容器的名称,默认的行元素名为<row>。只需要在RAW关键字后用括号加上希望的行元素名就可以了。例如: SELECT [PurchaseOrderID],[ModifiedDate] FROM [Purchasing].[PurchaseOrderHeader] WHERE [TotalDue] > 300000 FOR XML RAW('order'),ROOT('orders'); 查询返回的结果集为: <orders> <order> <PurchaseOrderID>4007</PurchaseOrderID> <RevisionNumber>10</RevisionNumber> <Status>2</Status> <EmployeeID>164</EmployeeID> <VendorID>102</VendorID> <ShipMethodID>3</ShipMethodID> <OrderDate>2004-04-01T00:00:00</OrderDate> <ShipDate>2004-04-26T00:00:00</ShipDate> <SubTotal>554020.0000</SubTotal> <TaxAmt>44321.6000</TaxAmt> <Freight>11080.4000</Freight> <TotalDue>609422.0000</TotalDue> <ModifiedDate>2005-09-12T12:25:46.407</ModifiedDate> </order> <!-- more order elements --> </orders> 默认情况下查询语句不输出值为null的元素和属性。为了区分包含null值的元素和包含空字符串的元素,包含null值得元素内需要有一个标签来表示它的值是null而不是一个空字符串。通常在ELEMENT关键字后使用XSINIL可选项,使得null值显示标记为xsi:nil="true"。这个标记来源于模式实例名称空间中的一个标准属性,所以SQL Server也需要加入正确的名称空间绑定。 SELECT [PurchaseOrderID],ELEMENTS XSINIL,ROOT('orders'); 查询返回的结果集为: <orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <order> <PurchaseOrderID>4007</PurchaseOrderID> <RevisionNumber>10</RevisionNumber> <Status>2</Status> <EmployeeID>164</EmployeeID> <VendorID>102</VendorID> <ShipMethodID>3</ShipMethodID> <OrderDate>2004-04-01T00:00:00</OrderDate> <ShipDate xsi:nil="true"> <SubTotal>554020.0000</SubTotal> <TaxAmt>44321.6000</TaxAmt> <Freight>11080.4000</Freight> <TotalDue>609422.0000</TotalDue> <ModifiedDate>2005-09-12T12:25:46.407</ModifiedDate> </order> <!-- more order element--> </orders> 查询添加XML模式:可以在代码后面添加XMLSCHEMA选型实现。 SELECT [PurchaseOrderID],ROOT('orders'),XMLSCHEMA; 查询返回的结果集为: <orders xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <xsd:schema targetNamespace="urn:schemas-microsoft-com:sql:SqlRowSet1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:sqltypes="http://schemas.microsoft.com/sqlserver/2004/sqltypes" elementFormDefault="qualified"> <xsd:import namespace="http://schemas.microsoft.com/sqlserver/2004/sqltypes" schemaLocation="http://schemas.microsoft.com/sqlserver/2004/sqltypes/sqltypes.xsd" /> <xsd:element name="order"> <xsd:complexType> <xsd:sequence> <xsd:element name="PurchaseOrderID" type="sqltypes:int" nillable="1" /> <xsd:element name="RevisionNumber" type="sqltypes:tinyint" nillable="1" /> <xsd:element name="Status" type="sqltypes:tinyint" nillable="1" /> <xsd:element name="EmployeeID" type="sqltypes:int" nillable="1" /> <xsd:element name="VendorID" type="sqltypes:int" nillable="1" /> <xsd:element name="ShipMethodID" type="sqltypes:int" nillable="1" /> <xsd:element name="OrderDate" type="sqltypes:datetime" nillable="1" /> <xsd:element name="ShipDate" type="sqltypes:datetime" nillable="1" /> <xsd:element name="SubTotal" type="sqltypes:money" nillable="1" /> <xsd:element name="TaxAmt" type="sqltypes:money" nillable="1" /> <xsd:element name="Freight" type="sqltypes:money" nillable="1" /> <xsd:element name="TotalDue" type="sqltypes:money" nillable="1" /> <xsd:element name="ModifiedDate" type="sqltypes:datetime" nillable="1" /> </xsd:sequence> </xsd:complexType> </xsd:element> </xsd:schema> <order xmlns="urn:schemas-microsoft-com:sql:SqlRowSet1"> <PurchaseOrderID>4007</PurchaseOrderID> <RevisionNumber>10</RevisionNumber> <Status>2</Status> <EmployeeID>164</EmployeeID> <VendorID>102</VendorID> <ShipMethodID>3</ShipMethodID> <OrderDate>2004-04-01T00:00:00</OrderDate> <ShipDate>2004-04-26T00:00:00</ShipDate> <SubTotal>554020.0000</SubTotal> <TaxAmt>44321.6000</TaxAmt> <Freight>11080.4000</Freight> <TotalDue>609422.0000</TotalDue> <ModifiedDate>2005-09-12T12:25:46.407</ModifiedDate> </order> <!-- more oreder element--> </orders> 2. FOR XML AUTO : 处理嵌套数据。查询出的元素名来自于查询语句中的数据表的别名,这位重名元素提供了方法。 例如: SELECT POH.[PurchaseOrderID],POH.[RevisionNumber],POH.[Status],POH.[EmployeeID],POH.[VendorID],POH.[ShipMethodID],POH.[OrderDate],POH.[ShipDate],POH.[SubTotal],POH.[TaxAmt],POH.[Freight],POH.[TotalDue],POH.[ModifiedDate],POD.[OrderQty],POD.[ProductID],POD.[UnitPrice] FROM [Purchasing].[PurchaseOrderHeader] POH INNER JOIN Purchasing.PurchaseOrderDetail POD ON POH.PurchaseOrderID = POD.PurchaseOrderID WHERE [TotalDue] > 300000 FOR XML AUTO,ROOT('orders'); 查询返回的结果集为: <orders> <POH PurchaseOrderID="4007" RevisionNumber="10" Status="2" EmployeeID="164" VendorID="102" ShipMethodID="3" OrderDate="2004-04-01T00:00:00" ShipDate="2004-04-26T00:00:00" SubTotal="554020.0000" TaxAmt="44321.6000" Freight="11080.4000" TotalDue="609422.0000" ModifiedDate="2005-09-12T12:25:46.407"> <POD OrderQty="5000" ProductID="849" UnitPrice="24.7500" /> <POD OrderQty="5000" ProductID="850" UnitPrice="24.7500" /> <POD OrderQty="5000" ProductID="851" UnitPrice="24.7500" /> <POD OrderQty="750" ProductID="852" UnitPrice="30.9400" /> <POD OrderQty="750" ProductID="853" UnitPrice="30.9400" /> <POD OrderQty="750" ProductID="854" UnitPrice="30.9400" /> <POD OrderQty="1050" ProductID="855" UnitPrice="37.1000" /> <POD OrderQty="1000" ProductID="856" UnitPrice="37.1000" /> <POD OrderQty="1000" ProductID="857" UnitPrice="37.1000" /> </POH> <POH PurchaseOrderID="4008" RevisionNumber="16" Status="2" EmployeeID="244" VendorID="95" ShipMethodID="3" OrderDate="2004-05-23T00:00:00" ShipDate="2004-06-17T00:00:00" SubTotal="396729.0000" TaxAmt="31738.3200" Freight="7934.5800" TotalDue="436401.9000" ModifiedDate="2005-09-12T12:25:46.420"> <POD OrderQty="700" ProductID="858" UnitPrice="9.1500" /> <POD OrderQty="700" ProductID="859" UnitPrice="9.1500" /> <POD OrderQty="700" ProductID="860" UnitPrice="9.1500" /> <POD OrderQty="900" ProductID="861" UnitPrice="15.6700" /> <POD OrderQty="900" ProductID="862" UnitPrice="15.6700" /> <POD OrderQty="900" ProductID="863" UnitPrice="15.6700" /> <POD OrderQty="1250" ProductID="864" UnitPrice="23.7500" /> <POD OrderQty="1250" ProductID="865" UnitPrice="23.7500" /> <POD OrderQty="1250" ProductID="866" UnitPrice="23.7500" /> <POD OrderQty="1250" ProductID="865" UnitPrice="23.7500" /> <POD OrderQty="1250" ProductID="866" UnitPrice="23.7500" /> <POD OrderQty="2000" ProductID="867" UnitPrice="26.1800" /> <POD OrderQty="2000" ProductID="868" UnitPrice="26.1800" /> <POD OrderQty="1250" ProductID="865" UnitPrice="23.7500" /> <POD OrderQty="2000" ProductID="869" UnitPrice="26.1800" /> </POH> <POH PurchaseOrderID="4012" RevisionNumber="5" Status="2" EmployeeID="231" VendorID="29" ShipMethodID="3" OrderDate="2004-07-25T00:00:00" ShipDate="2004-08-19T00:00:00" SubTotal="997680.0000" TaxAmt="79814.4000" Freight="19953.6000" TotalDue="1097448.0000" ModifiedDate="2005-09-12T12:25:46.483"> <POD OrderQty="6000" ProductID="881" UnitPrice="41.5700" /> <POD OrderQty="6000" ProductID="882" UnitPrice="41.5700" /> <POD OrderQty="6000" ProductID="883" UnitPrice="41.5700" /> <POD OrderQty="6000" ProductID="884" UnitPrice="41.5700" /> </POH> </orders> 3. FOR XML EXPLICIT : 几乎可以不受限制的控制XML的生成格式。但用法比较复杂,很难掌握,而且构造结果XML的机制依赖于只前(forward-only)XML写入器,因此结果必须按某种特定的方式分组和排序。一般建议使用PATH可选项取而代之。 4. FOR XML PATH :PATH使用XPath来指定输出格式,通过它可以相对简单的方法建立内嵌元素和属性的XML文档。 需要返回作为属性的数据,其相应的列明采用别名方式,前面加一个@,不用别名的列作为元素返回。例如: SELECT [PurchaseOrderID] [@PurchaseOrderID],[Status] [@Status],[EmployeeID] [@EmployeeID],[TotalDue] FROM [Purchasing].[PurchaseOrderHeader] POH WHERE [TotalDue] > 300000 FOR XML PATH('order'),ROOT('orders'); 查询返回的结果集为: <orders> <order PurchaseOrderID="4007" Status="2" EmployeeID="164"> <VendorID>102</VendorID> <ShipMethodID>3</ShipMethodID> <OrderDate>2004-04-01T00:00:00</OrderDate> <ShipDate>2004-04-26T00:00:00</ShipDate> <SubTotal>554020.0000</SubTotal> <TaxAmt>44321.6000</TaxAmt> <Freight>11080.4000</Freight> <TotalDue>609422.0000</TotalDue> </order> <!--more order element--> </orders> SELECT [POH].[PurchaseOrderID] [@PurchaseOrderID],[POH].[Status] [@Status],[POH].[EmployeeID] [@EmployeeID],[POH].[VendorID] [@VendorID],[POH].[ShipMethodID] [@ShipMethodID],[POH].[SubTotal] [@SubTotal],[POH].[TaxAmt] [@TaxAmt],[POH].[Freight] [@Freight],[POH].[TotalDue] [@TotalDue],[POH].[OrderDate] [Dates/Order],[POH].[ShipDate] [Dates/Ship],( SELECT [POD].[OrderQty],[POD].[ProductID],[POD].[UnitPrice] FROM [Purchasing].[PurchaseOrderDetail] POD WHERE POH.[PurchaseOrderID] = POD.[PurchaseOrderID] ORDER BY POD.[PurchaseOrderID] FOR XML PATH('orderDetail'),TYPE ) FROM [Purchasing].[PurchaseOrderHeader] POH WHERE [POH].[TotalDue] > 300000 FOR XML PATH('order'),ROOT('orders'); 查询返回的结果集为: <orders> <order PurchaseOrderID="4007" Status="2" EmployeeID="164" VendorID="102" ShipMethodID="3" SubTotal="554020.0000" TaxAmt="44321.6000" Freight="11080.4000" TotalDue="609422.0000"> <Dates> <Order>2004-04-01T00:00:00</Order> <Ship>2004-04-26T00:00:00</Ship> </Dates> <orderDetail> <OrderQty>5000</OrderQty> <ProductID>849</ProductID> <UnitPrice>24.7500</UnitPrice> </orderDetail> <orderDetail> <OrderQty>5000</OrderQty> <ProductID>850</ProductID> <UnitPrice>24.7500</UnitPrice> </orderDetail> <orderDetail> <OrderQty>5000</OrderQty> <ProductID>851</ProductID> <UnitPrice>24.7500</UnitPrice> </orderDetail> <orderDetail> <OrderQty>750</OrderQty> <ProductID>852</ProductID> <UnitPrice>30.9400</UnitPrice> </orderDetail> <orderDetail> <OrderQty>750</OrderQty> <ProductID>853</ProductID> <UnitPrice>30.9400</UnitPrice> </orderDetail> <orderDetail> <OrderQty>750</OrderQty> <ProductID>854</ProductID> <UnitPrice>30.9400</UnitPrice> </orderDetail> <orderDetail> <OrderQty>1050</OrderQty> <ProductID>855</ProductID> <UnitPrice>37.1000</UnitPrice> </orderDetail> <orderDetail> <OrderQty>1000</OrderQty> <ProductID>856</ProductID> <UnitPrice>37.1000</UnitPrice> </orderDetail> <orderDetail> <OrderQty>1000</OrderQty> <ProductID>857</ProductID> <UnitPrice>37.1000</UnitPrice> </orderDetail> </order> <!--more order elements--> </orders>

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇