在JAXB中为java.util.Map使用XmlJavaTypeAdapter时如何删除item元素名称

时间:2010-02-26 01:02:45

标签: java jaxb

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

那么现在如果没有那个拐杖对象怎么办呢?

1 个答案:

答案 0 :(得分:0)

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

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

}