Java学习---XML的读写操作
DOM4_Jwriter.java
package com.ftl.xmlparse; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.dom4j.*; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class DOM4_Jwriter { public static void main(String[] args) { // 创建文档 Document doc = DocumentHelper.createDocument(); // 定义节点 Element addresslist = doc.addElement("addresslist"); Element linkman = addresslist.addElement("linkman"); Element employee = addresslist.addElement("employee"); Element name = linkman.addElement("name"); Element email = linkman.addElement("email"); Element salary = employee.addElement("salary"); Element job = employee.addElement("engineer"); name.setText("FTL1012"); email.setText("www.44.com"); salary.setText("15000+"); job.setText("Java开发"); // 定义输出格式 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("GBK"); try { XMLWriter writer = new XMLWriter(new FileOutputStream(new File("G:" + File.separator + "outputt.xml")), format); writer.write(doc); writer.close(); } catch (Exception e) { e.printStackTrace(); } } }
DOM4J_Jreader.java
package com.huawei.xmlparse; import java.io.File; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class DOM4J_Jreader { public static void main(String[] args) { // TODO 自动生成的方法存根 File file = new File("G:" + File.separator + "outputt.xml"); SAXReader reader = new SAXReader(); Document doc = null; try { doc = reader.read(file); } catch (DocumentException e) { e.printStackTrace(); } Element root = doc.getRootElement(); Iterator iter = root.elementIterator(); while (iter.hasNext()) { Element linkman = (Element) iter.next(); System.out.println("姓名:" + linkman.elementText("name")); System.out.println("邮件:" + linkman.elementText("email")); Element employee = (Element) iter.next(); System.out.println("薪资:" + employee.elementText("salary")); System.out.println("工作:" + employee.elementText("engineer")); } } }
XML.java
package com.ftl.xmlparse; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.XMLWriter; public class XML { public static void main(String[] args) { Document doc = DocumentHelper.createDocument(); Element school = doc.addElement("school"); Element person = school.addElement("person"); Element library = school.addElement("library"); Element id = person.addElement("id"); school.setText("xxx"); library.setText("xu"); person.setText("FTL1012"); id.setText("041077"); OutputFormat format = OutputFormat.createCompactFormat(); format.setEncoding("GBK"); try { XMLWriter writer = new XMLWriter(new FileOutputStream("G:" + File.separator + "new2016.xml"), format); writer.write(doc); writer.close(); } catch (UnsupportedEncodingException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (FileNotFoundException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } } }
XMLReader.java
package com.ftl.xmlparse; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Iterator; 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.SAXReader; import org.dom4j.io.XMLWriter; public class XMLReader { public static void main(String[] args) { File file = new File("G:" + File.separator + "new2016.xml"); SAXReader readr = new SAXReader(); Document doc = null; try { doc = readr.read(file); } catch (DocumentException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } Element root = doc.getRootElement(); Iterator iter = root.elementIterator(); while (iter.hasNext()) { Element person = (Element) iter.next(); System.out.println("图书馆:" + root.elementText("library")); System.out.println("人::" + root.elementText("person")); System.out.println("姓名:" + person.elementText("id")); } } }
作者:小a玖拾柒
-------------------------------------------
个性签名: 所有的事情到最後都是好的,如果不好,那說明事情還沒有到最後~
本文版权归作者【小a玖拾柒】和【博客园】共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利!