zoukankan      html  css  js  c++  java
  • xml解析用正则解决没有标签的文本的解析不出异常

    如  <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) {
    		 
    	}
    
    }
    

      

  • 相关阅读:
    报表设计器的使用之一:入门
    统计图开发之二:点图元
    统计图开发之一:画法定义
    集算器之五:序表
    集算器之四:程序流程
    忏悔录
    请不要离我而去
    所想和所做 所梦和所成
    做出改变,不断改变。
    Linux 操作命令
  • 原文地址:https://www.cnblogs.com/qinyios/p/11377128.html
Copyright © 2011-2022 走看看