这是一个老问题,但我最近在读取 XML 和使用相同的列名更新 postgresql 时遇到了同样的问题。这是我编写并为我工作的代码。我很难找到解决方案,所以这可能会对人们有所帮助。
public class readXmlToPostgres {
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
ArrayList<String> allcols = new ArrayList<String>();
allcols = getcolnames();
boolean hasvalue=false;
boolean colbool[] = new boolean[allcols.size()];
for(int i=0; i<allcols.size(); i++){
colbool[i] = false;
}
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLEventReader eventReader =
factory.createXMLEventReader(
new FileReader("path/to/xml/file"));
String columnName = "";
String columnValue="";
String attrval="";
String insertStatement_A = "";
String insertStatement_B = "";
String insertStatement_C="";
String insertStatement_full="";
boolean isnull = false;
ArrayList<String> insertColnames = new ArrayList<String>();
ArrayList<String> insertValues = new ArrayList<String>();
Connection ds = null;
String dbName = "";
ds = createConnection();
int count = 0;
while(eventReader.hasNext()){
XMLEvent event = eventReader.nextEvent();
switch(event.getEventType()){
case XMLStreamConstants.START_ELEMENT:
StartElement startElement = event.asStartElement();
String qName = startElement.getName().getLocalPart();
if (qName.equalsIgnoreCase("records")) {
insertColnames.clear();
insertValues.clear();
insertStatement_A="insert into \"Tablename\"(";
insertStatement_B=") values(";
insertStatement_C=" )";
insertStatement_full="";
}
for(int i=0; i<allcols.size(); i++)
{ if (qName.equalsIgnoreCase(allcols.get(i))) {
colbool[i] = true;
Iterator<Attribute> attributes = startElement.getAttributes();
columnName = allcols.get(i);
attrval="false";
if(attributes.hasNext()){
isnull = true;
colbool[i] = false;
columnName=allcols.get(i);
columnValue=null;
insertColnames.add(columnName);
insertValues.add(columnValue);
}
}
}
break;
case XMLStreamConstants.CHARACTERS:
Characters characters = event.asCharacters();
for(int i=0; i<allcols.size(); i++){
if(colbool[i]){
columnName=allcols.get(i);
columnValue=characters.getData();
insertColnames.add(columnName);
insertValues.add(columnValue);
colbool[i] = false;
}
}
break;
case XMLStreamConstants.END_ELEMENT:
EndElement endElement = event.asEndElement();
if(endElement.getName().getLocalPart().equalsIgnoreCase("records")){
System.out.println(": "+count);
count = count + 1;
for(int a=0; a<2;a++){
if((insertColnames.get(0).equals("Id")) || (insertColnames.get(0).equals("Type"))){
insertColnames.remove(0);
insertValues.remove(0);
}
}
//make query
for(int i=0; i<insertColnames.size(); i++){
insertStatement_A += "\""+insertColnames.get(i)+"\", ";
if(insertValues.get(i)==null){
// System.out.println("value is null");
insertStatement_B += ""+insertValues.get(i)+", ";
}
else{
//insertValues.get(i) = insertValues.get(i).replace("'", "");
insertStatement_B += "'"+insertValues.get(i).replace("'", "")+"', ";
}
}//for end
insertStatement_A=insertStatement_A.replaceAll(", $", "");
insertStatement_B=insertStatement_B.replaceAll(", $", "");
insertStatement_full = insertStatement_A + insertStatement_B + insertStatement_C;
insertData(insertStatement_full, ds);
}
break;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}
}
public static void insertData(String insertQuery, Connection ds) throws SQLException{
java.sql.Statement stmt = null;
stmt = ds.createStatement();
stmt.executeUpdate(insertQuery);
}
public static ArrayList<String> getcolnames() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException{
ArrayList<String> allcols = new ArrayList<String>();
allcols.clear();
Connection ds = null;
java.sql.Statement stmt = null;
String dbName = "";
ds = createConnection();
stmt = ds.createStatement();
String colQuery="SELECT column_name FROM information_schema.columns WHERE table_schema = 'public' AND table_name = 'tablename'";
ResultSet rs = stmt.executeQuery(colQuery);
while(rs.next()){
String colname=rs.getString("column_name");
allcols.add(colname);
System.out.println("column size : "+allcols.size());
return allcols;
}
public static Connection createConnection()
throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException {
Connection c;
String DbName = "Databasename";
Class.forName("org.postgresql.Driver");
c = DriverManager.getConnection("jdbc:postgresql://host/" + DbName , "user", "password");
System.out.println("connection established.");
return c;
}
}
1:它使用StAX读取xml。
2:它从数据库中获取您的列名。
3:读取 xml 并获取所需列的值,为您创建一个插入语句,最后更新您的数据库。