微信开发,对象转换为xml时候引用XStream这个类报错处理方案

时间:2015-07-13 16:19:03   收藏:0   阅读:2213

报错的信息为:The type org.xmlpull.v1.XmlPullParser cannot be resolved.





/**
 * 扩展XStream 支持CDATA
 */
private static XStream xstream = new XStream(new XppDriver(){
public HierarchicalStreamWriter createWriter(Writer out) {
return new PrettyPrintWriter(out){
//对所有XML节点的转换添加CDATA标志
boolean cdata =true;
@SuppressWarnings("unchecked")
public void starNode(String name,Class clazz) {
super.startNode(name,clazz);

}
protected void  writeText(QuickWriter writer,String text) {
if(cdata){
writer.write("<![CDATA[");
writer.write(text);
writer.write("]]>");
}else {
writer.write(text);
}

}
};
}
}); 
/**
 * 文本消息对象转换为xml
 * @param textMessage 文本消息对象
 * @return
 */
public static String messageToXml(TextMessage textMessage) {
xstream.alias("xml", textMessage.getClass());
return xstream.toXML(textMessage);
}
/**
 * 图片消息对象转换为xml
 * @param imageMessage 图片消息对象
 * @return
 */
public static String messageToXml(ImageMessage imageMessage) {
xstream.alias("xml", imageMessage.getClass());
return xstream.toXML(imageMessage);
}


/**
 * 语音消息对象转换为xml
 * @param voiceMessage 语音消息对象
 * @return
 */
public static String messageToXml(VoiceMessage voiceMessage) {
xstream.alias("xml", voiceMessage.getClass());
return xstream.toXML(voiceMessage);
}
/**
 * 视频消息对象转换为xml
 * @param videoMessage 视频消息对象
 * @return
 */
public static String messageToXml(VideoMessage videoMessage) {
xstream.alias("xml", videoMessage.getClass());
return xstream.toXML(videoMessage);
}
/**
 * 音乐消息对象转换为xml
 * @param musicMessage 音乐消息对象
 * @return
 */
public static String messageToXml(MusicMessage musicMessage) {
xstream.alias("xml", musicMessage.getClass());
return xstream.toXML(musicMessage);
}
/**
 * 图文消息对象转换为xml
 * @param newsMessage 图文消息对象
 * @return
 */
public static String messageToXml(NewsMessage newsMessage) {
xstream.alias("xml", newsMessage.getClass());
xstream.alias("item", new Article().getClass());
return xstream.toXML(newsMessage);
}
}





处理的方法就是

在当前类的第一行(package位置)会提示:Multiple markers at this line

- The type org.xmlpull.v1.XmlPullParser cannot be resolved. It is indirectly referenced from required .class files
- The type org.xmlpull.v1.XmlPullParser cannot be resolved. It is indirectly referenced from required .class files
- The type org.xmlpull.v1.XmlPullParserException cannot be resolved. It is indirectly referenced from 
required .class files

这样的信息,这是因为XStream只是一个jar文件,但是它里面会依赖一个jar包,依赖的jar包是:xmlpull_1_0_5.jar、只要把这个jar包引入之后,问题即可解决!

版权声明:本文为博主原创文章,未经博主允许不得转载。

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!