<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE students [ <!ELEMENT students (student+)> <!ELEMENT student (name,course,score)> <!ATTLIST student id CDATA #REQUIRED> <!ELEMENT name (#PCDATA)> <!ELEMENT course (#PCDATA)> <!ELEMENT score (#PCDATA)> ]> <students> <student id="11"> <name>张三</name> <course>JavaSE</course> <score>100</score> </student> <student id="22"> <name>李四</name> <course>Oracle</course> <score>98</score> </student> </students>
public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
//1.创建DOM解析器工厂
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//2.由DOM解析器工厂创建DOM解析器
DocumentBuilder db = dbf.newDocumentBuilder();
//3.由DOM解析器解析文档,生成DOM树
Document doc = db.parse("scores.xml");
//4.解析DOM树,获取文档内容(元素 属性 文本)
//4.1获取根元素scores
NodeList scoresList = doc.getChildNodes();
Node scoresNode = scoresList.item(1);
System.out.println(scoresList.getLength());
//4.2获取scores中所有的子元素student
NodeList studentList = scoresNode.getChildNodes();
System.out.println(studentList.getLength());
//4.3对每个student进行处理
for(int i=0;i<studentList.getLength();i++){
Node stuNode = studentList.item(i);
//System.out.println(stuNode.getNodeType());
//输出元素的属性 id
if(stuNode.getNodeType()==Node.ELEMENT_NODE){
Element elem =(Element)stuNode;
String id= elem.getAttribute("id");
System.out.println("id------>"+id);
}
//输出元素的子元素 name course score
NodeList ncsList = stuNode.getChildNodes();
//System.out.println(ncsList.getLength() );
for(int j=0;j<ncsList.getLength();j++){
Node ncs = ncsList.item(j);
if(ncs.getNodeType() == Node.ELEMENT_NODE){
String name = ncs.getNodeName();
//String value = ncs.getFirstChild().getNodeValue();//文本是元素的子节点,所以要getFirstChild
String value = ncs.getTextContent();
System.out.println(name+"----->"+value);
}
}
System.out.println();
}
}
public static void main(String[] args) throws DocumentException {
//使用dom4j解析scores2.xml,生成dom树
SAXReader reader = new SAXReader();
Document doc = reader.read(new File("scores.xml"));
//得到根节点:students
Element root = doc.getRootElement();
//得到students的所有子节点:student
Iterator<Element> it = root.elementIterator();
//处理每个student
while(it.hasNext()){
//得到每个学生
Element stuElem =it.next();
//System.out.println(stuElem);
//输出学生的属性:id
List<Attribute> attrList = stuElem.attributes();
for(Attribute attr :attrList){
String name = attr.getName();
String value = attr.getValue();
System.out.println(name+"----->"+value);
}
//输出学生的子元素:name,course,score
Iterator <Element>it2 = stuElem.elementIterator();
while(it2.hasNext()){
Element elem = it2.next();
String name = elem.getName();
String text = elem.getText();
System.out.println(name+"----->"+text);
}
System.out.println();
}
}
public class PropertiesUtil {
private static PropertiesUtil manager = null;
private static Object managerLock = new Object();
private Object propertiesLock = new Object();
private static String DATABASE_CONFIG_FILE = "/path.properties";
private Properties properties = null;
public static PropertiesUtil getInstance() {
if (manager == null) {
synchronized (managerLock) {
if (manager == null) {
manager = new PropertiesUtil();
}
}
}
return manager;
}
private PropertiesUtil() {
}
public static String getProperty(String name) {
return getInstance()._getProperty(name);
}
private String _getProperty(String name) {
initProperty();
String property = properties.getProperty(name);
if (property == null) {
return "";
} else {
return property.trim();
}
}
public static Enumeration<?> propertyNames() {
return getInstance()._propertyNames();
}
private Enumeration<?> _propertyNames() {
initProperty();
return properties.propertyNames();
}
private void initProperty() {
if (properties == null) {
synchronized (propertiesLock) {
if (properties == null) {
loadProperties();
}
}
}
}
private void loadProperties() {
properties = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream(DATABASE_CONFIG_FILE);
properties.load(in);
} catch (Exception e) {
System.err
.println("Error reading conf properties in PropertiesUtil.loadProps() "
+ e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
}
/**
* 提供配置文件路径
*
* @param filePath
* @return
*/
public Properties loadProperties(String filePath) {
Properties properties = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream(filePath);
properties.load(in);
} catch (Exception e) {
System.err
.println("Error reading conf properties in PropertiesUtil.loadProperties() "
+ e);
e.printStackTrace();
} finally {
try {
in.close();
} catch (Exception e) {
}
}
return properties;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有