您当前的位置: 首页 >  Java

宝哥大数据

暂无认证

  • 1浏览

    0关注

    1029博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Webservice04---java与XML的转换

宝哥大数据 发布时间:2017-09-24 19:14:30 ,浏览量:1

dom4j 使用简单, 但是dom4j会将整个文件读入, 如果文件过大, 非常耗时的。

本次学习Stax 和JAXB, Orcale公司提供的Xstream , 在java中封装成了Stax, 基于流的处理方式。

一、使用JAXB处理java与xml的转换 1.1、创建一个Classroom对象
public class Classroom {
	private int id;
	private String name;
	private int grade;
	
	public Classroom(int id, String name, int grade) {
		super();
		this.id = id;
		this.name = name;
		this.grade = grade;
	}
	
	public Classroom() {}
	setter getter ...
}
1.2、创建一个Student对象
package com.chb.model;

public class Student {
	private int id;
	private String name;
	private int age;
	private Classroom classroom;
	public Student(int id, String name, int age, Classroom classroom) {
		this.id = id;
		this.name = name;
		this.age = age;
		this.classroom = classroom;
	}
	public Student() {
	}
	setter  getter ...
}
1.3、测试java对象转化成XML
package com.chb.model;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import org.junit.Test;
public class TestJaxb {
	@Test
	public void test01() {
		try {
			JAXBContext  jaxbContext = JAXBContext.newInstance(Student.class);
			//创建Marshaller对象, 该对象功能是将Java转换为XML
			Marshaller marshaller = jaxbContext.createMarshaller();
			//使用输出流, 输出xml信息
			marshaller.marshal(new Student(1,"chb", 23, new Classroom(1, "电子信息", 2012)), System.out);
		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}
}

1.4、报错 : 由于类型 “com.chb.model.Student” 缺少 @XmlRootElement 注释, 无法将该类型编集为元素] 解决 : 在Student类添加@XmlRootElement注解
package com.chb.model;

import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Student {
	。。。
}
1.5、输出结果
2320121电子信息1chb
1.6、xml 转换成java对象
	@Test
	public void test02() throws JAXBException {
		String xml = "2320121电子信息1chb";
		JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
		//创建Unmarshaller对象, 
		Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
		//将XML转为java对象。
		Student stu = (Student)unmarshaller.unmarshal(new StringReader(xml));
		
		System.out.println(stu.getName() + "--" + stu.getClassroom().getName());
		
	}
二、Stax处理XML 2.1、基于光标   根据开始,文本,结束标签, 进行遍历, 获取对应节点的内容, 及属性。
	/**
	 * 基于光标, 
	 * 
	 */
	@Test
	public void test01() {
		try {
			//通过工厂读取StreamReader,XMLInputFactory 
			XMLInputFactory xmlInputFactory = XMLInputFactory.newFactory();
			
			InputStream in  = null;///WebService04_jaxb_xml/src/com/chb/stax/books.xml
			in = TestStax.class.getClassLoader().getResourceAsStream("com/chb/stax/books.xml");//输入流
			//通过XMLInput工厂, 获取StreamReater
			XMLStreamReader streamReader = xmlInputFactory.createXMLStreamReader(in);
			//记录文本信息
			String content  =  null;
			//开始标签
			String label = null;
			while(streamReader.hasNext()) {
				//获取节点的类型
				int type = streamReader.next();
				if (type == XMLStreamReader.START_ELEMENT) {//开始标签
					label = streamReader.getName().toString();
					if ("book".equals(label)) {//获取节点属性
						System.out.print("\t");
					}else if("bookstore".equals(label)) {
						System.out.println("");
					}else {
						System.out.print("\t\t");
					}
				}else if (type == XMLStreamReader.CHARACTERS) {//文本
					content = streamReader.getText();
					//System.out.println(content);
				}else if (type == XMLStreamReader.END_ELEMENT) {//结束标签
					String endLabel = streamReader.getName().toString();
					if (label == endLabel) {
						System.out.println( content +"");
					}else if ("book".equals(endLabel)) {
						System.out.println( "\t"+"");
					}else {
						System.out.println("");
					}
				}
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	
2.2、基于迭代模型

这里写图片描述

2.3、基于过滤器的

上面两种方式都需要从头开始读取, 这样比较耗时, 我们可以通过过滤器进行

这里写图片描述

2.4、基于XPath的解析    这个需要将整个文档读入。

这里写图片描述

三、Orcale公司提供的Xstream 更详细的可以了解这篇文章
关注
打赏
1587549273
查看更多评论
立即登录/注册

微信扫码登录

0.0387s