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) { } }
xml解析用正则解决没有标签的文本的解析不出异常的更多相关文章
- 用python解决打标签时将xml文件的标签名打错
用python解决打标签时将xml文件的标签名打错 问题描述:再进行达标签时将magnetic_tile的标签名错误的打成了magnetic_title,又不想一张一张的修改 出现问题的xml文件 & ...
- 【Thymeleaf】Thymeleaf模板对没有结束符的HTML5标签解析出错的解决办法
解决方案 spring: thymeleaf: mode: LEGACYHTML5 <dependency> <groupId>net.sourceforge.nekohtml ...
- boost.property_tree解析xml的帮助类以及中文解析问题的解决(转)
boost.property_tree可以用来解析xml和json文件,我主要用它来解析xml文件,它内部封装了号称最快的xml解析器rapid_xml,其解析效率还是很好的.但是在使用过程中却发现各 ...
- (原创)boost.property_tree解析xml的帮助类以及中文解析问题的解决
boost.property_tree可以用来解析xml和json文件,我主要用它来解析xml文件,它内部封装了号称最快的xml解析器rapid_xml,其解析效率还是很好的.但是在使用过程中却发现各 ...
- 爬虫的三种解析方式(正则解析, xpath解析, bs4解析)
一 : 正则解析 : 常用正则回顾: 单字符: . : 除换行符以外的所有字符 [] : [aoe] [a-w] 匹配集合中任意一个字符 \d : 数字 [0-9] \D : 非数字 \w : 非数字 ...
- 使用Jsoup帮助解析具有html标签的数据和解析网页
下载Jsoup包后导入 /** * Created by YGW on 2016/4/17. * 显示公告的详细内容 */public class PressFragment extends Frag ...
- 解决img标签间距问题
解决img标签间距问题 关于img标签间距问题:多个img之间有间距,包含img标签的div之间有间距. 代码如下: 1 <!doctype html> 2 <html lang=& ...
- Myeclipse解决dubbo标签不识别问题
Myeclipse解决dubbo标签不识别问题,引入dubbo.xsd文件,即可:
- Tomcat 在win7/win8 系统下tomcat-users.xml.new(拒绝访问)解决方法
tomcat启动报错No UserDatabase component found under key UserDatabase 也可以这样处理 Tomcat 在win7/win8 系统下tomcat ...
随机推荐
- 用jquery获取select标签中选中的option值及文本
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...
- 【报错】An error happened during template parsing (template: "class path resource [templates/hello1.html]")
页面显示: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing t ...
- JNDI配置笔记
先在tomcat Context.xml配置文件中配置 <Resource name="jdbc/elifecrm" type="javax.sql.DataSou ...
- CVE-2013-2094 porting to x86-32 分析
/* * linux 2.6.37-3.8.8 - x86 * @rikiji * * requires System.map and /dev/ptmx * this: http://zmbs.ne ...
- CodeChef Little Elephant and Balance
Given an array A1,A2...AN, you have to print the size of the largest contiguous subarray such that L ...
- K3 cloud选单时候必须把必录的数据录完以后才可以选单
解决办法:在bos中把选单按钮的提交时候校验打勾
- ltp-ddt realtime_cpu_load timeout
# @name Worstcase Latency with cpu load by using cyclictest# @desc Measure latency under cpu load by ...
- 【串线篇】加谈数据库之连接join
主题:内连接.左连接(左外连接).右连接(右外连接) 建表语句: CREATE TABLE `a_table` ( `a_id` int(11) DEFAULT NULL, `a_name` va ...
- Java中的heap和stack
heap和stack Java的内存分为两类,一类是栈内存,一类是堆内存. 栈内存:是指程序进入一个方法时,会为这个方法单独分配一块私属存储空间,用于存储这个方法内部的局部变量,当这个方法结束时,分配 ...
- 【NLP新闻-2013.06.16】Representative Reviewing
英语原文地址:http://nlp.hivefire.com/articles/share/40221/ 注:本人翻译NLP新闻只为学习专业英语和扩展视野,如果翻译的不好,请谅解! (实在是读不大懂, ...