private File file = new File("src/book.xml"); //遍历xml文档 @Test public void testList() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); list(root); } public void list(Element element){ System.out.println(element.getName()); List<Node> nodes = element.elements(); //System.out.println(nodes.size()); for(Node node : nodes){ if(node instanceof Element){ list((Element) node); } } }
//读取某个节点的值 @Test public void read() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); Element price = root.element("书").element("作者"); String value = http://www.mamicode.com/price.getText(); System.out.println(value); }
//添加某个节点 @Test public void add() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(file);
Element price = DocumentHelper.createElement("售价"); //创建新的节点 price.setText("44元"); //设置节点值
Element root = document.getRootElement(); root.element("书").add(price); //添加节点
//将内存中的document对象重新写入xml文档 特别要注意乱码问题
//方法1:使用OutputStreamWriter设置写入文档时所使用的编码表 OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"utf-8"); document.write(out); out.close();
/*//方法2: OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gb2312"); XMLWriter writer = new XMLWriter(new FileWriter(file),format); writer.write(document); writer.close();*/
/*//方法3: OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"),format); writer.write(document); writer.close();*/
/*//方法4: OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("gb2312"); //源文档用"gb2312"编码,既不改变源文档的编码格式,又不能有乱码 XMLWriter writer = new XMLWriter(new FileOutputStream(file),format); writer.write(document); writer.close();*/
//向指定位置添加节点 @Test public void add2() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); Element price = DocumentHelper.createElement("售价"); price.setText("48元"); List list = root.element("书").elements(); list.add(2,price);
OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileOutputStream(file),format); writer.write(document); writer.close(); }
//删除某个节点 @Test public void delete() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); Element price = root.element("书").element("售价"); price.getParent().remove(price);
OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileOutputStream(file),format); writer.write(document); writer.close(); }
//更改某个节点的内容 @Test public void update() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read(file); Element root = document.getRootElement(); Element price = root.element("书").element("售价"); price.setText("58元");
OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer = new XMLWriter(new FileOutputStream(file),format); writer.write(document); writer.close(); } }