前端之家收集整理的这篇文章主要介绍了
不使用第三方开源包 解析xml(正则解析),
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
package cn.sun.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*@description:解析xml
* @author:sunla
* @date:2013-8-19上午11:44:12
*/
public class ParsingXML {
/**
* 节点正则
*/
private String nodeRegex="<(?!/|\\?)(.*?)>";
/**节点内容正则*/
private String nodeContentRegexTemplate="<#node#>(.*?)</#node#>";
/**元素属性正则*/
private String elementRegex="\\s{0,}(.*?)\\s{0,}=\\s{0,}\"(.*?)\"";
private String outStr="";
public void parsingXML(String inStr){
String rootName=firstElementName(nodeRegex,inStr);
outStr+=rootName;
String nodeContentRegex=nodeContentRegexTemplate.replace("#node#",rootName);
Matcher rootContentM=match(nodeContentRegex,inStr);
while(rootContentM.find()){
String content=rootContentM.group(1);
String nodeName=firstElementName(nodeRegex,content);
String nextNodeContentRegex=nodeContentRegexTemplate.replace("#node#",nodeName);
Matcher nextContentM=match(nextNodeContentRegex,content);
int i=1;
while(nextContentM.find()){
outStr+="\r\n\t"+nodeName+(i++)+"\r\n\t\t";
String elementContent=nextContentM.group(1);
Matcher elementM=match(elementRegex,elementContent);
while(elementM.find()){
outStr+=elementM.group(1).replace("<","")+":"+elementM.group(2)+"\r\n\t\t";
}
}
}
System.out.println(outStr);
}
private Matcher match(String regex,String content){
Pattern p=Pattern.compile(regex);
Matcher m=p.matcher(content);
return m;
}
/**
*
* @Description: 获取匹配的第一个元素
* @param regex
* @param content
* @return
* @return String
*/
private String firstElementName(String regex,String content){
String firstElementName="";
Matcher m=match(regex,content);
while(m.find()){
firstElementName=m.group(1);
break;
}
return firstElementName;
}
public static void main(String args[]){
String in = "<?xml version=\"1.0\" ?><Books><Book><Name = \"The C++ Programming Language\" Author=\"Bjarne Stroustrup\" /></Book><Book><Name = \"Effective C++\" Author = \"Scott Meyers\" /></Book></Books>";
new ParsingXML().parsingXML(in);
}
}