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("------------------");
    		}
    	}
    
    }
    

     

  • 相关阅读:
    C++ 如何重复利用一个内存地址块
    C与C++在const用法上的区别
    C++ 与设计模式学习(其一)
    C/C++ 关于生成静态库(lib)/动态库(dll)文件如何使用(基于windows基础篇)
    c/c++----网站及其后门(CGI应用程序)
    C/C++深度copy和浅copy
    C/C++ 一段代码区分数组指针|指针数组|函数指针|函数指针数组
    C/C++ 如何劫持别人家的命令||函数||程序(只能对于window而言)
    C++继承与派生(原理归纳)
    Linux下如何查看自己的服务器有没有无线网卡
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5344399.html
Copyright © 2011-2022 走看看