以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 DOM/SAX/XPath 』  (http://bbs.xml.org.cn/list.asp?boardid=11)
----  ◆◆如何将ResulteSet对象转换为XML Document对象◆◆[求助]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=11&rootid=&id=40942)


--  作者:zt99
--  发布时间:12/7/2006 9:09:00 AM

--  ◆◆如何将ResulteSet对象转换为XML Document对象◆◆[求助]
求助:如何将ResulteSet对象转换为XML Document对象???

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


--  作者:zt99
--  发布时间:12/7/2006 10:13:00 AM

--  BZ指点一下!!谢谢了
在一个类中,加入一个方法,实现
将ResultSet对象生成XML Docement对象?

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


--  作者:zhu_ruixian
--  发布时间:12/7/2006 5:13:00 PM

--  
http://www.xml.org.cn/dispbbs.asp?boardID=17&ID=16291
看看这个贴子!
--  作者:zt99
--  发布时间:12/8/2006 9:05:00 AM

--  
谢谢bz

--  作者:zt99
--  发布时间:12/9/2006 9:55:00 AM

--  
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);
   }   
}
--  作者:zt99
--  发布时间:12/9/2006 2:08:00 PM

--  
我已经有解了
代码大家看一下,共同学习...
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{   
      }
 }


--  作者:zhu_ruixian
--  发布时间:12/9/2006 4:27:00 PM

--  
//由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


--  作者:zhu_ruixian
--  发布时间:12/9/2006 4:28:00 PM

--  
//由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

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


--  作者:zt99
--  发布时间:12/11/2006 8:38:00 AM

--  
呵呵,你的方法,我也要学一下.....
还是要谢谢你。。。
thank you  very  much!

W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
113.281ms