<span>JAXBContext 来完成xml和对象的转换操作
</span>
package org.xml;
public class Classroom {
private int id;
private String name;
private int grade;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getGrade() {
return grade;
}
public void setGrade(int grade) {
this.grade = grade;
}
public Classroom() {
super();
}
public Classroom(int id, String name, int grade) {
super();
this.id = id;
this.name = name;
this.grade = grade;
}
}
package org.xml;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Student {
private int id;
private String name;
private int age;
private Classroom classroom;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Classroom getClassroom() {
return classroom;
}
public void setClassroom(Classroom classroom) {
this.classroom = classroom;
}
public Student(int id, String name, int age, Classroom classroom) {
super();
this.id = id;
this.name = name;
this.age = age;
this.classroom = classroom;
}
public Student() {
super();
// TODO Auto-generated constructor stub
}
}
package org.xml;
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.junit.Test;
public class TestJaxb {
@Test
public void test01(){
try {
JAXBContext ctx = JAXBContext.newInstance(Student.class);
//编排:把对象转换成xml
Marshaller marshaller = ctx.createMarshaller();
Student stu = new Student(1, "zhangsan", 21, new Classroom(1, "计算机", 2010));
marshaller.marshal(stu, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
@Test
public void test02(){
try {
JAXBContext ctx = JAXBContext.newInstance(Student.class);
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><student><age>21</age><classroom><grade>2010</grade><id>1</id><name>计算机</name></classroom><id>1</id><name>zhangsan</name></student>";
//编排:把对象转换成xml
Unmarshaller unmarshaller = ctx.createUnmarshaller();
Student stu = (Student) unmarshaller.unmarshal(new StringReader(xml));
System.out.println(stu.getName()+stu.getClassroom().getName());;
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
基于Stax的对xml文件进行读取和更改操作
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
package org.xml.stax;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.stream.EventFilter;
import javax.xml.stream.FactoryConfigurationError;
import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import javax.xml.stream.events.XMLEvent;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.junit.Test;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* 基于光标的
* 都是java的api提供的
*/
public class TestStax {
/**
* 输出节点类型的表示数字
* 基于光标的方式
*/
@Test
public void test01(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()) {
int type = reader.next();
if (type == XMLStreamConstants.START_ELEMENT) {
System.out.println(reader.getName());
}else if (type == XMLStreamConstants.CHARACTERS) {
System.out.println(reader.getText().trim());
}else if (type == XMLStreamConstants.END_ELEMENT) {
System.out.println(reader.getName());
}
}
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 输出存在的书的类型
* 基于光标的方式
*/
@Test
public void test02(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()) {
int type = reader.next();
if (type == XMLStreamConstants.START_ELEMENT) {
String name = reader.getName().toString();
if (name.equals("book")) {
System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
}
}
}
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 输出存在的书的类型
* 基于光标的方式
*/
@Test
public void test03(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getResourceAsStream("books.xml");
XMLStreamReader reader = factory.createXMLStreamReader(is);
while (reader.hasNext()) {
int type = reader.next();
//判断节点类型是否是开始或者结束或者文本节点,之后根据情况进行处理
//基于光标的方式
if (type == XMLStreamConstants.START_ELEMENT) {
String name = reader.getName().toString();
if (name.equals("title")) {
System.out.println(reader.getAttributeName(0)+":"+reader.getAttributeValue(0));
System.out.print(reader.getElementText()+":");
}
if (name.equals("price")) {
System.out.println(reader.getElementText()+":");
}
}
}
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 输出存在的书的类型
* 基于迭代的方式
*/
@Test
public void test04(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getResourceAsStream("books.xml");
//基于迭代模型的操作方式
XMLEventReader reader = factory.createXMLEventReader(is);
int num = 0;
while (reader.hasNext()) {
//通过XMLEvent来获取是否是某种节点
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
//通过event.asXxxxx来转换节点
String name = event.asStartElement().getName().toString();
if (name.equals("title")) {
System.out.print(reader.getElementText()+":");
}
if (name.equals("price")) {
System.out.println(reader.getElementText()+":");
}
}
num++;
}
System.out.println("遍历次数:"+num);
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 输出存在的书的类型
* 过滤器
* 效率高于直接上述方法
*/
@Test
public void test05(){
XMLInputFactory factory = XMLInputFactory.newInstance();
InputStream is = null;
try {
is = TestStax.class.getResourceAsStream("books.xml");
//基于Filter的过滤方式,可以有效的过滤掉不进行操作的节点,效率会高
XMLEventReader reader = factory.createFilteredReader(factory.createXMLEventReader(is),
new EventFilter() {
@Override
public boolean accept(XMLEvent event) {
//返回true表示会显示,返回false表示不会显示
if (event.isStartElement()) {
String name = event.asStartElement().getName().toString();
if ("title".equals(name)||"price".equals(name)) {
return true;
}
}
return false;
}
});
int num = 0;
while (reader.hasNext()) {
//通过XMLEvent来获取是否是某种节点
XMLEvent event = reader.nextEvent();
if (event.isStartElement()) {
//通过event.asXxxxx来转换节点
String name = event.asStartElement().getName().toString();
if (name.equals("title")) {
System.out.print(reader.getElementText()+":");
}
if (name.equals("price")) {
System.out.println(reader.getElementText()+":");
}
}
num++;
}
System.out.println("遍历次数:"+num);
} catch (FactoryConfigurationError e) {
e.printStackTrace();
} catch (XMLStreamException e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 输出存在的书的类型
* DocumentBuilderFactory
* 可以读取部分文档
* 效率高于直接全部读取
*/
@Test
public void test06(){
InputStream is = null;
try {
is = TestStax.class.getResourceAsStream("books.xml");
//创建文档对象
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//通过DocumentBuilder创建Document的文档处理对象
Document doc = db.parse(is);
//创建xpath
XPath xPath = XPathFactory.newInstance().newXPath();
//第一个参数是xpath,第二个参数是文档
NodeList list = (NodeList) xPath.evaluate("//book[@category='WEB']", doc,XPathConstants.NODESET);
//遍历输出的结果
int length = list.getLength();
for (int i=0;i<length;i++) {
Element e = (Element)list.item(i);
System.out.println(e.getElementsByTagName("title").item(0).getTextContent());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* 创建xml
*/
@Test
public void test07(){
try {
XMLStreamWriter xsw = XMLOutputFactory.newInstance().createXMLStreamWriter(System.out);
//版本号
xsw.writeStartDocument("UTF-8","1.0");
xsw.writeEndDocument();
String ns = "http://aa:dd";
xsw.writeStartElement("test", "person", ns);
xsw.writeStartElement(ns,"id");
xsw.writeCharacters("1");
xsw.writeEndElement();
xsw.writeEndElement();
xsw.flush();
xsw.close();
} catch (XMLStreamException e) {
e.printStackTrace();
} catch (FactoryConfigurationError e) {
e.printStackTrace();
}
}
/**
* 使用transform来修改节点
*/
@Test
public void test08(){
InputStream is = null;
try {
is = TestStax.class.getResourceAsStream("books.xml");
//创建文档对象
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
//通过DocumentBuilder创建Document的文档处理对象
Document doc = db.parse(is);
//创建xpath
XPath xPath = XPathFactory.newInstance().newXPath();
Transformer tran = TransformerFactory.newInstance().newTransformer();
//设置输出属性
tran.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
tran.setOutputProperty(OutputKeys.INDENT, "yes");
//第一个参数是xpath,第二个参数是文档
NodeList list = (NodeList) xPath.evaluate("//book[title='Harry Potter']", doc,XPathConstants.NODESET);
//获取price节点
Element book = (Element) list.item(0);
Element price = (Element) (book.getElementsByTagName("price").item(0));
price.setTextContent("55555");
Result result = new StreamResult(System.out);
//通过transform来修改代码
tran.transform(new DOMSource(doc), result);
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerFactoryConfigurationError e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}finally{
if (is!=null) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}