java 01 XML
SAX 解析方式 相比DOM 快 更有效 不能对节点修改
DOM解析方法 慢 内存有压力,但是可以遍历和修改节点
使用DOM 解析XML的流程
1、创建SAXReader
2、用SAXReader 读取XML文档并返回Document对象
DOM解析耗时耗资源的体现
DOM会将XML文档全部读取并以一个Document对象形式存于内存
Document对象用于描述解析的XML文档
3、根据Document对象获取根元素
Element的每一个实例用于表示xml文档中的一个元素 一堆标签
4、按照xml的结构从根元素中开始逐级获取子元素以达到遍历xml的目的
String getName()
获取当前标签的名字
List elements()
获取当前标签下的所有子标签
List elements(String name)
获取当前变迁下所有同名子标签
SAXReader xmlRead = new SAXReader(); Document doc = xmlRead.read(new FileInputStream("emplist.xml")); Element root = doc.getRootElement(); List<Emp> empList = new ArrayList<Emp>(); List<Element> elements = root.elements(); for(Element sub:elements) { int id = Integer.parseInt(sub.attribute("id").getValue()); Element nameEle = sub.element("name"); String name = nameEle.getTextTrim(); int age = Integer.parseInt(sub.element("age").getTextTrim()); String gender = sub.element("gender").getTextTrim(); int salary = Integer.parseInt(sub.element("salary").getTextTrim()); empList.add(new Emp(id,name,age,gender,salary)); } System.out.println(empList.size()); for(Emp sub:empList) { System.out.println(sub.toString()); }
写出XML文档的大致步骤
创建应给Document 对象 表示一个空白的xml文档
想Document对象中添加根元素
按照应当生成的xml文档的格式逐级向根元素中添加子元素以形成xml文档格式
创建XmlWriter
通过XmlWriter将Document 写出
Document doc = DocumentHelper.createDocument();
通过DocumentHepler 创建一个Document 对象 简单
doc.addElement("xx")
doc对象只能调用一次addElement 添加根元素 返回一个Element对象
Element emp = root.addElement("emp");
emp 对象可以可以调用addElement 添加子元素 返回一个Element对象
emp.addAttribute("id", String.valueOf(sub.getId()));
emp 添加属性 返回自身
emp.addText(arg0)
emp 添加文本.返回自身
package maven; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.SAXWriter; import org.dom4j.io.XMLWriter; public class WriteXml { public static void main(String[] args) { List<Emp> empList = new ArrayList<Emp>(); empList.add(new Emp(1,"长大",22,"女",1000)); empList.add(new Emp(3,"长二",20,"男",2000)); empList.add(new Emp(5,"长三",18,"女",1000)); empList.add(new Emp(7,"长四",16,"男",2000)); empList.add(new Emp(9,"长五",14,"女",3000)); Document doc = DocumentHelper.createDocument(); Element root = doc.addElement("list"); for(Emp sub : empList) { Element emp = root.addElement("emp"); emp.addAttribute("id", String.valueOf(sub.getId())); emp.addElement("name").addText(sub.getName()); emp.addElement("age").addText(String.valueOf(sub.getAge())); emp.addElement("gender").addText(sub.getGender()); emp.addElement("salary").addText(String.valueOf(sub.getSalary())); } FileOutputStream fos = null; XMLWriter weriter = null; try { fos = new FileOutputStream("emplist2.xml"); weriter = new XMLWriter(); weriter.setOutputStream(fos); weriter.write(doc); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally { if(weriter != null) { try { weriter.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
Xpath
/ 斜杠 分隔符
.当前
..上一层
/ 根节点
//任意位置的节点
@ 某个属性
条件
[]
<dependency>
<groupId>jaxen</groupId>
<artifactId>jaxen</artifactId>
<version>1.1.4</version>
</dependency>
package maven; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.io.SAXReader; public class ReadXml2 { public static void main(String[] args) { try { SAXReader read = new SAXReader(); Document doc = read.read(new FileInputStream("emplist2.xml")); List list = doc.selectNodes("//*"); System.out.print(list.size()); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }