首页 > 代码库 > SaxReader读取,更新xml文件
SaxReader读取,更新xml文件
package com.sun.xml; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class ReadXmlBySaxReader { public static void main(String[] args) throws IOException { String path="C:\\Users\\Administrator\\Desktop\\bookstore.xml"; Document document=getDocument(path); getNode(document); updateEle("aaaa", document,path); addEle(document, "编程书籍", path); } public static Document getDocument(String path) throws UnsupportedEncodingException, FileNotFoundException{ SAXReader saxReader=new SAXReader(); File file=new File(path); Document document=null; InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(file)); try { document=saxReader.read(inputStreamReader); return document; } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public static void getNode(Document document){ Element element=document.getRootElement(); @SuppressWarnings("unchecked") List<Element> list=element.elements("book"); Iterator<Element> iterator=list.iterator(); while(iterator.hasNext()){ Element node=iterator.next(); Element element2=node.element("title"); System.out.println(element2.getText()); } System.out.println("ss"); } public static void updateEle(String text,Document document,String path) throws IOException{ Element element=document.getRootElement(); @SuppressWarnings("unchecked") List<Element> list=element.elements("book"); Iterator<Element> iterator=list.iterator(); while(iterator.hasNext()){ Element node=iterator.next(); Element element2=node.element("title"); System.out.println("title为: "+element2.getText()); if(element2.getText().equals("ss")){ element2.setText("java书籍"); } } writeXml(document,path); } public static void addEle(Document document,String text,String path) throws IOException{ Element rootElement=document.getRootElement(); Element element=rootElement.addElement("book"); Element element2=element.addElement("title"); Element element3=element.addElement("author"); Element element4=element.addElement("price"); element2.setText(text); element3.setText("万福"); element4.setText("39.0"); writeXml(document, path); } public static void writeXml(Document document,String path) throws IOException{ OutputFormat outputFormat=OutputFormat.createPrettyPrint(); try { XMLWriter xmlWriter=(XMLWriter) new XMLWriter(new FileOutputStream(path),outputFormat); xmlWriter.write(document); xmlWriter.close(); } catch (UnsupportedEncodingException | FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1876374
SaxReader读取,更新xml文件
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。