zoukankan      html  css  js  c++  java
  • JDOM解析xml

    <PeopleList>
        <People id="1">
            <Name en='zhangsan'>张三</Name>
            <Age>20</Age>
        </People>
        <People id="2">
            <Name en='lisi'>李四</Name>
            <Age>30</Age>
        </People>
    </PeopleList>
    
    

    Element:表示元素

    Attribute:表示属性

     

    SAXBuilder saxBuilder = new SAXBuilder();		//实例化对象
    Document document = saxBuilder.build(xmlPath);	        //获取xml
    Element root = document.getRootElement();		//获取根节点;示例中为peopleList
    
    List<Element> list = root.getChildren();		//element为子节点
    
    for (Element element : list) {
        List<Attribute> attributes = element.getAttributes();	//attribute为节点属性
        for (Attribute att : attributes) {		  //这里获取到people的属性,获取到的是id
            System.out.println(att.getName() + " = " + att.getValue());
        }
    
        List<Element> list1 = element.getChildren();	//获取第一子节点
        for (Element e : list1) {				//遍历第二子节点
            System.out.println("名" + e.getName());	        //第二子节点名字
            List<Attribute> attributeList = e.getAttributes();
            for (Attribute a : attributeList) {		//遍历第二子节点属性
                System.out.println("属性" + a.getValue());
                a.setValue("test");                        设置值
            }
        }
    }
    
    //设置结束以后保存值
    XMLOutputter xmlOutputter = new XMLOutputter();
    FileWriter fileWriter = new FileWriter(xmlPath);
    xmlOutputter.setFormat(Format.getPrettyFormat().setEncoding("UTF-8"));
    xmlOutputter.output(document, fileWriter);
    xmlOutputter.clone();
    
  • 相关阅读:
    TCP/IP笔记 一.综述
    Makefile的规则
    u盘安装ubuntu10.04 server.txt
    浅谈数据库技术,磁盘冗余阵列,IP分配,ECC内存,ADO,DAO,JDBC
    cocos2d-js 热更新具体解释(一)
    C#一个托付的样例
    JAVA学习之 异常处理机制
    阿里巴巴校招内推简历筛选方案
    《凑硬币》 动态规划算法入门
    android 读取xml
  • 原文地址:https://www.cnblogs.com/lyxin/p/10052308.html
Copyright © 2011-2022 走看看