1. 读properties文件
Properties prop = new Properties();
InputStream in = this.class.getClassLoader().getResourceAsStream("cn/com/sever.properties");
prop.load(in);
String b2bic_ip = prop.getProperty("b2bic.sever.ip").trim();
in.close();
Setting setting = new Setting();
File lppzXmlFile = new ClassPathResource("cn/aa.xml").getFile();
Document document = new SAXReader().read(lppzXmlFile);
List
elements = document.selectNodes("/lppz/setting");
for (Element element : elements) {
String name = element.attributeValue("name");
String value = element.attributeValue("value");
try {
beanUtils.setProperty(setting, name, value);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
--------------------------------------------------------------------------------
Element element = null;
// 可以使用绝对路劲
File f = new File("D:/aa.xml");
DocumentBuilder db = null;
DocumentBuilderFactory dbf = null;
try {
dbf = DocumentBuilderFactory.newInstance();
db = dbf.newDocumentBuilder();
Document dt = db.parse(f);
element = dt.getDocumentElement();
System.out.println("根元素:" + element.getNodeName());
NodeList childNodes = element.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
Node node1 = childNodes.item(i);
if ("Account".equals(node1.getNodeName())) {
node1.getAttributes().getNamedItem("type").getNodeValue();
NodeList nodeDetail = node1.getChildNodes();
for (int j = 0; j < nodeDetail.getLength(); j++) {
Node detail = nodeDetail.item(j);
if ("code".equals(detail.getNodeName())) // 输出code
System.out.println("卡号: " + detail.getTextContent());
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
100001
123
李四
1000000.00
100002
123
张三
1000.00