dom方式:
import javax.xml.parsers.*;import org.apache.xerces.parsers.DOMParser;
import org.xml.sax.SAXException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;import org.apache.xerces.parsers.DOMParser;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
public class GenSQL {
public static void main(String[] args) {
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder parser =dbfactory.newDocumentBuilder();
Document xmldoc = parser.parse("sql.xml");
NodeList nlist = xmldoc.getDocumentElement().getElementsByTagName("row");
if ( nlist.getLength() > 0 ) {
nlist = nlist.item(0).getChildNodes();
Node node;
String strSQL = "Insert into table(";
String strValues = " Values(";
String strName;
String strValue;
int j = 0;
for ( int i = 0; i < nlist.getLength(); i++) {
node = nlist.item(i);
strName = node.getNodeName();
if ( !strName.equals("#text") ) {
strValue = node.getFirstChild().getNodeValue();
if ( j > 0 ) {
strSQL += ",";
strValues += ",";
}
j++;
strSQL += strName;
strValues += strValue;
}
}
strSQL += ")" + strValues + ")";
System.out.println(strSQL);
}

} catch (Exception exp) {
exp.printStackTrace();
}
}
}