zoukankan      html  css  js  c++  java
  • JavaWeb学习笔记——JDOM

    JavaDOC的网址:http://www.jdom.org/docs/apidocs/index.html

    import java.io.FileOutputStream;
    
    import org.jdom2.Attribute;
    import org.jdom2.Document;
    import org.jdom2.Element;
    import org.jdom2.output.XMLOutputter;
    import org.xml.sax.Attributes;
    
    public class WriteXML {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		//建立各个操作节点
    		Element addresslist = new Element("addresslist");
    		Element linkman = new Element("linkman");
    		Element name = new Element("name");
    		Element email = new Element("email");
    		//定义属性
    		Attribute id = new Attribute("id","zs"); 
    		//声明一个Document对象
    		Document doc = new Document(addresslist);
    		//设置元素的内容
    		name.setText("张三");
    		name.setAttribute(id);				//设置name的属性
    		email.setText("www.baidu.com");
    		//设置linkman的子节点
    		linkman.addContent(name);
    		linkman.addContent(email);
    		//将linkman加入根节点中
    		addresslist.addContent(linkman);
    		//用来输出XML文件
    		XMLOutputter out = new XMLOutputter();
    		//设置输出的编码
    		out.setFormat(out.getFormat().setEncoding("UTF-8"));
    		//输出XML文件
    		try{
    			out.output(doc, new FileOutputStream("/home/common/software/coding/HelloWord/JavaWeb/bin/address.xml"));
    		}catch(Exception e){
    			e.printStackTrace();
    		}
    	}
    
    }
    

     

    import java.util.List;
    
    import org.jdom2.Document;
    import org.jdom2.Element;
    import org.jdom2.input.SAXBuilder;
    
    //=================================================
    //File Name       :	ReadXML
    //------------------------------------------------------------------------------
    //Author          :	Common
    
    
    
    //主类
    //Function        : 	ReadXML
    public class ReadXML {
    
    	public static void main(String[] args) throws Exception{
    		// TODO 自动生成的方法存根
    		//建立SAX解析
    		SAXBuilder builder = new SAXBuilder();
    		//找到Document
    		Document read_doc = builder.build("/home/common/software/coding/HelloWord/JavaWeb/bin/address.xml");
    		//读取根元素
    		Element stu = read_doc.getRootElement();
    		//得到全部linkman子元素
    		List list = stu.getChildren("linkman");
    		for(int i=0;i<list.size();i++){
    			Element e = (Element)list.get(i);		//取得全部的linkman子元素
    			String name = e.getChildText("name");
    			String id = e.getChild("name").getAttribute("id").getValue();
    			String email = e.getChildText("email");
    			
    			System.out.println("---------联系人---------");
    			System.out.println("姓名:"+name+",编号:"+id);
    			System.out.println("邮箱:"+email);
    			System.out.println("------------------");
    		}
    	}
    
    }
    

     

  • 相关阅读:
    洛谷-P5729 【深基5.例7】工艺品制作
    洛谷-P5728 【深基5.例5】旗鼓相当的对手
    洛谷-P5727 【深基5.例3】冰雹猜想
    洛谷-P1720 月落乌啼算钱
    洛谷-P4956 [COCI2017-2018#6] Davor
    洛谷-P1075 质因数分解
    洛谷-P1420 最长连号
    洛谷-P1307 数字反转
    回调地址
    OAuth 2.0
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5344399.html
Copyright © 2011-2022 走看看