我有student.xml文件并使用SAX Parser解析此文件,现在我需要将数据存储到MySQL数据库中,因此建议采用何种方法。
代码:
package sax;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class ReadXML extends DefaultHandler{
public void characters(char[] ch, int start, int length) throws SAXException {
String s =new String(ch, start, length);
if(s.trim().length()>0) {
System.out.println(" Value: "+s);
}
}
public void startDocument() throws SAXException {
System.out.println("Start document");
}
public void endDocument() throws SAXException {
System.out.println("End document");
}
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
System.out.println("start element : "+name);
}
public void endElement(String uri, String localName, String name) throws SAXException {
System.out.println("end element");
}
public static void main(String[] args) {
ReadXML handler = new ReadXML();
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
saxParser.parse("student.xml", handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
答案 0 :(得分:1)
我会创建一组表来表示students.xml中包含的数据,然后在解析数据时填充它们。
答案 1 :(得分:0)
您可以将XML直接存储到数据库中。许多数据库包具有允许将XML格式化数据插入数据库中适当位置的功能。我相信PostGres和MS-SQL可以做到这一点。
在MySQL中有现成的功能。 See here