本文整理匯總了Java中javax.xml.bind.Marshaller.marshal方法的典型用法代碼示例。如果您正苦於以下問題:Java Marshaller.marshal方法的具體用法?Java Marshaller.marshal怎麽用?Java Marshaller.marshal使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.xml.bind.Marshaller
的用法示例。
在下文中一共展示了Marshaller.marshal方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: obj2xml
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
/**
* 對象轉為xml字符串
*
* @param obj
* @param isFormat
* true即按標簽自動換行,false即是一行的xml
* @param includeHead
* true則包含xm頭聲明信息,false則不包含
* @return
*/
public String obj2xml(Object obj, boolean isFormat, boolean includeHead) {
try (StringWriter writer = new StringWriter()) {
Marshaller m = MARSHALLERS.get(obj.getClass());
if (m == null) {
m = JAXBContext.newInstance(obj.getClass()).createMarshaller();
m.setProperty(Marshaller.JAXB_ENCODING, I18NConstants.DEFAULT_CHARSET);
}
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, isFormat);
m.setProperty(Marshaller.JAXB_FRAGMENT, !includeHead);// 是否省略xm頭聲明信息
m.marshal(obj, writer);
return writer.toString();
} catch (Exception e) {
throw new ZhhrException(e.getMessage(), e);
}
}
示例2: generateDetailedReportMultiSignatures
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
@Test
public void generateDetailedReportMultiSignatures() throws Exception {
JAXBContext context = JAXBContext.newInstance(DetailedReport.class.getPackage().getName());
Unmarshaller unmarshaller = context.createUnmarshaller();
Marshaller marshaller = context.createMarshaller();
DetailedReport detailedReport = (DetailedReport) unmarshaller.unmarshal(new File("src/test/resources/detailed-report-multi-signatures.xml"));
assertNotNull(detailedReport);
StringWriter writer = new StringWriter();
marshaller.marshal(detailedReport, writer);
String htmlDetailedReport = service.generateDetailedReport(writer.toString());
assertTrue(Utils.isStringNotEmpty(htmlDetailedReport));
logger.debug("Detailed report html : " + htmlDetailedReport);
}
示例3: toXml
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
public void toXml(final File to) {
try {
// TODO Moxy has to be used instead of default jaxb impl due to a bug
// default implementation has a bug that prevents from serializing xml in a string
JAXBContext jaxbContext = org.eclipse.persistence.jaxb.JAXBContextFactory.createContext(new Class[]{Config.class}, null);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(this, to);
} catch (final JAXBException e) {
throw new PersistException("Unable to persist configuration", e);
}
}
示例4: saveExamToFile
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
public static void saveExamToFile(File file,Exam exam){
try {
//prepare the marshaller
JAXBContext context = JAXBContext.newInstance(Exam.class);
Marshaller m = context.createMarshaller();
//Save the exam to a file and print it pretty on the console
m.marshal(exam,file);
//prettify here to save space in the file
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
System.out.println(exam);
m.marshal(exam,System.out);
MainApp.currentFilePath = file.getAbsolutePath();
}catch (Exception e){
e.printStackTrace();
}
}
示例5: readPayloadAsJAXB
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
@Override
public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
JAXBResult out = new JAXBResult(unmarshaller);
// since the bridge only produces fragments, we need to fire start/end document.
try {
out.getHandler().startDocument();
if (rawContext != null) {
Marshaller m = rawContext.createMarshaller();
m.setProperty("jaxb.fragment", Boolean.TRUE);
m.marshal(jaxbObject,out);
} else
bridge.marshal(jaxbObject,out);
out.getHandler().endDocument();
} catch (SAXException e) {
throw new JAXBException(e);
}
return (T)out.getResult();
}
示例6: testMarschallAcessToken
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
@Test @Ignore
public void testMarschallAcessToken() throws Exception {
JAXBContext jc = JAXBContext.newInstance(AccessToken.class);
// Create the Marshaller Object using the JaxB Context
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT,false);
AccessToken accessToken = new AccessToken();
accessToken.setAccessToken("eyJhbGciOiJSU");
accessToken.setExpiresIn(600);
accessToken.setExpiresIn(60);
accessToken.setRefreshToken("_dqAGxefbg0u58JAkz4nBkNE");
accessToken.setTokenType("token_type");
// Marshal the employee object to JSON and print the output to console
marshaller.marshal(accessToken, System.out);
}
示例7: testMarschallingMessage
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
@Test
public void testMarschallingMessage() throws Exception {
JAXBContext jc = JAXBContext.newInstance(Message.class);
// Create the Marshaller Object using the JaxB Context
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(MarshallerProperties.MEDIA_TYPE, "application/json");
marshaller.setProperty(MarshallerProperties.JSON_INCLUDE_ROOT,false);
Message message = new Message();
message.setMessage("success");
// Marshal the employee object to JSON and print the output to console
marshaller.marshal(message, System.out);
}
示例8: sniff
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
/**
* Obtains the tag name of the root element.
*/
private void sniff() {
RootElementSniffer sniffer = new RootElementSniffer(false);
try {
if (rawContext != null) {
Marshaller m = rawContext.createMarshaller();
m.setProperty("jaxb.fragment", Boolean.TRUE);
m.marshal(jaxbObject,sniffer);
} else
bridge.marshal(jaxbObject,sniffer,null);
} catch (JAXBException e) {
// if it's due to us aborting the processing after the first element,
// we can safely ignore this exception.
//
// if it's due to error in the object, the same error will be reported
// when the readHeader() method is used, so we don't have to report
// an error right now.
nsUri = sniffer.getNsUri();
localName = sniffer.getLocalName();
}
}
示例9: serializeIt
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
private String serializeIt(Distribution dist, Boolean format) throws Exception {
JAXBContext context = JAXBContext.newInstance(Distribution.class);
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, format);
StringWriter result = new StringWriter();
marshaller.marshal(dist, result);
return result.toString();
}
示例10: marshal
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
public void marshal(Marshaller m, Object object, XMLStreamWriter output) throws JAXBException {
m.setProperty(Marshaller.JAXB_FRAGMENT,true);
try {
m.marshal(object,output);
} finally {
m.setProperty(Marshaller.JAXB_FRAGMENT,false);
}
}
示例11: marshal
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
public void marshal(Marshaller m, Object object, Result result) throws JAXBException {
m.setProperty(Marshaller.JAXB_FRAGMENT,true);
try {
m.marshal(object,result);
} finally {
m.setProperty(Marshaller.JAXB_FRAGMENT,false);
}
}
示例12: saveDialogRoot
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
public static void saveDialogRoot(File dialogFile, Level root) throws JAXBException, SAXException
{
Marshaller marshaller = getJaxbContext().createMarshaller();
marshaller.setSchema(getSchema());
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(root, dialogFile);
}
示例13: getMarshalledAppInfo
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
static String getMarshalledAppInfo(ApplicationSubmissionContextInfo appInfo)
throws Exception {
StringWriter writer = new StringWriter();
JAXBContext context =
JAXBContext.newInstance(ApplicationSubmissionContextInfo.class);
Marshaller m = context.createMarshaller();
m.marshal(appInfo, writer);
return writer.toString();
}
示例14: generateDetailedReportMultiSignatures
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
@Test
public void generateDetailedReportMultiSignatures() throws Exception {
JAXBContext context = JAXBContext.newInstance(DetailedReport.class.getPackage().getName());
Unmarshaller unmarshaller = context.createUnmarshaller();
Marshaller marshaller = context.createMarshaller();
DetailedReport detailedReport = (DetailedReport) unmarshaller.unmarshal(new File("src/test/resources/detailed-report-multi-signatures.xml"));
assertNotNull(detailedReport);
StringWriter writer = new StringWriter();
marshaller.marshal(detailedReport, writer);
FileOutputStream fos = new FileOutputStream("target/detailedReportMulti.pdf");
service.generateDetailedReport(writer.toString(), fos);
}
示例15: marshal
import javax.xml.bind.Marshaller; //導入方法依賴的package包/類
/**
* Converts the given {@link Throwable} into an XML representation
* and put that as a DOM tree under the given node.
*/
public static void marshal( Throwable t, Node parent ) throws JAXBException {
Marshaller m = JAXB_CONTEXT.createMarshaller();
try {
m.setProperty("com.sun.xml.internal.bind.namespacePrefixMapper",nsp);
} catch (PropertyException pe) {}
m.marshal(new ExceptionBean(t), parent );
}