• java读取xml文件内容


    xml文件

    <?xml version="1.0" encoding="UTF-8"?>
    <Object>
    <student>
    <stuName>张三</stuName>
    <stuAddress>郑州</stuAddress>
    <stuTel>111</stuTel>
    </student>
    <user>
    <userName>aaa</userName>
    <userAge>100</userAge>
    <userSex></userSex>
    </user>
    <book>
    <bookName>语文</bookName>
    <bookAuthor>XXX</bookAuthor>
    <bookPrice>100</bookPrice>
    </book>
    </Object>

    操作方法

    package com.xml;
    
    import java.io.File;
    import java.net.URL;
    import java.util.List;
    
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    import com.bean.Book;
    import com.bean.Student;
    import com.bean.User;
    
    public class SaxXml {
        public static void main(String[] args) throws Exception {
            //File file=new File("config/student.xml");
            //获取dom树
            URL url=SaxDbXml.class.getClassLoader().getResource("student.xml");
            SAXReader sax=new SAXReader();
            //Document dom=sax.read(file);
            Document dom = sax.read(url);
            //获取dom树里面的根节点
            Element rootEle=dom.getRootElement();
            //获取根节点下的节点元素
            List<Element> eleList=rootEle.elements();
            //System.out.println(eleList.size());a
            //循环获取每个节点元素
            for(int i=0;i<eleList.size();i++){
                
                Element childEle=eleList.get(i);
                String name=childEle.getName();
                if(name.equalsIgnoreCase("student")){
                    Student stu=new Student();
                    //获取节点下面的子节点
                    List<Element> childList=childEle.elements();
                    for(int j=0;j<childList.size();j++){
                        Element ele=childList.get(j);
                        String eleName=ele.getName();
                        if(eleName.equalsIgnoreCase("stuName")){
                            stu.setStuName(ele.getText());
                            //System.out.println(ele.getText());
                        }else if(eleName.equalsIgnoreCase("stuAddress")){
                            stu.setStuAddress(ele.getText());
                        }else if(eleName.equalsIgnoreCase("stuTel")){
                            stu.setStuTel(ele.getText());
                        }
                        
                    }
                }else if(name.equalsIgnoreCase("user")){
                    User user=new User();
                    //获取子节点元素
                    List<Element> userList=childEle.elements();
                    for(int k=0;k<userList.size();k++){
                        Element userEle=userList.get(k);
                        //判断子节点元素的名称,进行相应的存值
                        if(userEle.getName().equalsIgnoreCase("userName")){
                            user.setUserName(userEle.getText());
                        }else if(userEle.getName().equalsIgnoreCase("userSex")){
                            
                            user.setUserSex(userEle.getText());
                        }else if(userEle.getName().equalsIgnoreCase("userAge")){
                            user.setUserAge(userEle.getText());
                        }
                    }
                    
                }else if(name.equalsIgnoreCase("book")){
                    Book book=new Book();
                    List<Element> bookList=childEle.elements();
                    for(int k=0;k<bookList.size();k++){
                        Element bookEle=bookList.get(k);
                        String eleName=bookEle.getName();
                        if(eleName.equalsIgnoreCase("bookName")){
                            book.setBookName(bookEle.getText());
                        }else if(eleName.equalsIgnoreCase("bookAuthor")){
                            book.setBookAuthor(bookEle.getText());
                        }else if(eleName.equalsIgnoreCase("bookPrice")){
                            book.setBookPrice(bookEle.getText());
                            
                        }
                    }
                    
                    
                }
                
                }
            }
        }
  • 相关阅读:
    使用FormatterServices 类序列化或反序列化
    HaozesFx(飞信精灵)发布
    EF Provider for Access/ODBC 以及ADO.Net Entity Framework 与Linq to SQL的比较和适用场景:
    Fetion2008 分析 Part3:会话
    Gleaner(个人文档管理)
    This implementation is not part of the Windows Platform FIPS validated cryptographic algorithms.
    发一个收取Pop3邮件的代码
    码农架构技术周刊 | 第1期
    这样学Redis,才能技高一筹!
    CommunityServer2.1删除anonymous帐号后的解决办法
  • 原文地址:https://www.cnblogs.com/lishuo/p/4554103.html
Copyright © 2020-2023  润新知