@Test
public void testContent() throws Exception
{
Content content = new Content();
content.setJAXBObject(new CustomerAtom("bill"));
JAXBContext ctx = JAXBContext.newInstance(Content.class, CustomerAtom.class);
Marshaller marshaller = ctx.createMarshaller();
marshaller.setProperty("com.sun.xml.bind.indentString", " ");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter writer = new StringWriter();
NamespacePrefixMapper mapper = new NamespacePrefixMapper()
{
public String getPreferredPrefix(String namespace, String s1, boolean b)
{
if (namespace.equals("http://www.w3.org/2005/Atom"))
{
return "atom";
}
else return s1;
}
};
ClassResolver resolver = new ClassResolver()
{
@Nullable
public Class<?> resolveElementName(@NotNull String ns, @NotNull String location) throws Exception
{
System.out.println("Resolve: " + ns + " " + location);
return null;
}
};
//marshaller.setProperty(ClassResolver.class.getName(), resolver);
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper", mapper);
marshaller.marshal(content, writer);
marshaller.marshal(content, System.out);
System.out.println("**********");
System.out.println(writer.toString());
content = (Content) ctx.createUnmarshaller().unmarshal(new StringReader(writer.toString()));
//JAXBElement<Customer> cust = ctx.createUnmarshaller().unmarshal(node, Customer.class);
//System.out.println(cust.getValue().getName());
CustomerAtom cust = content.getJAXBObject(CustomerAtom.class);
System.out.println(cust.getName());
}