新书推介:《语义网技术体系》
作者:瞿裕忠,胡伟,程龚
   XML论坛     W3CHINA.ORG讨论区     计算机科学论坛     SOAChina论坛     Blog     开放翻译计划     新浪微博  
 
  • 首页
  • 登录
  • 注册
  • 软件下载
  • 资料下载
  • 核心成员
  • 帮助
  •   Add to Google

    >> 本版讨论DOM, SAX, XPath等。
    [返回] 中文XML论坛 - 专业的XML技术讨论区XML.ORG.CN讨论区 - XML技术『 DOM/SAX/XPath 』 → ◆◆如何将ResulteSet对象转换为XML Document对象◆◆[求助] 查看新帖用户列表

      发表一个新主题  发表一个新投票  回复主题  (订阅本版) 您是本帖的第 6451 个阅读者浏览上一篇主题  刷新本主题   树形显示贴子 浏览下一篇主题
     * 贴子主题: ◆◆如何将ResulteSet对象转换为XML Document对象◆◆[求助] 举报  打印  推荐  IE收藏夹 
       本主题类别:     
     zt99 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:28
      积分:222
      门派:XML.ORG.CN
      注册:2006/11/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zt99发送一个短消息 把zt99加入好友 查看zt99的个人资料 搜索zt99在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zt99的博客楼主
    发贴心情 ◆◆如何将ResulteSet对象转换为XML Document对象◆◆[求助]

    求助:如何将ResulteSet对象转换为XML Document对象???

    急急急!!!!!!!!!!


       收藏   分享  
    顶(0)
      




    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/7 9:09:00
     
     zt99 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:28
      积分:222
      门派:XML.ORG.CN
      注册:2006/11/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zt99发送一个短消息 把zt99加入好友 查看zt99的个人资料 搜索zt99在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zt99的博客2
    发贴心情 BZ指点一下!!谢谢了
    在一个类中,加入一个方法,实现
    将ResultSet对象生成XML Docement对象?

    作业,急急急!!!!!!!!!!

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/7 10:13:00
     
     zhu_ruixian 帅哥哟,离线,有人找我吗?射手座1983-12-2
      
      
      威望:2
      等级:大二期末(Java考了96分!)
      文章:406
      积分:3471
      门派:W3CHINA.ORG
      注册:2006/3/30

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zhu_ruixian发送一个短消息 把zhu_ruixian加入好友 查看zhu_ruixian的个人资料 搜索zhu_ruixian在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zhu_ruixian的博客3
    发贴心情 

    ----------------------------------------------
    为什么总是索取的人多,奉献的人少...

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/7 17:13:00
     
     zt99 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:28
      积分:222
      门派:XML.ORG.CN
      注册:2006/11/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zt99发送一个短消息 把zt99加入好友 查看zt99的个人资料 搜索zt99在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zt99的博客4
    发贴心情 
    谢谢bz
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/8 9:05:00
     
     zt99 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:28
      积分:222
      门派:XML.ORG.CN
      注册:2006/11/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zt99发送一个短消息 把zt99加入好友 查看zt99的个人资料 搜索zt99在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zt99的博客5
    发贴心情 
    BZ,看了你给的帖子,知道了如何把ResultSet转换为XML。
    但是转换为XML Document对象就不大明白了,
    我现在只能返回一个字符串(xml文本)??
    代码如下,还望再指点一下:
     public class PullValuesForXML{
     private String WriteForXML()throws Exception{   
       String driverName = "sun.jdbc.odbc.JdbcOdbcDriver";
          String dbUrl = "jdbc:odbc:Book";
          String user ="";
          String password ="",inputname; 
          Connection db = null;      
          Class.forName(driverName);
          db = DriverManager.getConnection(dbUrl,user,password);
              //Execute the query to populate the ResultSet     
         inputname="jsp";
          PreparedStatement  s=db.prepareStatement("select * from Book where bookname=? ");
          s.setString(1,inputname);
          ResultSet rs = s.executeQuery();        
          //Check for data by moving the cursor to the first record (if there is one)
          final StringBuffer buffer = new StringBuffer(1024);
              //String re;
          if ((rs == null))
             return "there's no record1";  
          if(!rs.next())
           return "there's no record2";
         buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
         buffer.append("<ResultSet>\n");
         rs = s.executeQuery();
         ResultSetMetaData rsmd = rs.getMetaData();  
         int colCount = rsmd.getColumnCount();        
         for (int id = 0; rs.next(); id++) {  
            //格式为row id , col name, col context
            buffer.append("\t<book>").append("\n");
            for (int i = 1; i <= colCount; i++) {         
             buffer.append(("\t\t<")+rsmd.getColumnName(i)+">");
             buffer.append(rs.getString(i));
             buffer.append("</"+rsmd.getColumnName(i)+">\n");
              }
             buffer.append("\t</book>\n");
           }
         buffer.append("</ResultSet>");          
         return buffer.toString();  
        }  
       public static void main (String args[])throws Exception{      
        String xmlstring =(new PullValuesForXML()).WriteForXML();
        System.out.println(xmlstring);
       }   
    }
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/9 9:55:00
     
     zt99 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:28
      积分:222
      门派:XML.ORG.CN
      注册:2006/11/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zt99发送一个短消息 把zt99加入好友 查看zt99的个人资料 搜索zt99在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zt99的博客6
    发贴心情 
    我已经有解了
    代码大家看一下,共同学习...
    public class PullValuesForXML{
     public Document  buildXML()throws Exception{
        String dbUrl = "jdbc:odbc:Book";
        String user ="";
        String password ="",inputname;            
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        Connection db = DriverManager.getConnection(dbUrl,user,password);
       inputname="haha";
        PreparedStatement  s=db.prepareStatement("select * from Book where bookname=? ");
        s.setString(1,inputname);
        ResultSet rs = s.executeQuery();        
        //Check for data by moving the cursor to the first record (if there is one)
        if ((rs == null)||(!rs.next()))
         {System.out.println("There's no record found!");
         return null;
         }
       else{   
       rs = s.executeQuery();
       Element root = new Element("book_info");  
       root.addContent(new Element("ResultSet"));
       Document doc = new Document(root);
       ResultSetMetaData rsmd = rs.getMetaData();  
       int colCount = rsmd.getColumnCount();
       for (int id = 0; rs.next(); id++) {
        root.addContent(0,new Element("ResultSet"));
          for (int i = 1; i <= colCount; i++) {         
          String cname=rsmd.getColumnName(i);
         String val= rs.getString(i);
         root.getChild("ResultSet").addContent(
        (Content) (new Element(cname).addContent(val)));
            }        
         }      
        Format format = Format.getCompactFormat();
        format.setEncoding("gb2312");
        format.setIndent("    ");
        //XMLOutputter XMLOut = new XMLOutputter(format);
        //XMLOut.output(doc, new FileOutputStream("haha.xml"));
        return doc;    
       }

       public static void main (String args[])throws Exception{   
          }
     }
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/9 14:08:00
     
     zhu_ruixian 帅哥哟,离线,有人找我吗?射手座1983-12-2
      
      
      威望:2
      等级:大二期末(Java考了96分!)
      文章:406
      积分:3471
      门派:W3CHINA.ORG
      注册:2006/3/30

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zhu_ruixian发送一个短消息 把zhu_ruixian加入好友 查看zhu_ruixian的个人资料 搜索zhu_ruixian在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zhu_ruixian的博客7
    发贴心情 
    //由String得到XML中Document对象的一种方法代码
    /**
      * 由String对象,创建一个结果向量Vector
      *
      * @return a Vector
      */

    public Vector getNameByString(String text){
      Vector result=new Vector();
      
      Document doc=parseXMLDocument(text);
      Element root = doc.getDocumentElement();
      
      NodeList address = root.getElementsByTagName("ADDRESS");
      for(int i = 0; i < address.getLength(); i++){
       Element record = (Element) address.item(i);
       
       String districtName=getStringByTag(record,"DISTRICTNAME");
       String belongName=getStringByTag(record,"BELONGNAME");
       String serviceName=getStringByTag(record,"SERVICE");
       String layerName=getStringByTag(record,"LAYER");
       String typeName=getStringByTag(record,"TYPE");
       String remarkContent=getStringByTag(record,"REMARK");
       
       PlaceNameRecord bean=new PlaceNameRecord(districtName,belongName,serviceName,layerName,typeName,remarkContent);
       result.addElement(bean);
      }
      
      return result;
    }

    /**
      * 由Element对象和相应的标签String,返回对应标签的内容
      *
      * @return a String
      */
    private String getStringByTag(Element record,String text){
      String result=null;
      
      NodeList node = record.getElementsByTagName(text);
      if (node.getLength() == 1) {
       Element e = (Element) node.item(0);
       Text t = (Text) e.getFirstChild();
       result=t.getNodeValue();
      }
      
      return result;
    }


    /**
      * 由String对象,初始化一个Document
      *
      * @return a Document
      */
    public static Document parseXMLDocument(String xmlString) {
      if (xmlString == null) {
       throw new IllegalArgumentException();
      }
      try {
       return newDocumentBuilder().parse(
         new InputSource(new StringReader(xmlString)));
      } catch (Exception e) {
       throw new RuntimeException(e.getMessage());
      }
    }

    /**
      * 初始化一个DocumentBuilder
      *
      * @return a DocumentBuilder
      * @throws ParserConfigurationException
      */
    public static DocumentBuilder newDocumentBuilder()
       throws ParserConfigurationException {
      return newDocumentBuilderFactory().newDocumentBuilder();
    }

    /**
      * 初始化一个DocumentBuilderFactory
      *
      * @return a DocumentBuilderFactory
      */
    public static DocumentBuilderFactory newDocumentBuilderFactory() {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      return dbf;
    }

    原文出处:http://blog.csdn.net/jaytse/archive/2005/12/29/565467.aspx

    ----------------------------------------------
    为什么总是索取的人多,奉献的人少...

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/9 16:27:00
     
     zhu_ruixian 帅哥哟,离线,有人找我吗?射手座1983-12-2
      
      
      威望:2
      等级:大二期末(Java考了96分!)
      文章:406
      积分:3471
      门派:W3CHINA.ORG
      注册:2006/3/30

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zhu_ruixian发送一个短消息 把zhu_ruixian加入好友 查看zhu_ruixian的个人资料 搜索zhu_ruixian在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zhu_ruixian的博客8
    发贴心情 
    //由String得到XML中Document对象的一种方法代码
    /**
      * 由String对象,创建一个结果向量Vector
      *
      * @return a Vector
      */

    public Vector getNameByString(String text){
      Vector result=new Vector();
      
      Document doc=parseXMLDocument(text);
      Element root = doc.getDocumentElement();
      
      NodeList address = root.getElementsByTagName("ADDRESS");
      for(int i = 0; i < address.getLength(); i++){
       Element record = (Element) address.item(i);
       
       String districtName=getStringByTag(record,"DISTRICTNAME");
       String belongName=getStringByTag(record,"BELONGNAME");
       String serviceName=getStringByTag(record,"SERVICE");
       String layerName=getStringByTag(record,"LAYER");
       String typeName=getStringByTag(record,"TYPE");
       String remarkContent=getStringByTag(record,"REMARK");
       
       PlaceNameRecord bean=new PlaceNameRecord(districtName,belongName,serviceName,layerName,typeName,remarkContent);
       result.addElement(bean);
      }
      
      return result;
    }

    /**
      * 由Element对象和相应的标签String,返回对应标签的内容
      *
      * @return a String
      */
    private String getStringByTag(Element record,String text){
      String result=null;
      
      NodeList node = record.getElementsByTagName(text);
      if (node.getLength() == 1) {
       Element e = (Element) node.item(0);
       Text t = (Text) e.getFirstChild();
       result=t.getNodeValue();
      }
      
      return result;
    }


    /**
      * 由String对象,初始化一个Document
      *
      * @return a Document
      */
    public static Document parseXMLDocument(String xmlString) {
      if (xmlString == null) {
       throw new IllegalArgumentException();
      }
      try {
       return newDocumentBuilder().parse(
         new InputSource(new StringReader(xmlString)));
      } catch (Exception e) {
       throw new RuntimeException(e.getMessage());
      }
    }

    /**
      * 初始化一个DocumentBuilder
      *
      * @return a DocumentBuilder
      * @throws ParserConfigurationException
      */
    public static DocumentBuilder newDocumentBuilder()
       throws ParserConfigurationException {
      return newDocumentBuilderFactory().newDocumentBuilder();
    }

    /**
      * 初始化一个DocumentBuilderFactory
      *
      * @return a DocumentBuilderFactory
      */
    public static DocumentBuilderFactory newDocumentBuilderFactory() {
      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
      dbf.setNamespaceAware(true);
      return dbf;
    }

    原文出处:http://blog.csdn.net/jaytse/archive/2005/12/29/565467.aspx

    还是感觉你上面的方法好一些。

    ----------------------------------------------
    为什么总是索取的人多,奉献的人少...

    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/9 16:28:00
     
     zt99 帅哥哟,离线,有人找我吗?
      
      
      等级:大二(研究C++)
      文章:28
      积分:222
      门派:XML.ORG.CN
      注册:2006/11/25

    姓名:(无权查看)
    城市:(无权查看)
    院校:(无权查看)
    给zt99发送一个短消息 把zt99加入好友 查看zt99的个人资料 搜索zt99在『 DOM/SAX/XPath 』的所有贴子 引用回复这个贴子 回复这个贴子 查看zt99的博客9
    发贴心情 
    呵呵,你的方法,我也要学一下.....
    还是要谢谢你。。。
    thank you  very  much!
    点击查看用户来源及管理<br>发贴IP:*.*.*.* 2006/12/11 8:38:00
     
     GoogleAdSense
      
      
      等级:大一新生
      文章:1
      积分:50
      门派:无门无派
      院校:未填写
      注册:2007-01-01
    给Google AdSense发送一个短消息 把Google AdSense加入好友 查看Google AdSense的个人资料 搜索Google AdSense在『 DOM/SAX/XPath 』的所有贴子 访问Google AdSense的主页 引用回复这个贴子 回复这个贴子 查看Google AdSense的博客广告
    2024/5/2 18:11:23

    本主题贴数9,分页: [1]

    管理选项修改tag | 锁定 | 解锁 | 提升 | 删除 | 移动 | 固顶 | 总固顶 | 奖励 | 惩罚 | 发布公告
    W3C Contributing Supporter! W 3 C h i n a ( since 2003 ) 旗 下 站 点
    苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
    109.375ms