将 XML 数据导入 Google App Engine
2010-10-22 17:18:53 来源:WEB开发网要获取更完整的 XML 文档,您还可以使用 Simple。
要用 Java 语言创建一个 SAX Parser,扩展 org.xml.sax.helpers.DefaultHandler 类并覆盖解析文档需要的方法,如 清单 3 所示。
清单 3. EmployeeHandler.java
package com.xmlimport.employee;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jdo.PersistenceManager;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import com.xmlimport.XMLImportPersistenceManagerFactory;
public class EmployeeHandler extends DefaultHandler {
private static final Logger log = Logger.getLogger(EmployeeHandler.class.getName());
private static final SimpleDateFormat hireDateFormat =
new SimpleDateFormat("yyyy-MM-dd");
private Stack<Employee> employeeStack;
private ArrayList<Employee> employees;
private PersistenceManager pm = null;
private String characters;
public EmployeeHandler() {
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
pm = XMLImportPersistenceManagerFactory.get().getPersistenceManager();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse(new InputSource("./employees.xml"), this);
pm.makePersistentAll(employees);
} catch (Throwable t) {
t.printStackTrace();
}
finally {
pm.close();
}
}
public void startDocument()
throws SAXException {
employeeStack = new Stack<Employee>();
employees = new ArrayList<Employee>();
}
public void startElement(String namespaceURI,
String localName,
String qualifiedName,
Attributes attributes)
throws SAXException {
if (qualifiedName.equals("employee")) {
Employee employee = new Employee();
employee.setId(Long.parseLong(attributes.getValue("id")));
employeeStack.push(employee);
}
}
public void endElement(String namespaceURI,
String simpleName,
String qualifiedName)
throws SAXException {
if (!employeeStack.isEmpty()) {
if (qualifiedName.equals("employee")) {
employees.add(employeeStack.pop());
}
else if (qualifiedName.equals("firstName")) {
Employee employee = employeeStack.pop();
employee.setFirstName(characters);
employeeStack.push(employee);
}
else if (qualifiedName.equals("surName")) {
Employee employee = employeeStack.pop();
employee.setSurName(characters);
employeeStack.push(employee);
}
else if (qualifiedName.equals("emailAddress")) {
Employee employee = employeeStack.pop();
employee.setEmailAddress(characters);
employeeStack.push(employee);
}
else if (qualifiedName.equals("hireDate")) {
Employee employee = employeeStack.pop();
try {
employee.setHireDate(hireDateFormat.parse(characters));
} catch (ParseException e) {
log.log(Level.FINE, "Could not parse date {0}", characters);
}
employeeStack.push(employee);
}
}
}
public void characters(char buf[], int offset, int len)
throws SAXException {
characters = new String(buf, offset, len);
}
}
更多精彩
赞助商链接