• DOM JDOM DOM4J解析 练习


    准备XML,Information.xml

    View Code
     1 <?xml version="1.0" encoding="GBK"?>
    2 <information>
    3 <person>
    4 <name>张三</name>
    5 <age>19</age>
    6 <job>软件工程师</job>
    7 <employ-date>
    8 <day>12</day>
    9 <month>12</month>
    10 <year>1989</year>
    11 </employ-date>
    12 </person>
    13 <person>
    14 <name>李四</name>
    15 <age>33</age>
    16 <job>测试工程师</job>
    17 <employ-date>
    18 <day>10</day>
    19 <month>9</month>
    20 <year>2010</year>
    21 </employ-date>
    22 </person>
    23 <person>
    24 <name>王五</name>
    25 <age>28</age>
    26 <job>客服工程师</job>
    27 <employ-date>
    28 <day>6</day>
    29 <month>9</month>
    30 <year>2001</year>
    31 </employ-date>
    32 </person>
    33 <person>
    34 <name>赵六</name>
    35 <age>33</age>
    36 <job>售前工程师</job>
    37 <employ-date>
    38 <day>11</day>
    39 <month>7</month>
    40 <year>2000</year>
    41 </employ-date>
    42 </person>
    43 <person>
    44 <name>孙七</name>
    45 <age>27</age>
    46 <job>项目经理</job>
    47 <employ-date>
    48 <day>6</day>
    49 <month>8</month>
    50 <year>2010</year>
    51 </employ-date>
    52 </person>
    53 <person>
    54 <name>龙五</name>
    55 <age>39</age>
    56 <job>总经理</job>
    57 <employ-date>
    58 <day>3</day>
    59 <month>4</month>
    60 <year>1987</year>
    61 </employ-date>
    62 </person>
    63 </information>

    一,DOM解析
      1,读取XML,XMLReader.java

    View Code
     1 package com.xiong.dom;
    2 import java.io.File;
    3
    4 import javax.xml.parsers.DocumentBuilderFactory;
    5 import javax.xml.parsers.DocumentBuilder;
    6
    7 import org.w3c.dom.Document;
    8 import org.w3c.dom.Element;
    9 import org.w3c.dom.NodeList;
    10
    11 public class XMLReader
    12 {
    13 public static void main(String[] args)
    14 {
    15 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    16 DocumentBuilder builder=null;
    17 try
    18 {
    19 builder=dbf.newDocumentBuilder();
    20 }
    21 catch(Exception ex)
    22 {
    23 ex.printStackTrace();
    24 }
    25 Document doc=null;
    26 try
    27 {
    28 doc=builder.parse(new File("E:"+File.separator+"webstudy"+File.separator+"Information.xml"));
    29 }
    30 catch(Exception ex)
    31 {
    32 ex.printStackTrace();
    33 }
    34 NodeList nodelist=doc.getElementsByTagName("person");
    35 for(int i=0;i<nodelist.getLength();i++)
    36 {
    37 Element link=(Element)nodelist.item(i);
    38 System.out.println("姓名:"+link.getElementsByTagName("name").item(0).getFirstChild().getNodeValue());
    39 System.out.println("年龄:"+link.getElementsByTagName("age").item(0).getFirstChild().getNodeValue());
    40 System.out.println("职业:"+link.getElementsByTagName("job").item(0).getFirstChild().getNodeValue());
    41 Element employdate=(Element)link.getElementsByTagName("employ-date").item(0);
    42 String day=employdate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
    43 String month=employdate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
    44 String year=employdate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
    45 System.out.println("雇佣日期:"+year+"-"+month+"-"+day);
    46 }
    47 }
    48 }

      2,写入XML,XMLWriter.java

    View Code
      1 package com.xiong.dom;
    2 import java.io.File;
    3
    4 import javax.xml.parsers.DocumentBuilderFactory;
    5 import javax.xml.parsers.DocumentBuilder;
    6
    7 import javax.xml.transform.TransformerFactory;
    8 import javax.xml.transform.Transformer;
    9 import javax.xml.transform.OutputKeys;
    10 import javax.xml.transform.dom.DOMSource;
    11 import javax.xml.transform.stream.StreamResult;
    12
    13 import org.w3c.dom.Document;
    14 import org.w3c.dom.Element;
    15 import org.w3c.dom.Text;
    16
    17 public class XMLWriter
    18 {
    19 public static void main(String[] args)
    20 {
    21 DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
    22 DocumentBuilder builder=null;
    23 try
    24 {
    25 builder=dbf.newDocumentBuilder();
    26 }
    27 catch(Exception ex)
    28 {
    29 ex.printStackTrace();
    30 }
    31 Document doc=builder.newDocument();
    32 Element information=null;
    33 Element person=null;
    34 Element name=null;
    35 Element age=null;
    36 Element job=null;
    37 Element employdate=null;
    38 try
    39 {
    40 information=doc.createElement("information");
    41 person=doc.createElement("person");
    42
    43 name=doc.createElement("name");
    44 Text na=doc.createTextNode("王麻子");
    45 name.appendChild(na);
    46
    47 age=doc.createElement("age");
    48 Text ag=doc.createTextNode("29");
    49 age.appendChild(ag);
    50
    51 job=doc.createElement("job");
    52 Text jb=doc.createTextNode("项目经理");
    53 job.appendChild(jb);
    54
    55 employdate=doc.createElement("employ-date");
    56 Text day=doc.createTextNode("-12");
    57 Text month=doc.createTextNode("-12");
    58 Text year=doc.createTextNode("2012");
    59 employdate.appendChild(year);
    60 employdate.appendChild(month);
    61 employdate.appendChild(day);
    62
    63 person.appendChild(name);
    64 person.appendChild(age);
    65 person.appendChild(job);
    66 person.appendChild(employdate);
    67 information.appendChild(person);
    68 doc.appendChild(information);
    69
    70 }
    71 catch(Exception ex)
    72 {
    73 ex.printStackTrace();
    74 }
    75 TransformerFactory tFactory=TransformerFactory.newInstance();
    76 Transformer tf=null;
    77 try
    78 {
    79 tf=tFactory.newTransformer();
    80 }
    81 catch(Exception ex)
    82 {
    83 ex.printStackTrace();
    84 }
    85 try
    86 {
    87 tf.setOutputProperty(OutputKeys.ENCODING,"GBK");
    88 }
    89 catch(Exception ex)
    90 {
    91 ex.printStackTrace();
    92 }
    93 DOMSource ds=new DOMSource(doc);
    94 StreamResult srt=new StreamResult(new File("E:"+File.separator+"webstudy"+File.separator+"Information-02.xml"));
    95 try
    96 {
    97 tf.transform(ds,srt);
    98 }
    99 catch(Exception ex)
    100 {
    101 ex.printStackTrace();
    102 }
    103 }
    104 }

    二,JDOM解析

      1,读取XML,JDomReader.java

    View Code
     1 package com.xiong.jdom;
    2 import java.util.List;
    3 import java.io.File;
    4
    5 import org.jdom.Document;
    6 import org.jdom.Element;
    7 import org.jdom.input.SAXBuilder;
    8
    9 public class JDomReader
    10 {
    11 public static void main(String[] args)
    12 {
    13 SAXBuilder builder=new SAXBuilder();
    14 Document doc=null;
    15 try
    16 {
    17 doc=builder.build(new File("E:"+File.separator+"webstudy"+File.separator+"Information.xml"));
    18 }
    19 catch(Exception ex)
    20 {
    21 ex.printStackTrace();
    22 }
    23 Element rootElement=doc.getRootElement();
    24 List list=rootElement.getChildren("person");
    25 for(int i=0;i<list.size();i++)
    26 {
    27 Element el=(Element)list.get(i);
    28 System.out.println("姓名:"+el.getChildText("name"));
    29 System.out.println("年龄:"+el.getChildText("age"));
    30 System.out.println("职业:"+el.getChildText("job"));
    31 Element child_el=el.getChild("employ-date");
    32 System.out.println("雇佣日期:"+child_el.getChildText("year")+"-"+child_el.getChildText("month")+"-"+child_el.getChildText("day")+"\r\n");
    33 }
    34
    35 }
    36 }

      2,写入XML,JDomWriter.java

    View Code
     1 package com.xiong.jdom;
    2 import java.io.File;
    3 import java.io.FileOutputStream;
    4
    5 import org.jdom.Document;
    6 import org.jdom.Element;
    7 import org.jdom.output.XMLOutputter;
    8 public class JDomWriter
    9 {
    10 public static void main(String[] args)
    11 {
    12 Element information =new Element("information");
    13 Element person =new Element("person");
    14 Element name =new Element("name");
    15 Element age =new Element("age");
    16 Element job =new Element("job");
    17 Element employdate =new Element("employdate");
    18 Element day =new Element("day");
    19 Element month =new Element("month");
    20 Element year =new Element("year");
    21
    22 name.setText("孔向东");
    23 age.setText("39");
    24 job.setText("大学教授");
    25 day.setText("13");
    26 month.setText("11");
    27 year.setText("2009");
    28
    29 employdate.addContent(year);
    30 employdate.addContent(month);
    31 employdate.addContent(day);
    32
    33 person.addContent(name);
    34 person.addContent(age);
    35 person.addContent(job);
    36 person.addContent(employdate);
    37
    38 information.addContent(person);
    39 Document doc=new Document(information);
    40
    41 XMLOutputter out=new XMLOutputter();
    42 out.setFormat(out.getFormat().setEncoding("GBK"));
    43 try
    44 {
    45 out.output(doc,new FileOutputStream(new File("E:"+File.separator+"webstudy"+File.separator+"Information-03.xml")));
    46 }
    47 catch(Exception ex)
    48 {
    49 ex.printStackTrace();
    50 }
    51
    52 }
    53 }

    三,DOM4J解析

      1,读取XML,Dom4jReader.java

    View Code
     1 package com.xiong.dom4j;
    2 import java.util.Iterator;
    3 import java.io.File;
    4
    5 import org.dom4j.Document;
    6 import org.dom4j.Element;
    7 import org.dom4j.io.SAXReader;
    8
    9 public class Dom4jReader
    10 {
    11 public static void main(String[] args)
    12 {
    13 SAXReader reader=new SAXReader();
    14 Document doc=null;
    15 try
    16 {
    17 doc=reader.read(new File("E:"+File.separator+"webstudy"+File.separator+"Information.xml"));
    18 }
    19 catch(Exception ex)
    20 {
    21 ex.printStackTrace();
    22 }
    23 Element rootElement=doc.getRootElement();
    24 Iterator iter=rootElement.elementIterator();
    25 while(iter.hasNext())
    26 {
    27 Element person=(Element)iter.next();
    28 System.out.println("姓名:"+person.elementText("name"));
    29 System.out.println("年龄:"+person.elementText("age"));
    30 System.out.println("职业:"+person.elementText("job"));
    31 Iterator employdate_iter=person.elementIterator();
    32 String year=null;
    33 String month=null;
    34 String day=null;
    35 while(employdate_iter.hasNext())
    36 {
    37 Element employdate=(Element)employdate_iter.next();
    38 year=employdate.elementText("year");
    39 month=employdate.elementText("month");
    40 day=employdate.elementText("day");
    41 }
    42 System.out.println("雇佣日期:"+year+"-"+month+"-"+day+"\r\n");
    43 }
    44 }
    45 }

      2,写入XML,Dom4jWriter.java

    View Code
     1 package com.xiong.dom4j;
    2 import java.io.File;
    3 import java.io.FileOutputStream;
    4
    5 import org.dom4j.Document;
    6 import org.dom4j.Element;
    7 import org.dom4j.DocumentHelper;
    8
    9 import org.dom4j.io.XMLWriter;
    10 import org.dom4j.io.OutputFormat;
    11
    12 public class Dom4jWriter
    13 {
    14 public static void main(String[] args)
    15 {
    16 Document doc=DocumentHelper.createDocument();
    17 Element information=doc.addElement("information");
    18 Element person=information.addElement("person");
    19
    20 Element name=person.addElement("name");
    21 Element age=person.addElement("age");
    22 Element job=person.addElement("job");
    23 name.setText("吴小斌");
    24 age.setText("29");
    25 job.setText("苦逼程序员");
    26
    27 Element employdate=person.addElement("employ-date");
    28 Element year=employdate.addElement("year");
    29 Element month=employdate.addElement("month");
    30 Element day=employdate.addElement("day");
    31 year.setText("2006");
    32 month.setText("1");
    33 day.setText("16");
    34
    35 OutputFormat outformat=OutputFormat.createPrettyPrint();
    36 outformat.setEncoding("GBK");
    37
    38 XMLWriter xmlwriter=null;
    39 try
    40 {
    41 xmlwriter=new XMLWriter(new FileOutputStream(new File("E:"+File.separator+"webstudy"+File.separator+"Information-05.xml")),outformat);
    42 xmlwriter.write(doc);
    43 xmlwriter.close();
    44 }
    45 catch(Exception ex)
    46 {
    47 ex.printStackTrace();
    48 }
    49 }
    50 }
  • 相关阅读:
    Linux下基于PAM机制的USB Key的制作
    IP-route管理路由
    Linux下CPU主板监控工具lm_sensors
    两个网卡隔离方法
    关机后内存的数据是怎么丢失的呢?
    6.0 移动端页面布局
    CyberPlayer 使用教程
    5.10 HTML5 音频和视频
    让Ecshop网店系统用户自动登陆
    設定 Bootstrap/SASS/Bower/gulp (Windows平台)
  • 原文地址:https://www.cnblogs.com/xiongyu/p/2328630.html
Copyright © 2020-2023  润新知