Skip to content

Instantly share code, notes, and snippets.

@miao1007
Created February 10, 2017 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miao1007/1c68764776b85f7f91b2ec116ad11546 to your computer and use it in GitHub Desktop.
Save miao1007/1c68764776b85f7f91b2ec116ad11546 to your computer and use it in GitHub Desktop.
package com.xxxx.xxxx.xxx.utils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
/**
* Created by leon on 10/02/2017.
* jsoup 2 xml utils
*/
public class XMLUtils {
private static final boolean isDebug = true;
public static Document string2Doc(String s) {
try {
Document doc = Jsoup.parse(s, "utf8");
if (isDebug) {
pwd(doc);
}
return doc;
} catch (Exception e) {
if (isDebug) {
System.out.println("file2Elements failed, e = " + e);
}
return null;
}
}
public static Document file2Doc(File file) {
try {
Document doc = Jsoup.parse(file, "utf8");
if (isDebug) {
pwd(doc);
}
return doc;
} catch (Exception e) {
if (isDebug) {
System.out.println("file2Elements failed, e = " + e);
}
return null;
}
}
/**
* tag: get `<dependency attr="233"></dependency>` => `dependencies dependency`
* <p>
* html()(Str) / getAllElements(AST): <groupId>junit</groupId>
* <artifactId>junit</artifactId>
* <version>4.11</version>
* <type>jar</type>
* <scope>test</scope>
* attr(): "233"
* <p>
* Elements links = doc.select("a[href]"); // a with href
* Elements pngs = doc.select("img[src$=.png]");
* // img with src ending .png
* <p>
* Element masthead = doc.select("div.masthead").first();
* // div with class=masthead
* <p>
* see <a href>https://jsoup.org/cookbook/extracting-data/selector-syntax</a>
*
* @param element
* @param css
* @return
*/
public static Elements select(Element element, String css) {
return element.select(css);
}
public static String firstText(Element element, String css) {
return element.select(css).first().text();
}
public static void pwd(Element element) {
if (isDebug) {
System.out.println(element.tag() + " is at = " + element.cssSelector());
}
}
/**
* Last level xml 2 map
*
* @param element
* @return
*/
public static Map<String, String> element2map(Element element) {
Map<String, String> map = new TreeMap<String, String>();
Elements childs = element.children();
for (int i = 0; i < childs.size(); i++) {
Element e = childs.get(i);
String key = e.tagName();
String val = e.text();
map.put(key, val);
}
return map;
}
public static List<Element> asList(Elements elements) {
List<Element> list = new ArrayList<Element>();
for (int i = 0; i < elements.size(); i++) {
Element e = elements.get(i);
list.add(e);
}
return list;
}
public static void main(String[] args) throws Exception {
File file = new File("pom.xml");
Document doc = file2Doc(file);
Elements plugins = doc.select("project build plugins plugin");
for (Element plugin : plugins) {
System.out.println("element = " + element2map(plugin));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment