游子的博客
慈母手中线,游子身上衣, 临行密密缝,意恐迟迟归, 谁言寸草心,报得三春晖。 数据读取中,请稍候......
posts - 337,  comments - 546,  trackbacks - 0


根据数据库生成xml二法  
 
方法一:
 
 
SqlConnection conn = new SqlConnection();
      conn.ConnectionString = "Server=127.0.0.1;User ID=sa;Password=fdahgdrethj31313210212121;Database=northwind;Persist Security Info=True";
      conn.Open();
      SqlDataAdapter da = new SqlDataAdapter("select * from 表", conn);
      SqlCommandBuilder thisBulder = new SqlCommandBuilder(da);
      DataSet ds = new DataSet();
      da.Fill(ds);
      ds.WriteXml(@"C:\temp.xml");

方法二:
private void WriteXmlToFile(DataSet thisDataSet) {
    if (thisDataSet == null) { return; }
    // Create a file name to write to.
    string filename = "myXmlDoc.xml";
    // Create the FileStream to write with.
    System.IO.FileStream myFileStream = new System.IO.FileStream
       (filename, System.IO.FileMode.Create);
    // Create an XmlTextWriter with the fileStream.
    System.Xml.XmlTextWriter myXmlWriter =
       new System.Xml.XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
    // Write to the file with the WriteXml method.
    thisDataSet.WriteXml(myXmlWriter);  
    myXmlWriter.Close();
 }
 
 

posted on 2006-08-27 17:41 游子 阅读(312) 评论(1)  编辑 收藏 引用 所属分类: 软件

FeedBack:
# 用XML作sql和web应用的桥
2006-08-27 22:16 | 游子


onjava网站上介绍了一篇来自dev2dev的文章,XML as a Bridge between SQL and Web Applications。介绍了一个Oracle提供的工具Oracle XML-SQL Utility (XSU). ,使用该工具能够让你的sql操作很简单的以XML的格式返回。同时文章还介绍了在web应用中,如何使用这个工具取得数据,以及如何使用xsl和jsp taglib来显示xml数据。可以直接使用xsl来显示数据,还介绍了如何使用weblogic提供的一个xsl处理的jsp标签库Using the JSP Tag to Transfer XML Data 在jsp中转换xml数据。例子代码如下:
程序用xsu工具取得xml格式的数据后,直接使用xsl标签将数据转换成html。

1. <%@ taglib uri="xmlx.tld" prefix="x"%>

2. <HTML>
3. <HEAD>
4. <TITLE>:: An XML bridge ::</TITLE>
5. </HEAD>
6. <BODY>

7. <%@page import="java.sql.*"%>
8. <%@page import="oracle.xml.sql.query.*"%>
9. <%@page import="oracle.jdbc.*"%>

10. <%

11. String tableName = "emp";
12. int maxRows = 3;

13. Context ctx = new InitialContext ();
14. DataSource ds = (DataSource) ctx.lookup ("MyOra");

15. Connection conn = ds.getConnection ();

16. OracleXMLQuery qu = new OracleXMLQuery
(conn, "select EMPNO, ENAME from " + tableName);

17. qu.setMaxRows (maxRows);
18. qu.setRowsetTag ("EMPLOYERS");
19. qu.setRowTag ("PERSON");

20. String xmlString = qu.getXMLString ();

21. conn.close ();
22. %>

23. <x:xslt stylesheet="html.xsl">
24. <x:xml>
25. <%=xmlString%>
26. </x:xml>
27. </x:xslt>

28. </BODY>
29. </HTML>



  回复  更多评论
  
只有注册用户登录后才能发表评论。

欢迎大家扔鸡蛋!送鲜花!

博客可以收入过千吗?

<2006年8月>
日一二三四五六3031
12345678910111213141516171819202122232425262728293031123456789

常用链接

留言簿(8)

随笔分类(314)

随笔档案(337)

文章分类(7)

文章档案(10)

相册

收藏夹(1)

其它

友情链接

数字电视

生活、旅游

自己的链接

计算机

搜索

  •  

积分与排名

  • 积分 - 403678
  • 排名 - 9

最新评论

阅读排行榜

评论排行榜