创建java类JaxB问题,怎么java 解决乱码问题

博客分类:
package com.apusic.esb.util.
import java.io.ByteArrayInputS
import java.io.F
import java.io.InputS
import java.io.StringW
import java.io.UnsupportedEncodingE
import java.util.HashM
import java.util.M
import java.util.concurrent.ConcurrentHashM
import javax.xml.bind.JAXBC
import javax.xml.bind.JAXBE
import javax.xml.bind.M
import javax.xml.bind.U
import org.eclipse.core.resources.IF
import org.eclipse.core.runtime.CoreE
import org.eclipse.core.runtime.NullProgressM
import com.apusic.esb.util.UtilP
* JAXB工具,通过使用注解,实现java对象与XML文件的相互转换
* @author TigerlChen
public class JaxbUtil {
private static ConcurrentHashMap&Class&?&, JAXBContext& jaxbContextMap = new ConcurrentHashMap&Class&?&,JAXBContext&();
* 将JAXB实现对象转换成XML格式的字符串
* @param &T& 这里的类是具体类,不能是接口
* @param tclz 转换对象实例
public static &T& String marshToXmlBinding(Class&T& tclz,T t) throws JAXBException {
JAXBContext jc =
if(jaxbContextMap.get(tclz)==null) {
Map&String, String& properties = new HashMap&String, String&();
jc = JAXBContext.newInstance(new Class&?&[]{tclz},properties);
jaxbContextMap.put(tclz, jc);
jc = jaxbContextMap.get(tclz);
Marshaller u = jc.createMarshaller();
// XML内容格式化
u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
StringWriter
StringWriter ();
u.marshal(t, sw);
return sw.toString();
* 将XML格式的字符串转换成JAXB实现对象
* @param &T& 这里的类是具体类,不能是接口
* @param tclz
* @param file 当目录存在文件不存在时,自动创建;当目录不存在时,将抛异常
public static &T& void marshToXmlBinding(Class&T& tclz, T t, File file) throws JAXBException {
if (tclz == null || file == null) {
JAXBContext jc =
if (jaxbContextMap.get(tclz) == null) {
Map&String, String& properties = new HashMap&String, String&();
jc = JAXBContext.newInstance(new Class&?&[] {tclz}, properties);
jaxbContextMap.put(tclz, jc);
jc = jaxbContextMap.get(tclz);
Marshaller u = jc.createMarshaller();
u.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
u.marshal(t, file);
public static &T& void marshToXmlBinding(Class&T& tclz, T t, IFile file) throws JAXBException {
String str = marshToXmlBinding(tclz, t);
ByteArrayInputStream stream =
stream = new ByteArrayInputStream(str.getBytes("UTF8"));
if (!file.exists()) {
file.create(stream, true, new NullProgressMonitor());
file.setContents(stream, true, false, new NullProgressMonitor());
} catch (CoreException e) {
UtilPlugin.log(e);
} catch (UnsupportedEncodingException e) {
UtilPlugin.log(e);
* 将XML格式的字符串转换成JAXB实现对象
* @param &T& 这里的类是具体类,不能是接口
* @param tclz
* @param xmlstr
@SuppressWarnings("unchecked")
public static &T& T unmarshToObjBinding(Class&T& tclz,String xmlstr) {
JAXBContext jc =
if(jaxbContextMap.get(tclz)==null) {
Map&String, String& properties = new HashMap&String, String&();
jc = JAXBContext.newInstance(new Class&?&[]{tclz},properties);
jaxbContextMap.put(tclz, jc);
jc = jaxbContextMap.get(tclz);
Unmarshaller un = jc.createUnmarshaller();
return (T) un.unmarshal(new ByteArrayInputStream(xmlstr.getBytes("utf-8")));
} catch (JAXBException e) {
UtilPlugin.log(e);
} catch (UnsupportedEncodingException e) {
UtilPlugin.log(e);
* 将XML格式的文件转换成JAXB实现对象
* @param &T&
* @param tclz
* @param file
@SuppressWarnings("unchecked")
public static &T& T unmarshToObjBinding(Class&T& tclz,File file) {
if (tclz == null || file == null || !file.exists()) {
JAXBContext jc =
if(jaxbContextMap.get(tclz)==null) {
Map&String, String& properties = new HashMap&String, String&();
jc = JAXBContext.newInstance(new Class&?&[]{tclz},properties);
jaxbContextMap.put(tclz, jc);
jc = jaxbContextMap.get(tclz);
Unmarshaller un = jc.createUnmarshaller();
return (T) un.unmarshal(file);
} catch (JAXBException e) {
UtilPlugin.log(e);
@SuppressWarnings("unchecked")
public static &T& T unmarshToObjBinding(Class&T& tclz,InputStream inputStream) {
if (tclz == null || inputStream == null) {
JAXBContext jc =
if(jaxbContextMap.get(tclz)==null) {
Map&String, String& properties = new HashMap&String, String&();
jc = JAXBContext.newInstance(new Class&?&[]{tclz},properties);
jaxbContextMap.put(tclz, jc);
jc = jaxbContextMap.get(tclz);
Unmarshaller un = jc.createUnmarshaller();
return (T) un.unmarshal(inputStream);
} catch (JAXBException e) {
UtilPlugin.log(e);
public static &T& T unmarshToObjBinding(Class&T& tclz, IFile file) {
return unmarshToObjBinding(tclz, file.getLocation().toFile());
tigerlchen
浏览: 122367 次
来自: 深圳
WatchKey都没有reset,第二次loop妥妥的捕获不到 ...
复杂对象就需要使用framedecorator了
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'博客分类:
转自 /topic/1118082
之前写了如何从xsd快捷生成java代码
不过写xsd也是个力气活啊,今天看到一篇blog,有工具直接生成,方便多了
首先我们需要使用XML Schema来描述XML格式,怎样自动生成xsd,我们可以通过trang.jar这个包来生成
java -jar trang.jar a.xml a.xsd
后面就按照下面的步骤来就好~
------------------------------------------------------
1 首先是配置好java的环境变量,将java安装目录下bin文件夹下的xjc.exe配到classpath下
2 进入到你的xsd的文件夹下,在cmd模式下执行
xjc -p com.ebupt a.xsd -d abc
xjc -p 包的路径 xsd的名字.xsd -d 目标的文件夹
包的路径跟命名空间有关,如果不匹配,可能会失败,自己根据提示错误具体情况具体解决吧。
浏览: 90807 次
来自: 北京
Spring的啊
赶紧搞啊。。。
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'博客分类:
JAXB是将java实体类序列化和反序列化处理中比较方便的一个工具,在jaxb的使用中,若是实体类中存在继承的层次关系,那么就需要一些值得注意的地方了。
比如在开发百度轻应用时候就需要处理到:
&?xml version="1.0" encoding="UTF-8"?&
&loc&/&/loc&
&lastmod&&/lastmod&
&changefreq&always&/changefreq&
&priority&1.0&/priority&
&!-- display 标签内的字段为微站在标准 sitemap 协议基础上的扩展,
描述微站所需的标签内容
&/display&
可以看到此xml中,display节点下是根据各个模板的不同,而不同的,那么在java的开发实体构建中,我们是可以定义几个子类的,然后让它们来继承这个display类。则所有的模板格式就可以通过JAXB来生成了。
简化的格式如下:
public class A {
public class B {}
public class C {}
public class D {}
public class E extends A {
public class F extends A {
}
这个方案看着比较直观也符合一般逻辑思想,但是问题在于实践中只能生成父类部分,结果如下:
这并不是我们所需要的,所以需要申明一些标记来达到我们所需要的结果:
改造如下:
public class A {
@XmlElements({
@XmlElement(name = "c", type=C.class),
@XmlElement(name = "d", type=D.class)
public class B {}
public class E {}
public class C extends E {}
public class D extends E {}
这样的结构,最后就能正确生成了。
ps:类结构可以使用这个工具生成,(我本是c#程序员,因为工作上的需要,所以在独自的学习java)
那么在实际的开发过程中,序列化成xml可能会碰到很多特殊的一些问题,但是自己去看并理解那些报错信息,对于我们问题的解决是有帮助的,灵活运用上面的那些xml标记,那么才能体现到JAXB的好用。
&完&
浏览: 4589 次
来自: 北京
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'&>&JAXB乱码问题解决
JAXB乱码问题解决
上传大小:32KB
解释并解决java开发中使用jaxb解析或组装xml遇到乱码的问题。
综合评分:4.3(13位用户评分)
所需积分/C币:
下载个数:127
{%username%}回复{%com_username%}{%time%}\
/*点击出现回复框*/
$(".respond_btn").on("click", function (e) {
$(this).parents(".rightLi").children(".respond_box").show();
e.stopPropagation();
$(".cancel_res").on("click", function (e) {
$(this).parents(".res_b").siblings(".res_area").val("");
$(this).parents(".respond_box").hide();
e.stopPropagation();
/*删除评论*/
$(".del_comment_c").on("click", function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_invalid/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parents(".conLi").remove();
alert(data.msg);
$(".res_btn").click(function (e) {
var q = $("#form1").serializeArray();
console.log(q);
var res_area_r = $.trim($(".res_area_r").val());
if (res_area_r == '') {
$(".res_text").css({color: "red"});
$.post("/index.php/comment/do_comment_reply/", q,
function (data) {
if (data.succ == 1) {
var $target,
evt = e || window.
$target = $(evt.target || evt.srcElement);
var $dd = $target.parents('dd');
var $wrapReply = $dd.find('.respond_box');
console.log($wrapReply);
var mess = $(".res_area_r").val();
var str = str.replace(/{%header%}/g, data.header)
.replace(/{%href%}/g, 'http://' + window.location.host + '/user/' + data.username)
.replace(/{%username%}/g, data.username)
.replace(/{%com_username%}/g, _username)
.replace(/{%time%}/g, data.time)
.replace(/{%id%}/g, data.id)
.replace(/{%mess%}/g, mess);
$dd.after(str);
$(".respond_box").hide();
$(".res_area_r").val("");
$(".res_area").val("");
$wrapReply.hide();
alert(data.msg);
}, "json");
/*删除回复*/
$(".rightLi").on("click",'.del_comment_r', function (e) {
var id = $(e.target).attr("id");
$.getJSON('/index.php/comment/do_comment_del/' + id,
function (data) {
if (data.succ == 1) {
$(e.target).parent().parent().parent().parent().parent().remove();
$(e.target).parents('.res_list').remove()
alert(data.msg);
//填充回复
function KeyP(v) {
$(".res_area_r").val($.trim($(".res_area").val()));
评论共有7条
试了一下,好像是解决了乱码
谢谢楼主的分享,资源还是不错的
看了下你写的demo,虽然我遇到的问题跟你不一样,但还是谢谢。jaxb我感觉有个缺陷就是对xml的格式要求太苛刻了
虽然没有解决我的问题,但还是感谢!
谢谢分享心得
谢谢楼主的分享,资源还是不错的
谢谢分享了,看了下你写的demo,虽然我遇到的问题跟你不一样,但还是谢谢。jaxb我感觉有个缺陷就是对xml的格式要求太苛刻了
审核通过送C币
java自主学习知识总结
创建者:qq_
iText 7 相关jar包
创建者:ealu1234
39本架构类书籍
创建者:jsntghf
上传者其他资源上传者专辑
java_double_精度
Dell灵越Inspiron_M4040、N4050官方拆机图解
开发技术热门标签
VIP会员动态
下载频道积分规则调整V1710.18
CSDN下载频道积分调整公告V1710.17
开通VIP,海量IT资源任性下载
spring mvc+mybatis+mysql+maven+bootstrap 整合实现增删查改简单实例.zip
CSDN&VIP年卡&4000万程序员的必选
为了良好体验,不建议使用迅雷下载
JAXB乱码问题解决
会员到期时间:
剩余下载个数:
剩余C币:593
剩余积分:786
为了良好体验,不建议使用迅雷下载
积分不足!
资源所需积分/C币
当前拥有积分
您可以选择
程序员的必选
绿色安全资源
资源所需积分/C币
当前拥有积分
当前拥有C币
(仅够下载10个资源)
全站1200个资源免积分下载
为了良好体验,不建议使用迅雷下载
资源所需积分/C币
当前拥有积分
当前拥有C币
全站1200个资源免积分下载
资源所需积分/C币
当前拥有积分
当前拥有C币
您的积分不足,将扣除 10 C币
全站1200个资源免积分下载
为了良好体验,不建议使用迅雷下载
你当前的下载分为234。
你还不是VIP会员
开通VIP会员权限,免积分下载
你下载资源过于频繁,请输入验证码
你下载资源过于频繁,请输入验证码
您因违反CSDN下载频道规则而被锁定帐户,如有疑问,请联络:!
若举报审核通过,可奖励20下载分
被举报人:
zong_hc_001
举报的资源分:
请选择类型
资源无法下载
资源无法使用
标题与实际内容不符
含有危害国家安全内容
含有反动色情等内容
含广告内容
版权问题,侵犯个人或公司的版权
*详细原因:
JAXB乱码问题解决

我要回帖

更多关于 java解决线程阻塞问题 的文章

 

随机推荐