在JAXB中使用XmlJavaTypeAdapter for java.util.Map时如何去掉项元素名
从在JAXB中使用XmlJavaTypeAdapter for java.util.Map时如何去掉项元素名,java,jaxb,Java,Jaxb,从 导入java.io.StringReader; 导入java.io.StringWriter; 导入java.util.HashMap; 导入java.util.Map; 导入javax.xml.bind.JAXBContext; 导入javax.xml.bind.Marshaller; 导入javax.xml.bind.Unmarshaller; 导入javax.xml.bind.annotation.XmlAttribute; 导入javax.xml.bind.annotation.xm
导入java.io.StringReader;
导入java.io.StringWriter;
导入java.util.HashMap;
导入java.util.Map;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.Unmarshaller;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.adapters.XmlAdapter;
导入javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
公课手册{
@XmlRootElement(name=“宣传册”)
静态课堂宣传册{
@XmlJavaTypeAdapter(courseListaAdapter.class)
@xmlement(name=“courses”)
地图课程IDMAP;
}
静态课堂{
@XmlAttribute
字符串id;
@XmlElement
字符串名;
}
静态类CourseListatAdapter扩展了XmlAdapter{
公共课程[]元帅(地图值){
返回value.values().toArray(新课程[value.size()]);
}
公共地图解组(课程[]值){
Map r=新的HashMap();
用于(课程c:价值)
r、 put(c.id,c);
返回r;
}
}
私有静态字符串convertObjectToXml(clazz类,T实例){
试一试{
JAXBContext jc=JAXBContext.newInstance(clazz);
Marshaller m=jc.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式化的_输出,true);
StringWriter sw=新的StringWriter();
m、 封送员(实例,软件);
返回sw.toString();
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
@抑制警告(“未选中”)
私有静态T convertXmlToObject(类clazz,字符串xml){
试一试{
JAXBContext jc=JAXBContext.newInstance(clazz);
解组器m=jc.createUnmarshaller();
StringReader sr=新的StringReader(xml);
T实例=(T)m.unmarshal(sr);
返回实例;
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
公共静态void main(字符串[]args){
宣传册b=新宣传册();
课程c=null;
//第一道菜
c=新课程();
c、 id=“cs501”;
c、 name=“软件工程”;
b、 coursesByIdMap=新HashMap();
b、 coursesByIdMap.put(c.id,c);
//第二道菜
c=新课程();
c、 id=“cs519”;
c、 name=“网络安全”;
b、 coursesByIdMap.put(c.id,c);
资料来源=b;
字符串sourceDisplay=getDisplay(source);
字符串xml=convertObjectToXml(宣传册.class,b);
System.out.println(sourceDisplay);
System.out.println(xml);
宣传册还原=convertXmlToObject(宣传册.class,xml);
字符串restoredDisplay=getDisplay(已还原);
System.out.println(恢复显示);
}
专用静态字符串显示(手册b){
字符串nl=System.getProperty(“line.separator”);
StringBuilder sb=新的StringBuilder();
sb.附加(nl+“手册”);
对于(Map.Entry:b.coursesByIdMap.entrySet()){
课程=entry.getValue();
sb.追加(nl+“coursesByIdMap.entry”);
sb.append(nl+”键:字符串(“+entry.getKey()+”);
sb.追加(nl+“值:课程(id=“+Course.id+”,name=“+Course.name+”));
}
使某人返回字符串();
}
}
这是输出
Brochure
coursesByIdMap.entry
key: String(cs519)
value: Course(id=cs519, name=Network Security)
coursesByIdMap.entry
key: String(cs501)
value: Course(id=cs501, name=Software Engineering)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<brochure>
<courses>
<item id="cs519">
<name>Network Security</name>
</item>
<item id="cs501">
<name>Software Engineering</name>
</item>
</courses>
</brochure>
Brochure
coursesByIdMap.entry
key: String(cs519)
value: Course(id=cs519, name=Network Security)
coursesByIdMap.entry
key: String(cs501)
value: Course(id=cs501, name=Software Engineering)
小册子
coursesByIdMap.entry
键:字符串(cs519)
值:课程(id=cs519,名称=网络安全)
coursesByIdMap.entry
键:字符串(cs501)
价值:课程(id=cs501,名称=软件工程)
网络安全
软件工程
小册子
coursesByIdMap.entry
键:字符串(cs519)
值:课程(id=cs519,名称=网络安全)
coursesByIdMap.entry
键:字符串(cs501)
价值:课程(id=cs501,名称=软件工程)
我想要的是像
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<brochure>
<courses>
<course id="cs519">
<name>Network Security</name>
</course>
<course id="cs501">
<name>Software Engineering</name>
</course>
</courses>
</brochure>
网络安全
软件工程
我似乎找不到摆脱“item”元素名称的方法
根据答案的参考资料,这里有一种使用crutch对象的方法
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class BrochureTest {
@XmlRootElement(name = "brochure")
static class Brochure {
@XmlJavaTypeAdapter(CourseListAdapter.class)
@XmlElement(name = "courses")
Map<String, Course> coursesByIdMap;
}
static class Course {
@XmlAttribute
String id;
@XmlElement
String name;
}
static class CourseListAdapter extends XmlAdapter<CoursesJaxbCrutch, Map<String, Course>> {
public CoursesJaxbCrutch marshal(Map<String, Course> value) {
CoursesJaxbCrutch courses = new CoursesJaxbCrutch();
courses.courses = value.values().toArray(new Course[value.size()]);
return courses;
}
public Map<String, Course> unmarshal(CoursesJaxbCrutch value) {
Map<String, Course> r = new HashMap<String, Course>();
for (Course c : value.courses)
r.put(c.id, c);
return r;
}
}
private static class CoursesJaxbCrutch {
@XmlElement(name = "course")
private Course[] courses;
}
private static <T> String convertObjectToXml(Class<T> clazz, T instance) {
try {
JAXBContext jc = JAXBContext.newInstance(clazz);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
m.marshal(instance, sw);
return sw.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
private static <T> T convertXmlToObject(Class<T> clazz, String xml) {
try {
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller m = jc.createUnmarshaller();
StringReader sr = new StringReader(xml);
T instance = (T) m.unmarshal(sr);
return instance;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
Brochure b = new Brochure();
Course c = null;
// 1st course
c = new Course();
c.id = "cs501";
c.name = "Software Engineering";
b.coursesByIdMap = new HashMap<String, Course>();
b.coursesByIdMap.put(c.id, c);
// 2nd course
c = new Course();
c.id = "cs519";
c.name = "Network Security";
b.coursesByIdMap.put(c.id, c);
Brochure source = b;
String sourceDisplay = getDisplay(source);
String xml = convertObjectToXml(Brochure.class, b);
System.out.println(sourceDisplay);
System.out.println(xml);
Brochure restored = convertXmlToObject(Brochure.class, xml);
String restoredDisplay = getDisplay(restored);
System.out.println(restoredDisplay);
}
private static String getDisplay(Brochure b) {
String nl = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append(nl + "Brochure");
for (Map.Entry<String, Course> entry : b.coursesByIdMap.entrySet()) {
Course course = entry.getValue();
sb.append(nl + " coursesByIdMap.entry");
sb.append(nl + " key: String(" + entry.getKey() + ")");
sb.append(nl + " value: Course(id=" + course.id + ", name=" + course.name + ")");
}
return sb.toString();
}
}
导入java.io.StringReader;
导入java.io.StringWriter;
导入java.util.HashMap;
导入java.util.Map;
导入javax.xml.bind.JAXBContext;
导入javax.xml.bind.Marshaller;
导入javax.xml.bind.Unmarshaller;
导入javax.xml.bind.annotation.XmlAttribute;
导入javax.xml.bind.annotation.xmlement;
导入javax.xml.bind.annotation.XmlRootElement;
导入javax.xml.bind.annotation.adapters.XmlAdapter;
导入javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
公开课小册子测试{
@XmlRootElement(name=“宣传册”)
静态课堂宣传册{
@XmlJavaTypeAdapter(courseListaAdapter.class)
@xmlement(name=“courses”)
地图课程IDMAP;
}
静态课堂{
@XmlAttribute
字符串id;
@XmlElement
字符串名;
}
静态类CourseListatAdapter扩展了XmlAdapter{
公共课程JAXBCRUTCH编组(地图值){
CoursesJaxbCrutch courses=新CoursesJaxbCrutch();
courses.courses=value.values().toArray(新课程[value.size()]);
返回课程;
}
公共地图解组(CoursesJaxbCrutch值){
Map r=新的HashMap();
对于(课程c:价值课程)
r、 put(c.id,c);
返回r;
}
}
私有静态类课程jaxbcrutch{
@xmlement(name=“课程”)
私人课程[]课程;
}
私有静态字符串convertObjectToXml(clazz类,T实例){
试一试{
JAXBContext jc=JAXBContext.newInstance(clazz);
Marshaller m=jc.createMarshaller();
m、 setProperty(Marshaller.JAXB_格式化的_输出,true);
StringWriter sw=新的StringWriter();
m、 封送员(实例,软件);
返回sw.toString();
}捕获(例外e){
e、 printStackTrace();
返回null;
}
}
@抑制警告(“未选中”)
私人静态电视公司
import java.io.StringReader;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.adapters.XmlAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
public class BrochureTest {
@XmlRootElement(name = "brochure")
static class Brochure {
@XmlJavaTypeAdapter(CourseListAdapter.class)
@XmlElement(name = "courses")
Map<String, Course> coursesByIdMap;
}
static class Course {
@XmlAttribute
String id;
@XmlElement
String name;
}
static class CourseListAdapter extends XmlAdapter<CoursesJaxbCrutch, Map<String, Course>> {
public CoursesJaxbCrutch marshal(Map<String, Course> value) {
CoursesJaxbCrutch courses = new CoursesJaxbCrutch();
courses.courses = value.values().toArray(new Course[value.size()]);
return courses;
}
public Map<String, Course> unmarshal(CoursesJaxbCrutch value) {
Map<String, Course> r = new HashMap<String, Course>();
for (Course c : value.courses)
r.put(c.id, c);
return r;
}
}
private static class CoursesJaxbCrutch {
@XmlElement(name = "course")
private Course[] courses;
}
private static <T> String convertObjectToXml(Class<T> clazz, T instance) {
try {
JAXBContext jc = JAXBContext.newInstance(clazz);
Marshaller m = jc.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
StringWriter sw = new StringWriter();
m.marshal(instance, sw);
return sw.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings("unchecked")
private static <T> T convertXmlToObject(Class<T> clazz, String xml) {
try {
JAXBContext jc = JAXBContext.newInstance(clazz);
Unmarshaller m = jc.createUnmarshaller();
StringReader sr = new StringReader(xml);
T instance = (T) m.unmarshal(sr);
return instance;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
public static void main(String[] args) {
Brochure b = new Brochure();
Course c = null;
// 1st course
c = new Course();
c.id = "cs501";
c.name = "Software Engineering";
b.coursesByIdMap = new HashMap<String, Course>();
b.coursesByIdMap.put(c.id, c);
// 2nd course
c = new Course();
c.id = "cs519";
c.name = "Network Security";
b.coursesByIdMap.put(c.id, c);
Brochure source = b;
String sourceDisplay = getDisplay(source);
String xml = convertObjectToXml(Brochure.class, b);
System.out.println(sourceDisplay);
System.out.println(xml);
Brochure restored = convertXmlToObject(Brochure.class, xml);
String restoredDisplay = getDisplay(restored);
System.out.println(restoredDisplay);
}
private static String getDisplay(Brochure b) {
String nl = System.getProperty("line.separator");
StringBuilder sb = new StringBuilder();
sb.append(nl + "Brochure");
for (Map.Entry<String, Course> entry : b.coursesByIdMap.entrySet()) {
Course course = entry.getValue();
sb.append(nl + " coursesByIdMap.entry");
sb.append(nl + " key: String(" + entry.getKey() + ")");
sb.append(nl + " value: Course(id=" + course.id + ", name=" + course.name + ")");
}
return sb.toString();
}
}
Brochure
coursesByIdMap.entry
key: String(cs519)
value: Course(id=cs519, name=Network Security)
coursesByIdMap.entry
key: String(cs501)
value: Course(id=cs501, name=Software Engineering)
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<brochure>
<courses>
<course id="cs519">
<name>Network Security</name>
</course>
<course id="cs501">
<name>Software Engineering</name>
</course>
</courses>
</brochure>
Brochure
coursesByIdMap.entry
key: String(cs519)
value: Course(id=cs519, name=Network Security)
coursesByIdMap.entry
key: String(cs501)
value: Course(id=cs501, name=Software Engineering)
public static void main(String[] args) {
Brochure b = new Brochure();
Course c = new Course();
c.id = "cs501";
c.name = "Software Engineering";
b.courses = new HashMap<String, Course>();
b.courses.put(c.id, c);
c = new Course() // You need to add this
c.id = "cs519";
c.name = "Network Security";
b.courses.put(c.id, c);
System.out.println(convertObjectToXml(Brochure.class, b));
}