下面列出了怎么用javax.xml.bind.JAXBIntrospector的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
public <T> String writeXml(final T model, final Schema schema, final ValidationEventHandler handler) {
if (model == null) {
throw new ConversionExeption("Can not serialize null");
}
try ( final StringWriter w = new StringWriter() ) {
final JAXBIntrospector introspector = getJaxbContext().createJAXBIntrospector();
final Marshaller marshaller = getJaxbContext().createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setSchema(schema);
marshaller.setEventHandler(handler);
final XMLOutputFactory xof = XMLOutputFactory.newFactory();
final XMLStreamWriter xmlStreamWriter = xof.createXMLStreamWriter(w);
if (null == introspector.getElementName(model)) {
final JAXBElement jaxbElement = new JAXBElement(createQName(model), model.getClass(), model);
marshaller.marshal(jaxbElement, xmlStreamWriter);
} else {
marshaller.marshal(model, xmlStreamWriter);
}
xmlStreamWriter.flush();
return w.toString();
} catch (final JAXBException | IOException | XMLStreamException e) {
throw new ConversionExeption(String.format("Error serializing Object %s", model.getClass().getName()), e);
}
}
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
@Override
public JAXBIntrospector createJAXBIntrospector() {
return new JAXBIntrospector() {
public boolean isElement(Object object) {
return getElementName(object)!=null;
}
public QName getElementName(Object jaxbElement) {
try {
return JAXBContextImpl.this.getElementName(jaxbElement);
} catch (JAXBException e) {
return null;
}
}
};
}
private QName discoverQNameFromJaxb(Class<?> classType) {
QName qName = null;
try {
final JAXBIntrospector jaxbIntrospector = JAXBContext.newInstance(classType).createJAXBIntrospector();
qName = jaxbIntrospector.getElementName(classType.getConstructor().newInstance());
} catch (Exception e) {
// Add e to the logger message because JAXB Exceptions has a lot of information in the toString().
// and some loggers implementations just print the getMessage();
logger.warn("Cannot discover QName from JAXB annotations for class: " + classType.getName()
+ ". Preparing generic QName." + e, e);
}
if (qName == null) {
// Could be null if getElementName returned null, or a exception was thrown.
return EMPTY_Q_NAME;
}
return qName;
}