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

时间:2021-06-22 23:32:45

报错的信息为: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包引入之后。问题就可以解决!