以下是应该能够在Elipse上运行的正确代码。但是我收到错误消息:“CreatedBibData [Java Application]用法:CreateBibData文件名”。
import java.io.*;
import java.net.*;
import org.xml.sax.*;
import oracle.xml.parser.v2.SAXParser;
public class CreateBibData extends HandlerBase {
String elementEncountered;
String journalName, journalCode, vol, num,
startPage, endPage, title;
String authorNames[];
int numRows;
int numAuthors;
static public void main(String[] argv) {
try {
if (argv.length != 1) {
// Must pass in the name of the XML file.
System.err.println("Usage: CreateBibData filename");
System.exit(1);
}
// Create a new handler for the parser
CreateBibData createData = new CreateBibData();
// Get an instance of the parser
Parser parser = new SAXParser();
// Set Handlers in the parser
parser.setDocumentHandler(createData);
parser.setErrorHandler(createData);
// Convert file to URL and parse
try {
parser.parse(createURL(argv[0]).toString());
} catch (SAXParseException e) {
System.out.println(e.getMessage());
}
catch (SAXException e) {
System.out.println(e.getMessage());
}
} catch (Exception e) {
System.out.println(e.toString());
}
}
public void startDocument() {
System.out.println("--Start of SQL Insert Statements");
authorNames = new String[20];
numRows=0;
}
public void endDocument() throws SAXException {
System.out.println("--End of SQL Insert Statements");
System.out.println("Just generated "+numRows+
" SQL insert statements");
}
public void startElement(String name, AttributeList atts)
throws SAXException {
elementEncountered = name;
if (name.equals("Authors")) {
numAuthors=0;
}
}
public void endElement(String name) throws SAXException {
if (name.equals("Article")) {
String aid=journalCode+":"+vol+":"+num+":"+startPage;
System.out.println("insert into articles values ('"+
aid +"','" + journalName+"',"+ vol+","+ num+","+
startPage+","+ endPage+",'"+ title+"');");
numRows++;
for (int i=0; i<numAuthors; i++) {
System.out.println("insert into authors values('"+
aid+"','"+authorNames[i]+"');");
numRows++;
}
numAuthors=0;
}
}
public void characters(char[] cbuf, int start, int len) {
if (elementEncountered.equals("JournalName")) {
journalName = new String(cbuf,start,len);
int ii = journalName.indexOf("(");
int jj = journalName.indexOf(")");
journalCode=journalName.substring(ii+1,jj);
}
else if (elementEncountered.equals("Volume")) {
vol=new String(cbuf,start,len);
}
else if (elementEncountered.equals("Number")) {
num=new String(cbuf,start,len);
}
else if (elementEncountered.equals("Title")) {
title=new String(cbuf,start,len);
}
else if (elementEncountered.equals("StartPage")) {
startPage=new String(cbuf,start,len);
}
else if (elementEncountered.equals("EndPage")) {
endPage=new String(cbuf,start,len);
}
else if (elementEncountered.equals("Title")) {
title=new String(cbuf,start,len);
}
else if (elementEncountered.equals("Author")) {
authorNames[numAuthors++]=new String(cbuf,start,len);
}
elementEncountered = "";
}
public void warning (SAXParseException e)
throws SAXException {
System.out.println("Warning:"+e.getMessage());
}
public void error (SAXParseException e)
throws SAXException {
throw new SAXException(e.getMessage());
}
public void fatalError (SAXParseException e)
throws SAXException {
System.out.println("Fatal error");
throw new SAXException(e.getMessage());
}
static URL createURL(String fileName) {
URL url = null;
try {
url = new URL(fileName);
} catch (MalformedURLException ex) {
File f = new File(fileName);
try {
String path = f.getAbsolutePath();
String fs = System.getProperty("file.separator");
if (fs.length() == 1) {
char sep = fs.charAt(0);
if (sep != '/')
path = path.replace(sep, '/');
if (path.charAt(0) != '/')
path = '/' + path;
}
path = "file://" + path;
url = new URL(path);
} catch (MalformedURLException e) {
System.out.println("Cannot create url for: "+
fileName);
System.exit(0);
}
}
return url;
}
}
有谁知道这是为什么?