I'm trying to create mobile sitemap using JAXB. According to google documentation, it should have following format:
<?xml version="1.0" encoding="UTF-8" ?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:mobile="http://www.google.com/schemas/sitemap-mobile/1.0">
<url>
<loc>http://mobile.example.com/article100.html</loc>
<mobile:mobile/>
</url>
</urlset>
To solve this problem I created following beans:
URL.java :
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
#XmlType(propOrder = {"loc"})
public class URL {
private String loc;
#XmlElement
public String getLoc() {
return loc;
}
public void setLoc(String loc) {
this.loc = loc;
}
}
URLSet.java
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name="urlset")
public class URLSet{
private List<URL> urlset;
#XmlAttribute(name="xmlns")
private String xmlns = "http://www.sitemaps.org/schemas/sitemap/0.9";
public URLSet() {
urlset = new ArrayList<URL>();
}
#XmlElement(name="url")
public List<URL> getUrlset() {
return urlset;
}
public void setUrlset(List<URL> urlset) {
this.urlset = urlset;
}
}
marshalling:
List<URL> list = new ArrayList<URL>();
URLSet urlSet = new URLSet();
for(int i = 0 ; i < 5 ; i++) {
URL url = new URL();
url.setLoc("www.test" + i + ".com");
list.add(url);
}
urlSet.setUrlset(list);
JAXBContext jaxbContext = JAXBContext.newInstance(URLSet.class);
Marshaller m = jaxbContext.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
//m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
//m.setProperty("com.sun.xml.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
//m.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
StringWriter sw = new StringWriter();
m.marshal(urlSet,sw);
log.info("\n" + sw.toString() + "\n");
The questions are: how can I add static <mobile:mobile/> child to every node? how should I name mobile sitemap file in case I store this together with desktop "sitemap.xml" ? Thanks in advance.
Related
I want to read the data from multiple XML files based on the XSD document and the structure of one XML file will not be the same as to another but will follow the xsd schema. Also, the xmls are nested xmls.Can someone help me with it so that I can use the already created utility and add the attributes to it? I need a XML parser which parses each xml file(They are different) based on the xsd schema and returns me a list of the map so that the data can be matched with the DB data.
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class Test {
public static void main(String[] args) {
Customer customer = new Customer();
try {
File file = new File("SalesPoslog20200225104558676.xml");
JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
// output pretty printed
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(customer, file);
jaxbMarshaller.marshal(customer, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
public class Customer {
int organizationID;
int retailStoreID;
int workstationID;
int tillID;
int sequenceNumber;
public int organizationID() {
return organizationID;
}
#XmlElement
public void RetailStoreID(int retailStoreID) {
this.retailStoreID = retailStoreID;
}
public int workstationID() {
return workstationID;
}
#XmlAttribute
public void RetailStoreId(int RetailStoreId) {
this.retailStoreID = retailStoreID;
}
}
i fetched the defect url:
String defectUrl = conn.buildEntityCollectionUrl("defect");
defectUrl += "/98";
Map<String, String> requestHeaders = new HashMap<String, String>();
requestHeaders.put("Accept", "application/xml");
Response res = conn.httpGet(defectUrl, null, requestHeaders);
then converted into Entity:
String postedEntityReturnedXml = res.toString();
Entity entity = EntityMarshallingUtils.marshal(Entity.class,postedEntityReturnedXml);
than change the field value:
List<Field> fields = entity.getFields().getField();
for (Field f : fields) {
if (f.getName().equalsIgnoreCase("id"))
{
int i=f.hashCode();
System.out.println(i);
f.getValue().clear();
f.setName("");
}
}
now trying to convert it to xml formate again:
String xml = EntityMarshallingUtils.unmarshal(org.eclipse.jetty.server.Response.class,**fields**);
System.out.println(xml);
throwing error as object fields should not be there and not able to get what should i put in place of fields?
my EntityMarshallingUtils class is like:
package org.hp.qc.web.restapi.docexamples.docexamples.infrastructure;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.Marshaller;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
//import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class EntityMarshallingUtils {
private EntityMarshallingUtils() {}
#SuppressWarnings("unchecked")
public static <T> T marshal(Class<T> c, String xml) throws JAXBException {
T res;
if (c == xml.getClass()) {
res = (T) xml;
}
else {
JAXBContext ctx = JAXBContext.newInstance(c);
Unmarshaller marshaller = ctx.createUnmarshaller();
res = (T) marshaller.unmarshal(new StringReader(xml));
}
return res;
}
#SuppressWarnings("unchecked")
public static <T> String unmarshal(Class<T> c, Object o) throws Exception {
JAXBContext ctx = JAXBContext.newInstance(c);
Marshaller marshaller = ctx.createMarshaller();
StringWriter entityXml = new StringWriter();
marshaller.marshal(o, entityXml);
String entityString = entityXml.toString();
return entityString;
}
}
AnyOne who can help me on this?
I am new born for Jaxb and trying to do read/write operation in xml.
I am done with the write operation but having trouble with the read one.
I have the following xml-
<docOperations>
<SkuSlabs id="1">
<docId>677-WORK</docId>
<itemIds>11</itemIds>
<itemName>new item addedaaaaaa</itemName>
</SkuSlabs>
<SkuSlabs id="2">
<docId>699-WORK</docId>
<itemIds>21</itemIds>
<itemName>extra</itemName>
</SkuSlabs>
</docOperations>
Now i want to unmarshal the SkuSlabs object based on the condition supplied 'where id = 1', but don't know how to achieve that.
Please help.
Here you are (as you want add same access modifiers to attributes):
package pl.skuslab;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "SkuSlabs")
#XmlAccessorType(XmlAccessType.FIELD)
public class SkusLab {
#XmlAttribute
int id;
String docId;
int itemIds;
String itemName;
public SkusLab(int id, String docId, int itemIds, String itemName) {
super();
this.id = id;
this.docId = docId;
this.itemIds = itemIds;
this.itemName = itemName;
}
public SkusLab() {
super();
}
#Override
public String toString() {
return "SkusLab [id=" + id + ", docId=" + docId + ", itemIds=" + itemIds + ", itemName=" + itemName + "]";
}
}
Class DocOperation:
package pl.skuslab;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement
#XmlAccessorType(XmlAccessType.FIELD)
public class DocOperation {
#XmlElement(name = "SkuSlabs")
List<SkusLab> docOperations = new ArrayList<SkusLab>();
}
Class with the main function. Firstly I generate XML like you wrote in question, then I unmarshall xml to obejcts. First obejct is printed.
package pl.skuslab;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class SkusLabJaxb {
private static JAXBContext jc;
static {
try {
jc = JAXBContext.newInstance(DocOperation.class);
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
private static Marshaller getMarshaller() {
try {
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.FALSE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
return marshaller;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
private static Unmarshaller getUnmarshaller() {
try {
Unmarshaller unmarshaller = jc.createUnmarshaller();
return unmarshaller;
} catch (JAXBException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
String xml = "";
try {
DocOperation doper = new DocOperation();
doper.docOperations.add(new SkusLab(1, "677-WORK", 11, "new item addedaaaaaa"));
doper.docOperations.add(new SkusLab(2, "699-WORK", 21, "extra"));
StringWriter sw = new StringWriter();
getMarshaller().marshal(doper, sw);
xml = sw.toString();
System.out.println(xml);
} catch (JAXBException e) {
e.printStackTrace();
}
try {
DocOperation docOperation = (DocOperation) getUnmarshaller().unmarshal(
new StringReader(xml));
System.out.println(docOperation.docOperations.get(0).toString());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
I have java code and i'm trying to parse it and store it as XML file on my PC using JAXB but i have a marshaling exception:
Exception in thread "main" javax.xml.bind.MarshalException
- with linked exception:
[com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an #XmlRootElement annotation]
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:311)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.marshal(MarshallerImpl.java:236)
at javax.xml.bind.helpers.AbstractMarshallerImpl.marshal(AbstractMarshallerImpl.java:103)
at xml.ConfList.addToList(ConfList.java:29)
at xml.Tester.work(Tester.java:34)
at xml.Tester.main(Tester.java:16)
Caused by: com.sun.istack.internal.SAXException2: unable to marshal type "java.lang.String" as an element because it is missing an #XmlRootElement annotation
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.reportError(XMLSerializer.java:237)
at com.sun.xml.internal.bind.v2.runtime.LeafBeanInfoImpl.serializeRoot(LeafBeanInfoImpl.java:126)
at com.sun.xml.internal.bind.v2.runtime.XMLSerializer.childAsRoot(XMLSerializer.java:483)
at com.sun.xml.internal.bind.v2.runtime.MarshallerImpl.write(MarshallerImpl.java:308)
... 5 more
Java Result: 1
BUILD SUCCESSFUL (total time: 9 seconds)
The JAXB code i'm using :
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
public class ConfList {
private static final String fileName = "Source.xml";
List<String> xmlConfList;
private Object object;
public ConfList(Object object){
this.object = object;
}
public void addToList() throws IOException, JAXBException {
File file = new File(fileName);
JAXBContext jaxbContext = JAXBContext.newInstance(XmlConf.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(object, file); // i got the exception here
jaxbMarshaller.marshal(object, System.out);
}
}
The main class i use:
import java.io.IOException;
import java.util.ArrayList;
import javax.xml.bind.JAXBException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.xpath.XPathExpressionException;
import org.xml.sax.SAXException;
public class Tester{
public static void main (String [] args) throws XPathExpressionException, IOException, ParserConfigurationException, SAXException, TransformerException, JAXBException{
work();
}
public static void work () throws IOException, ParserConfigurationException, SAXException, XPathExpressionException, TransformerException, JAXBException{
String surl = "http://api.worldweatheronline.com/free/v1/weather.ashx?q=Cairo&format=xml&num_of_days=1&key=wd63kxr294rcgvbynhaf2z4r";
XmlSource xml = new XmlSource(surl);
xml.validate();
xml.load();
ArrayList<String> paths = xml.getAllPaths();
for(String path : paths){
System.out.println(path);
}
ConfList v = new ConfList(xml.getXml());
v.addToList();
}
}
The XmlConf Class :
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
#XmlRootElement(name = "XmlSource")
public class XmlConf {
private URL url;
private List<String> path = new ArrayList<String>();
private String urlp;
private Map<String, String> parameters;
private String host;
public URL getUrl() {
return url;
}
#XmlAttribute(name = "URL")
public void setUrl(URL url) {
this.url = url;
}
#XmlElement
public List<String> getPath() {
return path;
}
public void setPath(String path) {
this.path.add(path);
}
#XmlElement
public void setUrlPath(String urlp){
this.urlp = urlp;
}
public String getUrlPath(){
return urlp;
}
public void setParameters(Map<String, String> parameters){
this.parameters = parameters;
}
public Map<String, String> getParameters(){
return parameters;
}
public void setHostName(String host){
this.host = host;
}
public String getHostName(){
return host;
}
}
Note that all (XmlSource class) methods are run correctly.
Below is some sample code that unmarshals some XML into an instance of XmlConf and then marshals it back to XML.
Demo
import java.io.File;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(XmlConf.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
File xml = new File("src/forum16762200/input.xml");
XmlConf xmlConf = (XmlConf) unmarshaller.unmarshal(xml);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(xmlConf, System.out);
}
}
input.xml/Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<XmlSource URL="http://www.example.com">
<parameters>
<entry>
<key>A</key>
<value>a</value>
</entry>
<entry>
<key>B</key>
<value>b</value>
</entry>
</parameters>
<path>foo</path>
<urlPath>bar</urlPath>
</XmlSource>
From 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();
}
}
This is the output...
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)
I was wanting something like...
<?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>
I can't seem to find a way to get rid of that "item" element name.
From the resources of the answer, here is a way to do it with a crutch object.
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();
}
}
Output is:
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)
So now how can you do it without that crutch object?
The example from your link doesn't work - here are two links which discuss how you can get the results you're looking for:
http://old.nabble.com/XmlJavaTypeAdapter-help-td19127284.html
http://forums.java.net/jive/message.jspa?messageID=267376
Note that there is a minor problem with your main() method, I've highlighted the fix:
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));
}