简体   繁体   English

将Oracle数据库中的表转换为XML文件

[英]Converting a Table in Oracle Database into an XML file

I need to create XML file, given a table/view in the Database. 给定数据库中的表/视图,我需要创建XML文件。 The actual requirement would be: The code has to get the table/view name as input parameter, read the schema of the same, create a corresponding XML file with the same structure as that of the table/view, and load the XML with the data of the table/view. 实际的要求是:代码必须获取表/视图的名称作为输入参数,读取相同的模式,创建与表/视图具有相同结构的对应XML文件,并使用表/视图的数据。 Language preferred here is JSP. 这里首选的语言是JSP。 Kindly let me know how to go about this idea. 请让我知道如何实现这个想法。

Thanks in advance, Geetha 在此先感谢Geetha

This sort of does what you want, except it isn't specific to Oracle. 此类操作可以满足您的要求,只是它不是特定于Oracle的。 It produces an XML string, not an XML document. 它产生一个XML字符串,而不是XML文档。

Excerpt: 摘抄:

private String ExecQueryGetXml(java.sql.PreparedStatement stmt, String rootEltName, String rowEltName) {

  String result= "<none/>";
  String item;
  java.sql.ResultSet resultSet;
  java.sql.ResultSetMetaData metaData ;
  StringBuffer buf = new StringBuffer();
  int i;

  try {
    resultSet = stmt.executeQuery();
    metaData= resultSet.getMetaData();

    int numberOfColumns =  metaData.getColumnCount();
    String[] columnNames = new String[numberOfColumns];
    for( i = 0; i < numberOfColumns; i++) 
      columnNames[i] = metaData.getColumnLabel(i+1);

    try {
      if ((rootEltName!=null) && (!rootEltName.equals("")))
          buf.append('<').append(rootEltName).append('>').append('\n');

      // each row is an element, each field a sub-element
      while ( resultSet.next() ) {
        // open the row elt
        buf.append(' ').append('<').append(rowEltName).append(">\n");
        for( i= 0; i < numberOfColumns; i++) {
          item = resultSet.getString(i+1); 
          if(item==null)   continue;
          buf.append("  <").append(columnNames[i]).append('>');
          // check for CDATA required here? 
          buf.append(item);
          buf.append("</").append(columnNames[i]).append(">\n");
        }
        buf.append("\n </").append(rowEltName).append(">\n");
      }
      if ((rootEltName!=null) && (!rootEltName.equals("")))
          buf.append("</").append(rootEltName).append(">\n");
      result= buf.toString();
    } 
    catch(Exception e1) {
      System.err.print("\n\n----Exception (2): failed converting ResultSet to xml.\n");
      System.err.print(e1);
      result= "<error><message>Exception (2): " + e1.toString() + ". Failed converting ResultSet to xml.</message></error>\n";
    }
  }
  catch(Exception e2) {
    System.err.print("\n\n----Exception (3).\n");
    System.err.print("\n\n----query failed, or getMetaData failed.\n");
    System.err.print("\n\n---- Exc as string: \n" + e2);
    System.err.print("\n\n---- Exc via helper: \n" + 
                     ExceptionHelper.getStackTraceAsString(e2));

    result= "<error><message>Exception (3): " + e2 + ". query failed, or getMetaData() failed.</message></error>\n";
  }

  return result;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM