<?xml version="1.0" encoding="UTF-8"?>
<roots>
<package name="src">
<table id="qqqqqqqqqqq">
<key name="key1">11111</key>
<key name="key2">222222</key>
</table>
<table id="tblAPTRpt">
<key name="key1">333333</key>
<key name="key2">444444444</key>
<key name="key3">5555555</key>
</table>
</package>
</roots>
package primaryKey;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
public class PrimaryKeyReader {
public static List<String> GetPrimaryKeys(String tableName){
List<String> lstString = new ArrayList<String>();
try {
File xmlFile = new File("src//primaryKey//primaryKeyFile.xml");
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();//步骤1
DocumentBuilder builder = factory.newDocumentBuilder();//步骤2
Document doc = builder.parse(xmlFile);//步骤3
NodeList nl = doc.getElementsByTagName("table");
if(null!=nl){
for (int i = 0; i < nl.getLength(); i++) {
if(((Element)(nl.item(i))).getAttribute("id").equals(tableName)){
Node tempNode= nl.item(i);
NodeList nlTemp= tempNode.getChildNodes();
for (int j = 0; j < nlTemp.getLength(); j++)
{
if(nlTemp.item(j) instanceof Element){
lstString.add(nlTemp.item(j).getTextContent());
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return lstString;
}
}