用XML显示相关数据库

前端之家收集整理的这篇文章主要介绍了用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>

猜你在找的XML相关文章