java 生成xml 下载_java 生成和解析xml

本文主要使用的是Jdom.jar包(包的下载百度一下)实现了生成xml文件和解析xml文件

下面是生成xml的实现

说明:stuLists集合是一个存放着Student对象的集合

1 importjava.io.File;2 importjava.io.FileNotFoundException;3 importjava.io.FileOutputStream;4 importjava.io.IOException;5 importjava.util.ArrayList;6

7 importorg.jdom.Document;8 importorg.jdom.Element;9 importorg.jdom.input.SAXBuilder;10 importorg.jdom.output.Format;11 importorg.jdom.output.XMLOutputter;12

13 public classAddXml {14 public static voidmain(String[] args) {15 newAddXml().changeXml();16 }17 public voidchangeXml(){18 Jdbc jdbc = newJdbc();19 jdbc.addList();20 ArrayList stuLists =Jdbc.getStuList();21

22 Document docu = newDocument();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 = newXMLOutputter(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 }

结果如图所示:

3e027fce88a9f7fc9ec25cd188256b99.png

adce2cfcbd8d12308520aeec1668de18.png

下面是用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("--------------------");

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值