java 生成和解析xml
Posted TheStar
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 生成和解析xml相关的知识,希望对你有一定的参考价值。
本文主要使用的是Jdom.jar包(包的下载百度一下)实现了生成xml文件和解析xml文件
下面是生成xml的实现
说明:stuLists集合是一个存放着Student对象的集合
1 import java.io.File; 2 import java.io.FileNotFoundException; 3 import java.io.FileOutputStream; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 7 import org.jdom.Document; 8 import org.jdom.Element; 9 import org.jdom.input.SAXBuilder; 10 import org.jdom.output.Format; 11 import org.jdom.output.XMLOutputter; 12 13 public class AddXml { 14 public static void main(String[] args) { 15 new AddXml().changeXml(); 16 } 17 public void changeXml(){ 18 Jdbc jdbc = new Jdbc(); 19 jdbc.addList(); 20 ArrayList<Student> stuLists = Jdbc.getStuList(); 21 22 Document docu = new Document(); 23 Element root = new Element("root"); 24 docu.addContent(root); 25 for (int i = 0; i < stuLists.size(); i++) { 26 // System.out.println(stuLists.get(i)); 27 Student s = stuLists.get(i); 28 29 Element info = new Element("info"); 30 Element student = new Element("student"); 31 Element id = new Element("id"); 32 Element name = new Element("name"); 33 Element sex = new Element("sex"); 34 Element age = new Element("age"); 35 36 Element book = new Element("book"); 37 Element bid = new Element("bid"); 38 Element bname = new Element("bname"); 39 Element bprice = new Element("bprice"); 40 Element bautor = new Element("bautor"); 41 book.addContent(bid); 42 book.addContent(bname); 43 book.addContent(bprice); 44 book.addContent(bautor); 45 46 student.addContent(id); 47 student.addContent(name); 48 student.addContent(sex); 49 student.addContent(age); 50 info.addContent(student); 51 info.addContent(book); 52 53 root.addContent(info); 54 int a = i+1; 55 String No = "000"+a; 56 student.setAttribute("No", No); 57 id.setText(s.getId()); 58 name.setText(s.getName()); 59 sex.setText(s.getSex()); 60 age.setText(s.getAge()); 61 62 String b="0"+a; 63 bid.setText(b); 64 bname.setText("java核心"); 65 bprice.setText("1334.0"); 66 bautor.setText("star"); 67 68 } 69 //格式化生成的xml文件,如果不进行格式化的话,生成的xml文件将会是很长的一行... 70 Format format = Format.getCompactFormat(); 71 format.setEncoding("utf-8"); 72 format.setIndent(" "); 73 XMLOutputter xo = new XMLOutputter(format); 74 try { 75 xo.output(docu, new FileOutputStream(new File("e:/io/stu.xml"))); 76 } catch (FileNotFoundException e) { 77 // TODO Auto-generated catch block 78 e.printStackTrace(); 79 } catch (IOException e) { 80 // TODO Auto-generated catch block 81 e.printStackTrace(); 82 } 83 System.out.println("生成xml文件成功!!!"); 84 } 85 }
结果如图所示:
下面是用java解析上面所写的xml文件
简写版
package com.direct.demo; 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; public class Saxxml { public static void main(String[] args) { //解析xml文档 SAXBuilder builder = new SAXBuilder(); Document docu = null; try { docu = builder.build("e:/io/student.xml"); } catch (JDOMException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } Element root = docu.getRootElement();//得到根目录 List stulist = root.getChildren(); System.out.println(stulist.size()+"-----------"); System.out.println("------------------读取xml文档的信息---------------------"); for (int i = 0; i < stulist.size(); i++) { Element e = (Element) stulist.get(i); String stuid; String stuname ; String stusex ; String stuage; /*String stuValues = e.getAttribute("No").getValue();//属性值 String stuValues1 = e.getAttributeValue("No"); if (stuValues.equals("102")) { //修改姓名 System.out.println(stuValues); e.getChild("stuname").setText("砖石王老五"); //删除元素 root.removeContent(e); break; }*/ if (i==0) { stuid = e.getChildText("stuid"); stuname = e.getChildText("stuname"); stusex = e.getChildText("stusex"); stuage = e.getChildText("stuage"); }else { stuid = e.getChildText("stuid"+i); stuname = e.getChildText("stuname"+i); stusex = e.getChildText("stusex"+i); stuage = e.getChildText("stuage"+i); } System.out.println("属性:"+e.getAttributeValue("No")); System.out.println("学号:"+stuid); System.out.println("姓名:"+stuname); System.out.println("年龄:"+stuage); System.out.println("性别:"+stusex); System.out.println("--------------------"); } } }
以上是关于java 生成和解析xml的主要内容,如果未能解决你的问题,请参考以下文章