无法在Android上运行XML解析器Jackson
import android.content.Context;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.List;
in onCreate(){
ObjectMapper xmlMapper = new XmlMapper();
Channel root = xmlMapper.readValue(stringXML, Channel.class);
}
@JacksonXmlRootElement(localName = "channel")
public static class Channel {
public List<Item> channel;
}
public static class Item {
@JacksonXmlProperty(localName = "item")
public String item;
}
错误是:
java.lang.NoClassDefFoundError: Failed resolution of: Ljavax/xml/stream/XMLInputFactory;
Caused by: java.lang.ClassNotFoundException: Didn't find class "javax.xml.stream.XMLInputFactory" on path: DexPathList
答案 0 :(得分:7)
老问题但是......
Android没有javax.xml.stream
包,这是大多数涉及XML的库所必需的。
要自己添加,请将此依赖项添加到build.gradle文件中:
compile group: 'javax.xml.stream', name: 'stax-api', version: '1.0-2'
这是撰写此评论时的最新版本。您可以查看here以获取更新版本。
答案 1 :(得分:0)
您没有正确设置Jackson for Android。请看这页Using jackson-dataformat-xml on android。有很好的描述如何做到。