JAXBのReference with XmlAdapter

sometimes i also doubt what XmlAdapter do.
1. format the String to anyType you want.[data]
2. split the String, and set every part to Object.[interesting]
3. retrieve the ref's datas to owner Object properties.
...


package ycl.learn.xml.jaxb.list;

import java.util.List;

import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;

@XmlRootElement
public class Employee {


@XmlJavaTypeAdapter(MapXmlAdapter.class)
@XmlElement(name="family")
public List<Family> family;

}



package ycl.learn.xml.jaxb.list;

import javax.xml.bind.annotation.XmlAttribute;



public class Family {

private String name;
private String value;
private Family family;

@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}

public Family getFamily() {
return family;
}
public void setFamily(Family family) {
this.family = family;
}
public String toString(){
return name+":"+value+"["+family+"]";
}

}




<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>

<family name="a" ></family>
<family name="b" value="ccc"></family>
<family name="c" value="cccc" extendss="a"></family>

</employee>


It is very easy function, just set extendss's family to it's family.


package ycl.learn.xml.jaxb.list;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class MapXmlAdapter extends XmlAdapter<MapXmlAdapter.AdaptedHashMap,Family> {

List<Family> families = new ArrayList<Family>();
Map<String,Family> mmfa = new HashMap<String,Family>();

@Override
public AdaptedHashMap marshal(Family v) throws Exception {
AdaptedHashMap ae = new AdaptedHashMap();
ae.setName(v.getName());
ae.setValue(v.getValue());
Family family = v.getFamily();
if(family!=null){
String extendss = family.getName();
ae.setExtendss(extendss);
}
return ae;
}

@Override
public Family unmarshal(AdaptedHashMap v) throws Exception {
String extendss = v.getExtendss();
Family mm = new Family();
mm.setName(v.getName());
mm.setValue(v.getValue());
if(extendss != null){
if(mmfa.containsKey(extendss)){
mm.setFamily(mmfa.get(extendss));
}else{
System.out.println("extendss is not exists");
}
}
mmfa.put(mm.getName(), mm);
return mm;
}

public static class AdaptedHashMap extends Family{

private String extendss;

@XmlAttribute
public String getExtendss() {
return extendss;
}

public void setExtendss(String extendss) {
this.extendss = extendss;
}

}


}




package ycl.learn.xml.jaxb.list;

import java.io.File;
import java.io.FileNotFoundException;

import javax.xml.bind.JAXBException;

import ycl.learn.xml.jaxb.JAXBUtil;

public class JAXBUtilTest {

private static final String FILE_NAME_emps = "employees.xml";
private static final String FILE_NAME_emps_copy = "employees_copy.xml";
/**
* @param args
* @throws JAXBException
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException, JAXBException {

JAXBUtil<Employee> ju = new JAXBUtil<Employee>();
Employee empant = ju.unmarshal(Employee.class, new File(FILE_NAME_emps),new MapXmlAdapter());
System.out.println("reader success lll000");
System.out.println(empant.family);
ju.marshal(empant, new File(FILE_NAME_emps_copy));
}

}




package ycl.learn.xml.jaxb;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.adapters.XmlAdapter;

public class JAXBUtil<T> {

@SuppressWarnings("unchecked")
public T unmarshal(Class<T> clazz, InputStream is) throws JAXBException {
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller un = context.createUnmarshaller();
return (T) un.unmarshal(is);
}

@SuppressWarnings("unchecked")
public T unmarshal(Class<T> clazz, InputStream is,XmlAdapter adapter) throws JAXBException {
if(adapter == null){
return unmarshal(clazz,is);
}else{
JAXBContext context = JAXBContext.newInstance(clazz);
Unmarshaller un = context.createUnmarshaller();
un.setAdapter(adapter);
return (T) un.unmarshal(is);
}
}

public T unmarshal(Class<T> clazz, File file) throws JAXBException, FileNotFoundException {
return unmarshal(clazz,new FileInputStream(file));
}

public T unmarshal(Class<T> clazz, File file,XmlAdapter adapter) throws JAXBException, FileNotFoundException {
return unmarshal(clazz,new FileInputStream(file),adapter);
}

public void marshal(T element,OutputStream os) throws JAXBException{
JAXBContext jc = JAXBContext.newInstance(element.getClass());
Marshaller m = jc.createMarshaller();
m.marshal(element, os);
}

public void marshal(T element,OutputStream os,XmlAdapter adapter) throws JAXBException{
if(adapter == null){
marshal(element,os);
}else{
JAXBContext jc = JAXBContext.newInstance(element.getClass());
Marshaller m = jc.createMarshaller();
m.setAdapter(adapter);
m.marshal(element, os);
}
}

public void marshal(T element, File output) throws FileNotFoundException, JAXBException{
marshal(element,new FileOutputStream(output));
}

public void marshal(T element, File output,XmlAdapter adapter) throws FileNotFoundException, JAXBException{
marshal(element,new FileOutputStream(output),adapter);
}




}



JAXBUtils is complex now, hahah, more function, more complex, this is the rule.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Spring Boot中整合JAXB,您可以按照以下步骤进行操作: 1. 添加依赖:在您的项目的pom.xml文件中,添加JAXB依赖项。 ```xml <dependency> <groupId>javax.xml.bind</groupId> <artifactId>jaxb-api</artifactId> <version>2.3.1</version> </dependency> ``` 2. 创建Java类:创建要映射到XMLJava类。使用JAXB注解配置类和字段。 ```java @XmlRootElement public class Person { private String name; private int age; // 省略构造函数、getter和setter方法 } ``` 3. 创建XML转换工具类:创建一个工具类,用于执行XML转换操作。 ```java import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Marshaller; import javax.xml.bind.Unmarshaller; import java.io.File; public class JAXBUtils { public static void marshal(Object object, File file) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(object.getClass()); Marshaller marshaller = jaxbContext.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(object, file); } public static <T> T unmarshal(Class<T> clazz, File file) throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(clazz); Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); return clazz.cast(unmarshaller.unmarshal(file)); } } ``` 4. 配置Spring Boot:在您的Spring Boot应用程序的配置类上添加JAXB相关的配置。 ```java import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.xml.MarshallingHttpMessageConverter; import org.springframework.oxm.jaxb.Jaxb2Marshaller; @Configuration public class JAXBConfig { @Bean public Jaxb2Marshaller jaxb2Marshaller() { Jaxb2Marshaller marshaller = new Jaxb2Marshaller(); marshaller.setPackagesToScan("com.example.demo"); // 设置要扫描的包路径 return marshaller; } @Bean public HttpMessageConverter<Object> marshallingHttpMessageConverter(Jaxb2Marshaller jaxb2Marshaller) { return new MarshallingHttpMessageConverter(jaxb2Marshaller); } } ``` 5. 使用JAXB:在您的控制器或服务类中使用JAXB进行XML转换操作。 ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class PersonController { @Autowired private Jaxb2Marshaller jaxb2Marshaller; @GetMapping("/person") public Person getPerson() { Person person = new Person(); person.setName("John"); person.setAge(25); return person; } @GetMapping("/xml") public String getXml() { Person person = getPerson(); StringWriter writer = new StringWriter(); jaxb2Marshaller.marshal(person, new StreamResult(writer)); return writer.toString(); } } ``` 这样,当访问`/person`接口时,将返回一个Person对象的JSON表示。当访问`/xml`接口时,将返回Person对象的XML表示。 这就是在Spring Boot中整合JAXB的基本步骤。您可以根据需要进行扩展和自定义。希望对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值