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
                                .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.
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of Java to read xml file

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • Build flex + spring + blazeds + hibernate application

    Build flex + spring + blazeds + hibernate application First, set up the project blazeds 1, will blazeds.war extract to a directory, such as: myflex /; 2, set up java works were such as: MyFlex, in the orientation of selection create project from exis ...

  • Hibernate connection pool configuration

    Hibernate connection pool configuration <! - Jdbc -> <property name="connection.driver_class"> oracle.jdbc.driver.OracleDriver </ property> <property name="connection.url"> jdbc: oracle: thin: @ 10.203.14.132:15

  • hibernate generic generic DAO

    package org.lzpeng.dao; import java.io.Serializable; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.criterion.Criterion; import org.springside.modules.orm.hibernate.Page; /** * * @version 2009-1-10 *

  • First Hibernate Example

    Curd a simple example. Source does not contain the dependent libraries, or playing too much of the package. PO object Note: One must have the default constructor 2 non-final modified. Otherwise useless lazy loading. UserDAOImpl category code, and other co

  • Struts2 + hibernate + spring problem user log in

    dao layer services layer action jsp <tr> <td align="center"> <b> user name: </ b> </ td> <td> <s: textfield name = "czyNumber" cssClass = "textstyle" theme = "simple" size = &q

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

  • Hibernate's lazy strategy

    hibernate Lazy strategy can be used in: <class> tag, it can be true / false Tags can <PROPERTY> values true / false type of necessary tools to enhance <set> <list> can tag values true / false / extra <many-to-one> <on ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries