XML解析

来源:互联网 发布:最新网络流行英语词汇 编辑:程序博客网 时间:2024/05/17 06:20

public class Demo4jCRUD {

public static void main(String[] args) throws DocumentException, IOException {    // read();    // create2();    // update();    delete();}

// 1.查询
public static void read() throws DocumentException {
// 1.得到Document对象
Document document = Dom4JUtils.getDocument(“WebRoot/bookstore.xml”);//xml文件路径

    // 2.得到根元素    Element root = document.getRootElement();    // 3.获取其下所有子元素book    List<Element> books = root.elements("book");    // 4.遍历List集合    for (Element book : books) {        // 5.得到book元素的category属性值.        // Attribute category = book.attribute("category");        // System.out.println(category.getValue());        String attributeValue = book.attributeValue("category");        System.out.println(attributeValue);        // 6.得到book下的子元素author,并且得到其文本信息.        // Element author = book.element("author");// 得到book下的子元素author        // String text = author.getText();        // System.out.println(text);        String text = book.elementText("author"); // 得到book下子元素author的文本信息        System.out.println(text);    }}

//查询增强注意:在dom4j中使用xpath,必须导入一个jar包,否则会产生异常(NoClassDefFoundError:org/jaxen/jaxenException)必须导入,否则xpath在dom4j中不能使用。

public static void read1()throws DocumentException {

    // 1.创建一个SaxReader    SAXReader sr = new SAXReader();    // 2. 读取一个xml文件,得到这个xml文件的Document对象.    Document document = sr.read("WebRoot/bookstore.xml");    //3.使用xpath语法    //List<Element> elements = document.selectNodes("xpath表达式");    //Node elment = document.selectSingleNode("xpath表达式");    //3.1 得到所有的author元素    // List<Element> authors = document.selectNodes("//author");    //    // for(Element author:authors){    // System.out.println(author.getText());    // }    //3.2得到title元素属性lang=en。    Element title = (Element) document.selectSingleNode("//title[@lang='en']");    System.out.println(title.getText()); //Everyday Italian}

// 2.创建
public static void create() throws DocumentException, IOException {
// 1.得到Document对象
Document document = Dom4JUtils.getDocument(“WebRoot/bookstore.xml”);

    // 2.得到根元素    Element root = document.getRootElement();    // 3.获取root的最后一个book元素    Element book = (Element) root.elements("book").get(2);    // 4.向book元素中添加子元素    // 4.1 创建子元素    Element title = DocumentHelper.createElement("title");    // 4.2给元素添加属性    // title.setAttributeValue("lang", "en"); // 过时也,我们使用addAttribute方法    title.addAttribute("lang", "en");    // 4.2给元素添加文本    title.setText("Learning XML");    // 4.3将title子元素添加到book下.    // book.add(title);    book.elements().add(0, title);    // 回写    Dom4JUtils.documentToXml(document, "WebRoot/bookstore.xml");}

// 3.创建增强
// 在bookstore.xml文件上添加以下内容
//
// Learning XML
// Erik T. Ray
// 2003-10-10
// 39.95
//
public static void create1() throws DocumentException, IOException {
// 1.得到Document对象
Document document = Dom4JUtils.getDocument(“WebRoot/bookstore.xml”);

    // 2.得到根元素    Element root = document.getRootElement();    // 3.创建元素    Element book = DocumentHelper.createElement("book");    book.addAttribute("category", "WEB");    Element title = DocumentHelper.createElement("title");    title.addAttribute("lang", "en");    title.setText("Learning XML");    Element author = DocumentHelper.createElement("author");    author.setText("Erik T. Ray");    Element year = DocumentHelper.createElement("year");    year.setText("2003-10-10");    Element price = DocumentHelper.createElement("price");    price.setText("39.95");    book.add(title);    book.add(author);    book.add(year);    book.add(price);    // 4.将book添加到根元素下    root.add(book);    // 回写    Dom4JUtils.documentToXml(document, "WebRoot/bookstore.xml");

// 4.创建增强
public static void create2() throws DocumentException, IOException {
String text = “Learning XMLErik T. Ray2003-10-1039.95“;

    // 1.得到Document对象    Document document = Dom4JUtils.getDocument("WebRoot/bookstore.xml");    // 2.得到根元素    Element root = document.getRootElement();    // 3.将text代表的字符串转换成document    Document doc = DocumentHelper.parseText(text);    // 4.将doc的根元素,添加到root下    root.add(doc.getRootElement());    // 回写    Dom4JUtils.documentToXml(document, "WebRoot/bookstore.xml");}}// 5.修改操作public static void update() throws DocumentException, IOException {    // 1.得到Document对象    Document document = Dom4JUtils.getDocument("WebRoot/bookstore.xml");    // 2.得到根元素    Element root = document.getRootElement();    // 3.获取最后一个book元素    Element book = (Element) root.elements("book").get(2);    // 4.修改book元素的category属性为java    book.addAttribute("category", "java");// 属性不存在,执行添加,属性存在,执行修改    // 5.修改book下的子元素title中的文本信息    // 5.1得到book下的子元素title    // Element title = book.element("title");    // title.setText("编程思想");    book.element("title").setText("编程思想");    // 6.回写操作    Dom4JUtils.documentToXml(document, "WebRoot/bookstore.xml");}// 6.删除操作public static void delete() throws DocumentException, IOException {    // 1.得到Document对象    Document document = Dom4JUtils.getDocument("WebRoot/bookstore.xml");    // 2.得到根元素    Element root = document.getRootElement();    // 3.获取最后一个book元素    Element book = (Element) root.elements("book").get(2);    // 4.删除book的属性category    // book.remove(book.attribute("category"));// 参数就是属性对象,可以通过元素来获取属性对象在删除    // 5.删除book元素 需要通过父元素来删除子元素    // root.remove(book);    // book.getParent().remove(book);    List elements = root.elements();// 得到当前根下所有子元素,返回的是一个List集合    elements.remove(2);    Dom4JUtils.documentToXml(document, "WebRoot/bookstore.xml");}

}

0 0
原创粉丝点击