• xml_dom4j


    1.用dom4j解析文件

    package Xml3;
    
    import java.io.File;
    import java.util.Iterator;
    
    import org.dom4j.Attribute;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.Element;
    import org.dom4j.io.SAXReader;
    
    
    /*
     * 方法readDocumentByIterator()提供一种解析xml文件方法
     * 每个Element通过elementIterator()和attributeIterator获取
     * 其子元素和属性的迭代器
     */
    public class Dom4jTest2 {
    	private File file;
    	public Dom4jTest2(File file) {
    		this.file = file;
    	}
    	public static void main(String[] args) {
    		Dom4jTest2 test = new Dom4jTest2(
    				new File("src/com/briup/xml/chap02/student.xml"));
    		test.readDocumentByIterator();
    		//将xml转换成string类型
    		SAXReader reader = new SAXReader();
    		try {
    			Document document = reader.read
    					(new File("src/com/briup/xml/chap02/student.xml"));
    			String docXmlText = document.asXML();
    			System.out.println(docXmlText);
    			System.out.println("-------------docXmlText----------");
    			Element root = document.getRootElement();
    			String rootXmlText = root.asXML();
    			System.out.println(rootXmlText);
    			System.out.println("-------------rootXmlText----------");
    			Element son = root.element("student");
    			String sonXmlText = son.asXML();
    			System.out.println(sonXmlText);
    			System.out.println("-------------sonXmlText-----------");
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	public Document getDocument() {
    		SAXReader reader = new SAXReader();
    		Document document = null;
    		try {
    			document = reader.read(file);
    		} catch (DocumentException e) {
    			e.printStackTrace();
    		}
    		return document;
    	}
    	/*
    	 * 该方法只是枚举了两层,如果一直读取需要进行迭代循环
    	 */
    	public void readDocumentByIterator() {
    		Element root = getDocument().getRootElement();
    		//枚举根节点下所有子节点
    		for(Iterator ie = root.elementIterator();ie.hasNext();) {
    			System.out.println("========");
    			Element element = (Element) ie.next();
    			System.out.println(element.getName());
    			//枚举属性
    			for(Iterator ia = element.attributeIterator();ia.hasNext();) {
    				Attribute attribute = (Attribute) ia.next();
    				System.out.println(attribute.getName()
    						+"="+attribute.getValue());
    			}
    			//枚举当前节点下所有子节点
    			for(Iterator ieson = element.elementIterator();ieson.hasNext();) {
    				Element son = (Element) ieson.next();
    				System.out.println(son.getName()
    						+"="+son.getText());
    			}
    		}
    	}
    }
    

    1.2用来创建xml文件

    package Xml3;
    
    import java.io.File;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import org.dom4j.Document;
    import org.dom4j.DocumentException;
    import org.dom4j.DocumentHelper;
    import org.dom4j.Element;
    import org.dom4j.io.OutputFormat;
    import org.dom4j.io.XMLWriter;
    
    public class Dom4jTest {
    	public static void main(String[] args) {
    		Dom4jTest test = new Dom4jTest();
    		Document doc = test.generateDocument2();
    		File file = new File("src/com/briup/xml/chap02/student.xml");
    		test.saveDocument(doc, file);
    	}
    	public void saveDocument(Document doc,File file) {
    			//美化格式
    		OutputFormat format = OutputFormat.createPrettyPrint();
    		format.setEncoding("utf-8");
    		try {
    			//按照字符流将xml输出到指定file文件中
    			OutputStream os= new FileOutputStream(file);
    			OutputStreamWriter osw = new OutputStreamWriter(os);
    			XMLWriter writer = new XMLWriter(osw, format);
    			//根据document内容按照xml格式输出
    			writer.write(doc);
    			writer.flush();
    		} catch (Exception e) {
    			e.printStackTrace();
    		}
    	}
    	/*
    	 * <students>
    	 * 		<student id='1'>
    	 * 			<name>....</name>
    	 * 			....
    	 * 		</student>
    	 *</students>
    	 */
    	public Document generateDocument(){
    		//创建文档
    		Document document = DocumentHelper.createDocument();
    		//创建根节点
    		Element studentsElement = document.addElement("students");
    		//创建注释
    		studentsElement.addComment("this is comment");
    		//创建stu1子节点
    		Element stu1Element = studentsElement.addElement("student");
    		//stu1子节点添加属性
    		stu1Element.addAttribute("id","1");
    		Element nameElement = stu1Element.addElement("name");
    		nameElement.addText("tom");
    		Element ageElement = stu1Element.addElement("age");
    		ageElement.addText("18");
    		return document;
    	}
    	public Document generateDocument2() {
    		String str = "<?xml version='1.0' encoding='utf-8'?>"
    					+"<students><!--this is common-->"
    					+ "<student id='2'>"
    					+ "<name>jack</name><age>20</age>"
    					+ "</student></students>";
    		Document document = null;
    		try {
    			document = DocumentHelper.parseText(str);
    		} catch (DocumentException e) {
    			e.printStackTrace();
    		}
    		return document;
    	}
    }
    

      

  • 相关阅读:
    Bug生产队 【Alpha】Scrum Meeting 4
    2020春软件工程助教工作总结 第十六周
    2020春软件工程助教工作总结 第十四周
    2020春软件工程助教工作总结 第十二周
    word2vec算法原理理解
    2020春软件工程助教工作总结 第十周
    2020春软件工程助教工作总结 第八周
    2020春软件工程助教工作总结 第六周
    2020春软件工程助教工作总结 第四周
    2020春软件工程助教工作总结 第三周
  • 原文地址:https://www.cnblogs.com/yue-170305/p/11470504.html
Copyright © 2020-2023  润新知