zoukankan      html  css  js  c++  java
  • [Java] XML 解析 与 Dom4j 的使用

    DOM4JTest.java

    package com.bjsxt.dom4j;
    
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    
    import java.util.Iterator;
    import java.util.List;
    
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.Node;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.SAXReader;
    import org.dom4j.io.XMLWriter;
    
    public class DOM4JTest {
        public static void main(String[] args) throws Exception {
            SAXReader reader = new SAXReader();
            Document document = reader.read(new File("User.hbm.xml"));
            Element rootElement = document.getRootElement();
            p(rootElement.getName());
    
            // 枚举所有子节点
            for (Iterator i = rootElement.elementIterator(); i.hasNext();) {
                Element element = (Element) i.next();
                // do something
                p(element.getName());
    
                // 枚举属性
                for (Iterator j = element.attributeIterator(); j.hasNext();) {
                    Attribute attribute = (Attribute) j.next();
                    // do something
                    p(attribute.getName() + "-" + attribute.getValue());
                }
            }
            // xpath = xml+path
            List<Node> list = document.selectNodes("//hibernate-mapping/class/property");
            //Node node = document.selectSingleNode("//hibernate-mapping/class/property"); 只拿第一个
            //String name = node.valueOf( @name );
            for (Node n : list) {
                p(n.getName());
                p(n.valueOf("@name"));
            }
            createDocument();
            
        }
    
        public static void p(Object o) {
            System.out.println(o);
        }
        public static Document createDocument() throws Exception {
               Document document = DocumentHelper.createDocument();
               Element root = document.addElement("hibernate-mapping");
               Element classElement = root.addElement("class")
                       .addAttribute("name", "com.bjsxt.User")
                       .addAttribute("table", "t_user");
               classElement.addElement("property").addAttribute("name", "username");
               /*
               FileWriter out = new FileWriter("User1.hbm.xml");
               document.write(out);
               out.flush();
               out.close();
               */
               // 美化格式
               OutputFormat format = OutputFormat.createPrettyPrint();
               XMLWriter writer = new XMLWriter(
                       new FileWriter( "User2.hbm.xml" ), format
                   );
                   writer.write( document );
                   writer.close();
               
               return document;
            }
         
    }
    
    User.hbm.xml
    <hibernate-mapping>
    	<class name="com.bjsxt.User" table="t_user">
    		<property name="username"></property>
    		<property name="password"></property>
    	</class>
    </hibernate-mapping>
    User2.hbm.xml
    <?xml version="1.0" encoding="UTF-8"?>
    
    <hibernate-mapping>
      <class name="com.bjsxt.User" table="t_user">
        <property name="username"/>
      </class>
    </hibernate-mapping>
    --

    ---

  • 相关阅读:
    使用NHibernate绑定页面数据时,出现"未能加载视图状态,正在向其中加载视图状态。。。。"的Bug
    以?替代参数配合扩展方法简化数据库操作的实践
    好代码并非一蹴而就读《代码整洁之道》
    C#中,数据处理层之数据库基类
    C#中,页面层之简易验证模块
    C#中,数据交互层参数替代方案(三)
    读《程序员成长路线图:从入门到优秀》
    unity3d 屏蔽右键选项弹窗
    unity3d与web进行交互
    unity3d 使用背景贴图
  • 原文地址:https://www.cnblogs.com/robbychan/p/3786834.html
Copyright © 2011-2022 走看看