如 <q>sasas<w>eqwe</w>ddas</q>
package com.people.xmlToSql; import java.io.File; import java.io.IOException; import java.io.StringWriter; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.jdom.Document; import org.jdom.input.SAXBuilder; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; /** * * @author : qinyi * date : 2019年8月14日,下午5:25:40 */ public class T2 { public static Document load(){ Document document=null; String url="E://QQ//batch2.xml"; try { SAXBuilder reader = new SAXBuilder(); document=reader.build(new File(url)); } catch (Exception e) { e.printStackTrace(); } return document; } public static String XmlToString(){ Document document=null; document=load(); Format format =Format.getPrettyFormat(); format.setEncoding("UTF-8");//设置编码格式 StringWriter out=null; //输出对象 String sReturn =""; //输出字符串 XMLOutputter outputter =new XMLOutputter(); out=new StringWriter(); try { outputter.output(document,out); } catch (IOException e) { e.printStackTrace(); } sReturn=out.toString(); return sReturn; } public static List<String> getFieldListByRegex(String xml, String label) { //正则表达式 String regex = "<" + label + ">(.*?)</" + label + ">"; Pattern pattern = Pattern.compile(regex); Matcher m = pattern.matcher(xml); //匹配的有多个 List<String> fieldList = new ArrayList<>(); while (m.find()) { if (!("".equals(m.group(1).trim()))) { fieldList.add(m.group(1).trim()); } } return fieldList; } public static void main(String[] args) { } }