运行 XML 无法在 Android 上解析 Jackson
Can't run XML parser Jackson on Android
无法 运行 XML Android
上的解析器 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
您没有为 Android 正确设置 Jackson。查看此页面 Using jackson-dataformat-xml on android。有很好的描述如何去做。
老问题但是...
Android 没有 javax.xml.stream
包,而大多数涉及 XML 的库都需要它。
要自己添加,请将此依赖项添加到您的 build.gradle 文件中:
compile group: 'javax.xml.stream', name: 'stax-api', version: '1.0-2'
这是撰写此评论时的最新版本。您可以检查 here 以获取更新版本。
无法 运行 XML Android
上的解析器 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
您没有为 Android 正确设置 Jackson。查看此页面 Using jackson-dataformat-xml on android。有很好的描述如何去做。
老问题但是...
Android 没有 javax.xml.stream
包,而大多数涉及 XML 的库都需要它。
要自己添加,请将此依赖项添加到您的 build.gradle 文件中:
compile group: 'javax.xml.stream', name: 'stax-api', version: '1.0-2'
这是撰写此评论时的最新版本。您可以检查 here 以获取更新版本。