-- 作者:sophieflying
-- 发布时间:4/19/2004 12:58:00 PM
-- 在jbuilder环境下把xml文档导入导出oracle
毕业设计的题目是在在jbuilder环境下把xml文档导入导出oracle。导出可用jbuilder的xbean组件,很简单。但是导入呢? 目前的想法是用saxparser解析文档,最后来insert into table values""""""...但value值如何从串行改为并行呢?别告诉我用字符串。因为哪几个函数都是void型。 从网上查到得例子都是只有解析部分,没有操纵value的代码。 逼得不行了。望高手们拯救小妹于水火啊,帮我改改下面的程序。不然有代码或范例也好阿。 解析程序,部分自动生成,部分修改 package com.borland.samples.xml.saxparser; import java.io.IOException; import org.xml.sax.*; import org.xml.sax.helpers.*; import javax.xml.parsers.*; public class MySaxParser extends DefaultHandler { private static int INDENT = 2; private static String attList = "" ; public static void main(String[] argv) { if (argv.length != 1) { System.out.println("Usage: java MySaxParser [URI]"); System.exit(0); } System.setProperty("javax.xml.parsers.SAXParserFactory", "org.apache.xerces.jaxp.SAXParserFactoryImpl"); String uri = argv[0]; try { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); parserFactory.setValidating(false); parserFactory.setNamespaceAware(false); MySaxParser MySaxParserInstance = new MySaxParser(); SAXParser parser = parserFactory.newSAXParser(); parser.parse(uri, MySaxParserInstance); } catch(IOException ex) { ex.printStackTrace(); } catch(SAXException ex) { ex.printStackTrace(); } catch(ParserConfigurationException ex) { ex.printStackTrace(); } catch(FactoryConfigurationError ex) { ex.printStackTrace(); } } private int idx = 0; 得到元素内容但是如何存起来呢 public void characters(char[] ch, int start, int length) throws SAXException { String s = new String(ch, start, length); String T = new String(ch, start, length); if (!s.startsWith("\n")) System.out.println(getIndent()+ " Value: " + s); } 我估计在这里加一个insert函数 public void endDocument() throws SAXException { idx -= INDENT; System.out.println(getIndent() + "end document"); System.out.println("...PARSING ends"); } public void endElement(String uri, String localName, String qName) throws SAXException { if (!attList.equals("")) System.out.println(getIndent() + " Attributes: " + attList); attList = ""; System.out.println(getIndent() + "end element"); idx -= INDENT; } public void startDocument() throws SAXException { idx += INDENT; System.out.println("PARSING begins..."); System.out.println(getIndent() + "start document: "); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { idx += INDENT; System.out.println('\n' + getIndent() + "start element: " + qName); if (attributes.getLength()> 0) { idx += INDENT; for (int i = 0; i < attributes.getLength(); i++){ attList = attList + attributes.getQName(i) + " = " + attributes.getValue(i); if (i < (attributes.getLength() - 1)) attList = attList + ", "; } idx -= INDENT; } } private String getIndent() { StringBuffer sb = new StringBuffer(); for (int i = 0; i < idx; i++) sb.append(" "); return sb.toString(); } } 要导入得xml文档 <?xml version="1.0"?> <XmlEmployees> <XmlEmployee> <EmpNo>2</EmpNo> <FirstName>Robert</FirstName> <LastName>Nelson</LastName> </XmlEmployee> </XmlEmployees> 结果 PARSING begins... start document: start element: XmlEmployees start element: XmlEmployee start element: EmpNo Value: 2 end element start element: FirstName Value: Robert end element start element: LastName Value: Nelson end element end element end element end document ...PARSING ends
|