Java to read xml file

xml file used to package objects for client and server interaction, or as a configuration file to use, how to generate xml parsing, or it becomes very necessary. java parsing xml document commonly used in four ways: DOM, SAX, JDOM and Dom4J. In contrast, the performance of Dom4J best, hibernate all xml files using this analytic approach, SAX is event-driven use of special analytical methods, albeit inefficiently considerable, but not conducive to encoding, DOM parsing the xml form of the tree, though not as effective high, but the programmer to understand and easy to operate. In a small project before the need to read xml format configuration files, between small, it uses the most traditional way to handle DOM parsing. Review summary.

DocumentBuilderFactory domFactory = DocumentBuilderFactory
                try {
                        DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
                        InputStream is = new FileInputStream(path);
                        Document dom = domBuilder.parse(is);
                        Element ement = dom.getDocumentElement();
                        NodeList nodes = ement.getChildNodes();

                        for (int i = 0; i < nodes.getLength(); i++) {
                                Node root = nodes.item(i);
                                if (root.getNodeType() == root.ELEMENT_NODE) {
                                        String name = root.getAttributes().getNamedItem("name")
                        for (Node node = root.getFirstChild(); node != null;
node = node.getNextSibling()) {
                                                        if (node.getNodeType() == node.ELEMENT_NODE) {
                } catch (Exception e) {

As the process of the basic code, the xml as a tree, level by level traversal nodes, with a very convenient.

分类:Java 时间:2010-07-26 人气:197
blog comments powered by Disqus


  • Struts 1.2 Multiple configuration files 2010-10-12

    The main configuration file: general put forward a global, global exceptions, resource files, plug-ins, common configuration of the module Module configuration files: configuration module Form and Action

  • SQL Server 2005 files and file groups 2011-01-06

    1, the meaning of files and file groups and relationships Each database has one primary data file. and a number from the file. The physical embodiment of database file. File group can include distribution of documents across multiple logical partitio

  • Getting Started with Tomcat configuration files 2010-03-29

    Tomcat basic configuration tomcat read the configuration file is the first mention briefly how to read the tomcat configuration file. tomcat on startup, first of all to find the system variable CATALINA_BASE, if not, then look for CATALINA_HOME. Then

  • Understanding Linux configuration files 2010-09-29

    Introduction Every Linux program is an executable file that contains the opcode list, CPU will perform these operations to complete a specific operation code. For example, ls command from / bin / ls document, that document contains a list of machine

  • linux file system configuration files 2011-06-10

    Blue file ---------- directory White paper ---------- General documents, such as text files, configuration files, source files, etc. ---------- Blue file link file, mainly documents created using the ln command Green ---------- executable files, exec

  • MyEclipse generate the configuration files and POJO objects hibernate 2010-04-13

    Adding a drive class12.jar (Oracle database), and if other DBMS Please select the appropriate JDBC driver 2 configuration hibernate database view Windows-Preferences-MyEclipse-DataBaseExplorer-Drivers new one Driver. Driver template select Oracle (Th

  • liferay portal 5.23 tomcat Source analysis of the load configuration files <2> 2010-07-01

    In the listener PortalContextLoaderListener call contextInitialized () method after. Go add context parameters contextClass corresponding com.liferay.portal.spring.context.PortalApplicationContext class and load the resource file called loadBeanDefin

  • Get the configuration files in WebSphere Information 2010-07-26

    In the WebSphere environment, because the project is tied up in the war package pass, and tomcat different and can not get configuration file path, the path does not of course get less than a configuration file information. The following is a viable

  • Modify Struts2 (2.16 +) configuration files 2010-08-19

    Struts1 the first study, its configuration file is placed under the WEB-INF. Transition to Struts2, the configuration file into the src always feel uncomfortable. So to make their own habits, I'll Struts2 (2.16 or later) of the configuration files ar

  • On the class loader management of resources and configuration files 2010-08-20

    Resource file resource usual modes, with the class loader management of resources and configuration files; Sample documents to be used: Resource File: which only one line statement: className = java.util.ArrayList java code: package

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C), All Rights Reserved. 版权所有 闽ICP备15018612号

processed in 0.036 (s). 13 q(s)