我使用 jsoup 成功地从 xml 内容中提取标签作为一个元素。
class Post {
private String a;
private String b;
public void setA (String a){
this.a = a;
}
public void setB(String b){
this.b =b;
}
public String getA(){
return a;
}
public String getB(){
return b;
}
}
通过使用数组列表
public ArrayList<Post> PostList = new ArrayList<Post>();
那么如何使用 jsoup 将 ab 的值存储在不同的索引下。我提取单个标签
Document doc = Jsoup.parse(soap_xml_content);
Element element1 = doc.select("a");
Element element2 = doc.select("b");
Element compte = doc.select("comptes");
String value_a = element1.text();
String value_b = element2.text();
Post currentPost = new Post();
对于每个compte
我想在 Post 类中设置元素 a 和 b 并使用方法 get 访问它。
for( Element element : compte ){
System.out.println(": "+element);
currentPost.setA(element);
currentPost.setB(element);
Poslist.add(currentPost);
currentPost = new Post();
}
我知道错了,但我不知道如何执行此步骤,请帮忙。
xml:
<soap:Envelope xmlns:soap="schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<liste>
<comptes>
<a>pen drive</a>
<b>jsoup</b>
</comptes>
<comptes>
<a>pen drive</a>
<b>jsoup</b>
</comptes>
<comptes>
<a>pen drive</a>
<b>jsoup</b>
</comptes>
</liste>
</soap:Body>
</soap:Envelope>
04-04 10:39:25.535: E/AndroidRuntime(2532): FATAL EXCEPTION: AsyncTask #1
04-04 10:39:25.535: E/AndroidRuntime(2532): java.lang.RuntimeException: An error occured while executing doInBackground()
04-04 10:39:25.535: E/AndroidRuntime(2532): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.lang.Thread.run(Thread.java:1019)
04-04 10:39:25.535: E/AndroidRuntime(2532): Caused by: java.lang.NullPointerException: null format argument
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.lang.String.format(String.java:2173)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.lang.String.format(String.java:2151)
04-04 10:39:25.535: E/AndroidRuntime(2532): at .Convertor$loadingTask.doInBackground(Convertor.java:201)
04-04 10:39:25.535: E/AndroidRuntime(2532): at .Convertor$loadingTask.doInBackground(Convertor.java:1)
04-04 10:39:25.535: E/AndroidRuntime(2532): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-04 10:39:25.535: E/AndroidRuntime(2532): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-04 10:39:25.535: E/AndroidRuntime(2532): ... 4 more