gpt4 book ai didi

java - 在 JAXB 中将 XmlJavaTypeAdapter 用于 java.util.Map 时如何摆脱项目元素名称

转载 作者:行者123 更新时间:2023-12-02 05:14:14 25 4
gpt4 key购买 nike

来自 http://weblogs.java.net/blog/2005/04/22/xmladapter-jaxb-ri-ea

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 BrochureOriginal {
@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<Course[], Map<String, Course>> {
public Course[] marshal(Map<String, Course> value) {
return value.values().toArray(new Course[value.size()]);
}

public Map<String, Course> unmarshal(Course[] value) {
Map<String, Course> r = new HashMap<String, Course>();
for (Course c : value)
r.put(c.id, c);
return r;
}

}

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>
<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)

我想要的是……

<?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”元素名称的方法。

根据答案的资源,这里有一种方法可以用拐杖对象来完成。

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)

那么现在没有拐杖怎么办呢?

最佳答案

您链接中的示例不起作用 - 这里有两个链接讨论了如何获得您正在寻找的结果:

http://old.nabble.com/XmlJavaTypeAdapter-help-td19127284.html

http://forums.java.net/jive/message.jspa?messageID=267376

请注意,您的 main() 方法存在一个小问题,我已突出显示修复方法:

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));

}

关于java - 在 JAXB 中将 XmlJavaTypeAdapter 用于 java.util.Map 时如何摆脱项目元素名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2338847/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com