SAXReader读写xml
Posted xh_Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SAXReader读写xml相关的知识,希望对你有一定的参考价值。
解析读取XML
public static void main(String[] args) { //1获取SaxReader对象 SAXReader reader=new SAXReader(); try { //2使用Saxrreader对象加载xml文件,返回Document对象 Document document=reader.read(new File("student.xml")); //3使用document对象或根元素 Element rootele=document.getRootElement(); //4获取子元素的集合(获取的是student子元素的集合) List<Element> lsstu_ele=rootele.elements("student"); System.out.println("student元素的个数:"+lsstu_ele.size()); //遍历student元素 for (Element stuele : lsstu_ele) { /*********************************** //获取student元素下的子元素 Element sid_ele=stuele.element("sid"); Element sname_ele=stuele.element("sname"); Element sex_ele=stuele.element("sex"); Element address_ele=stuele.element("address"); Element age_ele=stuele.element("age"); //获取每个子元素的文本内容 System.out.println("编号:"+sid_ele.getText()); System.out.println("姓名:"+sname_ele.getText()); System.out.println("性别:"+sex_ele.getText()); System.out.println("地址:"+address_ele.getText()); System.out.println("年龄:"+age_ele.getText()); System.out.println("========================"); **************************************/ //获取每个子元素的文本内容 System.out.println("编号:"+stuele.elementText("sid")); System.out.println("姓名:"+stuele.elementText("sname")); System.out.println("性别:"+stuele.elementText("sex")); System.out.println("地址:"+stuele.elementText("address")); System.out.println("年龄:"+stuele.elementText("age")); System.out.println("========================"); } } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
向xml文件中添加节点并写入文件保存;
public static void main(String[] args) { //1创建SaxReader对象 SAXReader reader=new SAXReader(); try { //2读取xml文档,并返回document对象 Document document=reader.read(new File("student.xml")); //3获取根元素 Element rootele=document.getRootElement(); //4在根元素下添加student子元素 Element st_ele=rootele.addElement("student"); //在新增的student元素下添加其他子元素 Element sid_ele=st_ele.addElement("sid"); Element sname_ele=st_ele.addElement("sname"); Element sex_ele=st_ele.addElement("sex"); Element address_ele=st_ele.addElement("address"); Element age_ele=st_ele.addElement("age"); //在每个子元素下添加内容 Scanner sc=new Scanner(System.in); System.out.println("输入编号:"); sid_ele.setText(sc.next()); System.out.println("输入姓名:"); sname_ele.setText(sc.next()); System.out.println("输入性别:"); sex_ele.setText(sc.next()); System.out.println("输入地址:"); address_ele.setText(sc.next()); System.out.println("输入年龄:"); age_ele.setText(sc.next()); //创建字节输出流对象 FileOutputStream fout=new FileOutputStream("student.xml"); //创建缓冲区字符输出流对象 BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fout, "utf-8")); //写文档到xml文件 document.write(bw); bw.flush(); bw.close(); System.out.println("增加成功!"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
修改xml 文件中的节点并写入文件保存;
public static void main(String[] args) { Scanner sc=new Scanner(System.in); //1创建SaxReader对象 SAXReader reader=new SAXReader(); try { //2读取xml文档,并返回document对象 Document document=reader.read(new File("student.xml")); //3获取根元素 Element rootele=document.getRootElement(); //4获取student子元素的集合 List<Element> lsst_ele=rootele.elements("student"); System.out.println("请输入要修改的学生编号:"); String sid=sc.next(); for (Element stu_ele : lsst_ele) { if(stu_ele.elementText("sid").equals(sid)){ System.out.println("输入姓名:"); stu_ele.element("sname").setText(sc.next()); System.out.println("输入性别:"); stu_ele.element("sex").setText(sc.next()); System.out.println("输入地址:"); stu_ele.element("address").setText(sc.next()); System.out.println("输入年龄:"); stu_ele.element("age").setText(sc.next()); break; } } //创建字节输出流对象 FileOutputStream fout=new FileOutputStream("student.xml"); //创建缓冲区字符输出流对象 BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fout, "utf-8")); //写文档到xml文件 document.write(bw); bw.flush(); bw.close(); System.out.println("修改成功!"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
删除xml文件中的节点
public static void main(String[] args) { Scanner sc=new Scanner(System.in); //1创建SaxReader对象 SAXReader reader=new SAXReader(); try { //2读取xml文档,并返回document对象 Document document=reader.read(new File("student.xml")); //3获取根元素 Element rootele=document.getRootElement(); //获取要移除的学生编号 System.out.println("请输入要删除的学生编号:"); String sid=sc.next(); //获取student元素的集合 List<Element> lsstu_ele=rootele.elements("student"); for (Element stu_ele : lsstu_ele) { //在元素中查找学生编号内容是否与输入的一致 if(stu_ele.elementText("sid").equals(sid)){ rootele.remove(stu_ele);//删除整个student元素 System.out.println("元素删除成功!"); break; } } //创建字节输出流对象 FileOutputStream fout=new FileOutputStream("student.xml"); //创建缓冲区字符输出流对象 BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(fout, "utf-8")); //写文档到xml文件 document.write(bw); bw.flush(); bw.close(); System.out.println("移除成功!"); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
以上是关于SAXReader读写xml的主要内容,如果未能解决你的问题,请参考以下文章
java解析XML saxReader.read(xml) 错误:org.dom4j.DocumentException: no protocol