详解Java解析XML的四种方法

来源:java认证发布时间:2012-11-12 13:15:36java认证视频

    4.JDOM生成和解析XML
    为减少DOM、SAX的编码量,出现了JDOM;优点:20-80原则,极大减少了代码量。使用场合:要实现的功能简单,如解析、创建等,但在底层,JDOM还是使用SAX(最常用)、DOM、Xanan文档。
    package com.alisoft.facepay.framework.bean;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.util.List;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;
    /**  *
    * @author hongliang.dinghl  * JDOM 生成与解析XML文档  *
    */  public class JDomDemo implements XmlDocument {
    public void createXml(String fileName) {
    Document document;
    Element  root;
    root=new Element("employees");
    document=new Document(root);
    Element employee=new Element("employee");
    root.addContent(employee);
    Element name=new Element("name");
    name.setText("ddvip");
    employee.addContent(name);
    Element sex=new Element("sex");
    sex.setText("m");
    employee.addContent(sex);
    Element age=new Element("age");
    age.setText("23");
    employee.addContent(age);
    XMLOutputter XMLOut = new XMLOutputter();
    try {
    XMLOut.output(document, new FileOutputStream(fileName));
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    public void parserXml(String fileName) {
    SAXBuilder builder=new SAXBuilder(false);
    try {
    Document document=builder.build(fileName);
    Element employees=document.getRootElement();
    List employeeList=employees.getChildren("employee");
    for(int i=0;iElement employee=(Element)employeeList.get(i);
    List employeeInfo=employee.getChildren();
    for(int j=0;jSystem.out.println(((Element)employeeInfo.get(j))。
    getName()+":"+((Element)employeeInfo.get(j))。getValue());
    }
    }
    } catch (JDOMException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    tter();
    try {
    XMLOut.output(document, new FileOutputStream(fileName));
    }
    catch (FileNotFoundException e) {
    e.printStackTrace();   }
    catch (IOException e)
    {
    e.printStackTrace();   }
    }
    public void parserXml(String fileName) {
    SAXBuilder builder=new SAXBuilder(false);
    try {
    Document document=builder.build(fileName);
    Element employees=document.getRootElement();
    List employeeList=employees.getChildren("employee");
    for(int i=0;iElement employee=(Element)employeeList.get(i);
    List employeeInfo=employee.getChildren();
    for(int j=0;jSystem.out.println(((Element)employeeInfo.get(j))。getName()+":"+((Element)employeeInfo.get(j))。getValue());
    }
    }
    } catch (JDOMException e) {
    e.printStackTrace();
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

上一页345下一页

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答