我使用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;
}
}
使用arraylist
public ArrayList<Post> PostList = new ArrayList<Post>();
那么如何使用jsoup将b的值存储在不同的索引下呢?我提取单个标签
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
答案 0 :(得分:0)
在Post类公共帖子(A,B)中创建一个构造函数
public Post(String a, String b){
this.a =a;
this.b =b;
}
和
for( Element element : compte ){
System.out.println(": "+element);
Poslist.add(new Post(ElementA,ElementB));
}
您不会将Setters与上述解决方案一起使用。您只是在Post类型的ArrayList中添加类的对象。要使用get方法,您将这样做。
String A = Poslist.get(YOUR_POSTION).getA();
String B = Poslist.get(YOUR_POSTION).getB();