Jsoup

作者: Mr_码客 | 来源:发表于2017-06-06 11:27 被阅读0次

    1.解析attribute中值

    package com.xxx.parser.qy;
    
    import org.jsoup.Jsoup;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by user on 2017/6/6.
     */
    public class sheet {
        public static void main(String args[]) {
            String str = "<Root>\n" +
                    "<Item serviceID=\"16\" serviceName=\"住家保姆\" />\n" +
                    "<Item serviceID=\"17\" serviceName=\"不住家保姆\" />\n" +
                    "</Root>";
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
            org.jsoup.nodes.Document doc = Jsoup.parse(str);
            org.jsoup.select.Elements links = doc.getElementsByTag("Item");
            for(int i=0;i<links.size();i++){
                Map<String,Object> map = new HashMap<String, Object>();
                map.put("serviceid", links.get(i).attr("serviceid"));
                map.put("serviceName", links.get(i).attr("serviceName"));
                resultList.add(map);
            }
        }
    }
    

    2.解析节点值

    package com.xxx.parser.qy;
    
    import org.jsoup.Jsoup;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * Created by user on 2017/6/6.
     */
    public class sheet {
        public static void main(String args[]) {
        String str = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" +
                "<data>\n" +
                "<ITEM>\n" +
                "<ID>201203210001</ID>\n" +
                "<TITLE>xxx</TITLE>\n" +
                "<RELEASE_DATE>20110824</RELEASE_DATE>\n" +
                "<SOURCE>xxx</SOURCE>\n" +
                "<CONTENT>xxxxxx</CONTENT>\n" +
                "</ITEM>\n" +
                "\n" +
                "<ITEM>\n" +
                "<ID>201203210002</ID>\n" +
                "<TITLE>xxx</TITLE>\n" +
                "<RELEASE_DATE>20110824</RELEASE_DATE>\n" +
                "<SOURCE>xxx</SOURCE>\n" +
                "<CONTENT>xxxxxx</CONTENT>\n" +
                "</ITEM>\n" +
                "</data>";
    
            List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
            org.jsoup.nodes.Document doc = Jsoup.parse(str);
            org.jsoup.select.Elements links_id = doc.getElementsByTag("ID");
            org.jsoup.select.Elements links_title = doc.getElementsByTag("TITLE");
            org.jsoup.select.Elements links_date = doc.getElementsByTag("RELEASE_DATE");
            org.jsoup.select.Elements links_source = doc.getElementsByTag("SOURCE");
            org.jsoup.select.Elements links_content = doc.getElementsByTag("CONTENT");
            for(int i = 0;i<links_id.size();i++){
                Map<String, Object> resultMap = new HashMap<String, Object>();
                resultMap.put("id", links_id.get(i).text());
                resultMap.put("title", links_title.get(i).text());
                resultMap.put("relDate", links_date.get(i).text());
                resultMap.put("source", links_source.get(i).text());
                resultMap.put("content", links_content.get(i).text());
                resultList.add(resultMap);
            }
        }
    }
    

    3.解析html

    1>根据"td"或者"tr"来解析
         org.jsoup.nodes.Document doc = Jsoup.parse(resHtml);
         org.jsoup.select.Elements links = doc.getElementsByTag("td");
         循环获取td中的值:
         links.get(i).text();
    
    2>根据"class"参数来解析,如 <div class=".jtpsoft">...
         org.jsoup.nodes.Document doc = Jsoup.parse(resHtml);
         org.jsoup.select.Elements links = doc.select(".jtpsoft");
         然后根据links里面的 td或者tr标签来解析
    
    3>根据 "name"参数来解析
        org.jsoup.nodes.Document doc = Jsoup.parse(resHtml);
        org.jsoup.select.Elements _links1 = doc.select("[name=zgzh]");
        org.jsoup.select.Elements _links2= doc.select("[name=sfzh]");
        如有多个name参数相同的值,则循环输出 _links1.get(i).val();
        取第一个值也可以使用_links1.first().val();
    

    4.节点选择器

    body > *
    

    相关文章

      网友评论

          本文标题:Jsoup

          本文链接:https://www.haomeiwen.com/subject/uwegfxtx.html