xml实体转换为xml文件

private String obj2xml(Object obj) {
		try {
			JAXBContext context = JAXBContext.newInstance(CFX.class);// CXF是xml主实体对象
			Marshaller marshaller = context.createMarshaller();
			// 设置编码格式
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "GBK");
			// 去掉生成的默认报文头
			marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
			StringWriter writer = new StringWriter();
			writer.append(SFIConstantEnum.SFI_XML_ATTR); // SFI_XML_ATTR = "<?xml version=\"1.0\" encoding=\"GBK\"?>"
			marshaller.marshal(obj, writer);
			return writer.toString();
		} catch (Exception e) {
			Logger.error(this, SFICommonUtils.getShowLineMessage("实体转换xml异常"), e);
			throw new SFIServiceException("实体转换xml异常");
		}
	}

  

猜你喜欢

转载自www.cnblogs.com/ren-kai/p/11392433.html