Java to read xml file

分类:Java 2010-07-26 来源:CodeWeblog.com 人气:177

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
                                .newInstance();
                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")
                                                        .getNodeValue();
                        for (Node node = root.getFirstChild(); node != null;
node = node.getNextSibling()) {
                                                        if (node.getNodeType() == node.ELEMENT_NODE) {
                        System.out.println(node.getNodeValue);
                             }
                           }
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }

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

分享到:
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 partitions to

  • 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

  • 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 look

  • 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

  • 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 are

  • 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: config.properties which only one line statement: className = java.util.ArrayList java code: package

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.370 (s). 12 q(s)