jsoup -- xml文档解析

jsoup -- xml文档解析  

修改

https://jsoup.org/cookbook/modifying-data/set-attributes

https://jsoup.org/cookbook/modifying-data/set-html

https://jsoup.org/cookbook/modifying-data/set-text

<?xml version="1.0" encoding="UTF-8"?>
<message>
  <username>zsjdxc251</username>
  <list>
    <value>javascript</value>
    <value>xml</value>
    <value>jquery</value>
    <value>java</value>
  </list>
</message>
package org.util;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
 

 /**
 * @author zhengshijun
 * @date 2014-6-11 下午2:39:52
 */
public class XmlUtils {
  private static Document document;

  static{
    URL url = XmlUtils.class.getClassLoader().getResource("message.xml");
    File file = new File(url.getFile());
    try {
      document = Jsoup.parse(file, "UTF-8");
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }
    public static String userName(){
    	return document.select("username").text();
    }
    public static List<String> value(){
    	List<String> list = new ArrayList<String>();
    	Elements listrens = document.select("list").select("value");
    	for(Element listren:listrens){
      String text=listren.text();
      list.add(text);
    }
    	return list;
    }
   
}


posted @ 2016-08-17 03:28  donaldlee  阅读(1901)  评论(0编辑  收藏  举报