专业网站建设定制,南昌seo网站推广,做期货苯乙烯的网站,十种人不适合做管理者相关阅读#xff1a;1. XML文件#xff1a;什么是XML#xff1f;XML一般是指可扩展标记语言#xff0c;标准通用标记语言的子集#xff0c;是一种用于标记电子文件使其具有结构性的标记语言。2.XML文件的优点#xff1a;1)XML文档内容和结构完全分离。2)互操作性强。3)规…相关阅读1. XML文件什么是XMLXML一般是指可扩展标记语言标准通用标记语言的子集是一种用于标记电子文件使其具有结构性的标记语言。2.XML文件的优点1)XML文档内容和结构完全分离。2)互操作性强。3)规范统一。4)支持多种编码。5)可扩展性强。3.如何解析XML文档XML在不同的语言中解析XML文档都是一样的只不过实现的语法不一样基本的解析方式有两种一种是SAX方式是按照XML文件的顺序一步一步解析。另外一种的解析方式DOM方式而DOM方式解析的关键就是节点。另外还有DOM4J、JDOM等方式。本文介绍的是DOM、DOM4J方式与封装成一个工具类的方式来读取XML文档。4.XML文档:scores.xml:]张三JavaSE100李四Oracle985.DOM方式解析XMLpublic 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获取根元素scoresNodeList scoresList doc.getChildNodes();Node scoresNode scoresList.item(1);System.out.println(scoresList.getLength());//4.2获取scores中所有的子元素studentNodeList studentList scoresNode.getChildNodes();System.out.println(studentList.getLength());//4.3对每个student进行处理for(int i0;iNode stuNode studentList.item(i);//System.out.println(stuNode.getNodeType());//输出元素的属性 idif(stuNode.getNodeType()Node.ELEMENT_NODE){Element elem (Element)stuNode;String id elem.getAttribute(id);System.out.println(id------id);}//输出元素的子元素 name course scoreNodeList ncsList stuNode.getChildNodes();//System.out.println(ncsList.getLength() );for(int j0;jNode ncs ncsList.item(j);if(ncs.getNodeType() Node.ELEMENT_NODE){String name ncs.getNodeName();//String value ncs.getFirstChild().getNodeValue();//文本是元素的子节点所以要getFirstChildString value ncs.getTextContent();System.out.println(name-----value);}}System.out.println();}}6.DOM4J方式解析XML文档public static void main(String[] args) throws DocumentException {//使用dom4j解析scores2.xml,生成dom树SAXReader reader new SAXReader();Document doc reader.read(new File(scores.xml));//得到根节点studentsElement root doc.getRootElement();//得到students的所有子节点studentIterator it root.elementIterator();//处理每个studentwhile(it.hasNext()){//得到每个学生Element stuElem it.next();//System.out.println(stuElem);//输出学生的属性idList attrList stuElem.attributes();for(Attribute attr :attrList){String name attr.getName();String value attr.getValue();System.out.println(name-----value);}//输出学生的子元素namecoursescoreIterator 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();}}当然无论我们是使用那种方式解析XML的都需要导入jar包(千万不要忘记)。7.我自己的方式在实际开发的工程中我们要善于使用工具类将我们反复使用的功能封装成一个工具类所以下面的方式就是我在开发的过程中使用的方式.7.1什么是properties文件7.1.1 从结构上讲.xml文件主要是树形文件。.properties文件主要是以key-value键值对的形式存在7.1.2 从灵活的角度来说.xml文件要比.properties文件的灵活读更高一些。7.1.3 从便捷的角度来说.properties文件比.xml文件配置更加简单。7.1.4 从应用程度上来说.properties文件比较适合于小型简单的项目因为.xml更加灵活。7.2自己的properties文档在我自己的项目中创建了一个path.properties文件里面用来存放我即将使用的路径以名字值的方式存放。例如realPath D:/file/7.3 解析自己的.properties文件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;}}当我们使用之前我们只需要给 DATABASE_CONFIG_FILE 属性附上值就是我们.properties文件的名称当使用的时候我们就可以直接使用类名. getProperty(“realPath”);的方式就可以获取到在.properties文件中的key为realPath的内容。