zoukankan      html  css  js  c++  java
  • JDOM使用示例

    示例1:

    package com.shengsiyuan.jdom;
    
    import java.io.FileWriter;
    
    import org.jdom.Attribute;
    import org.jdom.Comment;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
    
    public class JDomTest1
    {
    	public static void main(String[] args) throws Exception
    	{
    		Document document = new Document();
    
    		Element root = new Element("root");
    
    		document.addContent(root);
    
    		Comment comment = new Comment("This is my comments");
    
    		root.addContent(comment);
    
    		Element e = new Element("hello");
    
    		e.setAttribute("sohu", "www.sohu.com");
    
    		root.addContent(e);
    
    		Element e2 = new Element("world");
    
    		Attribute attr = new Attribute("test", "hehe");
    
    		e2.setAttribute(attr);
    
    		e.addContent(e2);
    
    		e2.addContent(new Element("aaa").setAttribute("a", "b")
    				.setAttribute("x", "y").setAttribute("gg", "hh").setText("text content"));
    
    		
    		Format format = Format.getPrettyFormat();
    		
    		format.setIndent("    ");
    //		format.setEncoding("gbk");
    		
    		XMLOutputter out = new XMLOutputter(format);
    
    		out.output(document, new FileWriter("jdom.xml"));
    	}
    }
    

    示例2:

    package com.shengsiyuan.jdom;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.util.List;
    
    import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.Format;
    import org.jdom.output.XMLOutputter;
    
    public class JDomTest2
    {
    	public static void main(String[] args) throws Exception
    	{
    		SAXBuilder builder = new SAXBuilder();
    		
    		Document doc = builder.build(new File("jdom.xml"));
    		
    		Element element = doc.getRootElement();
    		
    		System.out.println(element.getName());
    		
    		Element hello = element.getChild("hello");
    		
    		System.out.println(hello.getText());
    		
    		List list = hello.getAttributes();
    		
    		for(int i = 0 ;i < list.size(); i++)
    		{
    			Attribute attr = (Attribute)list.get(i);
    			
    			String attrName = attr.getName();
    			String attrValue = attr.getValue();
    			
    			System.out.println(attrName + "=" + attrValue);
    		}
    		
    		hello.removeChild("world");
    		
    		XMLOutputter out = new XMLOutputter(Format.getPrettyFormat().setIndent("    "));
    		
    		
    		out.output(doc, new FileOutputStream("jdom2.xml"));		
    		
    	}
    }
    

      

  • 相关阅读:
    人生应该接受的教育
    【转】俞军给淘宝产品经理的分享
    【转】伪O2O已死?2016年实体零售将迎来真正的O2O
    【转】一个测试工程师的2015总结和2016年小展望
    【转】移动App测试中的最佳做法
    Net作业调度(一) -Quartz.Net入门
    Quartz学习
    Newtonsoft.Json.dll
    用C#实现Base64处理,加密解密,编码解码
    mysql 连接数的最大数
  • 原文地址:https://www.cnblogs.com/zfc2201/p/2141447.html
Copyright © 2011-2022 走看看