package com.chobits.xml;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class XMLDocument{
/**
* 唯一标识
*/
private String id = UUID.randomUUID().toString();
/**
* 从文件或原始字符串原始的字符串
*/
private String srcXMLText = "";
/**
* 除了声明,doctype意外的部分
*/
private String bodyText = "";
//声明对象
private XMLDeclaration declaration = null;
//语法类型
private XMLDoctype doctype = null;
//从第几个字符到哪个字符结束
private XMLPoint bodyPoint = null;
//标签和尾标签临时存放标识
private Map<String, String> matchIdCache = new HashMap<String, String>();
//
private XMLNode rootNode = null;
private XMLNode currentParentNode = null;
private List<String> notes = new ArrayList<String>();
private XMLElement rootElement = null;
public XMLDocument() {
}
public void build(String text) throws Exception{
matchIdCache.clear();
notes.clear();
srcXMLText = text;
adapterHeaderInfo();
adapterDocType();
adapterBodyText();
}
private void adapterHeaderInfo() throws Exception{
if(srcXMLText==null || srcXMLText.isEmpty()){
throw new Exception("xml文本是空的");
}
String firstNode = "<?xml";
String lastNode = "?>";
int s = srcXMLText.indexOf(firstNode);
int e = srcXMLText.indexOf(lastNode);
if(s==-1 && e==-1){
declaration = new XMLDeclaration();
bodyPoint = new XMLPoint(0, srcXMLText.length());
}else{
if(s>-1 && e>-1 && e>s){
declaration = new XMLDeclaration();
declaration.build(srcXMLText.substring(s, e+2));
bodyPoint = new XMLPoint(e+3, srcXMLText.length());
}else{
throw new Exception("xml头声明错误");
}
}
}
private void adapterDocType() throws Exception{
String firstNode = "<!DOCTYPE";
int s = srcXMLText.indexOf(firstNode);
if(s>-1){
int e = s;
while(true){
e++;
char c = srcXMLText.charAt(e);
if(c=='>'){
break;
}
}
String context = srcXMLText.substring(s+1+firstNode.length(), e).trim();
doctype = new XMLDoctype();
doctype.build(context);
bodyPoint = new XMLPoint(e+1, srcXMLText.length());
}
}
private void adapterBodyText() throws Exception{
bodyText = srcXMLText.substring(bodyPoint.getStartIndex(), bodyPoint.getEndIndex()).trim();
bodyText = bodyText.replaceAll("\r", "").replaceAll("\n", " ").replaceAll("\t", "").replaceAll("\r\n", " ").replaceAll(" ", " ");
int index = 0;
while(true){
char c = bodyText.charAt(index);
if(c=='<'){
int skipSize = this.findNode(index);
index += skipSize;
}else{
index ++;
}
if(index>=bodyText.length()){
break;
}
}
rootElement = new XMLElement();
rootElement.build(rootNode, null);
}
private int findNode(int startIndex) throws Exception{
int skipSize = 0;
for(int i=startIndex;i<bodyText.length();i++){
char c = bodyText.charAt(i);
if(c==' '){
for(int k=i+1;k<bodyText.length();k++){
char tempChar = bodyText.charAt(k);
if(tempChar=='>'){
String seed = bodyText.substring(startIndex, k+1).trim();
if(seed.indexOf("<!--")==0){
int tempEndNoteIndex = bodyText.indexOf("-->", startIndex);
seed = bodyText.substring(startIndex, tempEndNoteIndex+3);
acceptNote(seed, startIndex, tempEndNoteIndex+3);
skipSize = tempEndNoteIndex+3-startIndex;
}else if(seed.indexOf("<![CDATA[")==0){
int tempEndCdataIndex = bodyText.indexOf("]]>", startIndex);
seed = bodyText.substring(startIndex, tempEndCdataIndex+3);
acceptCDATA(seed, startIndex, tempEndCdataIndex+3);
skipSize = tempEndCdataIndex+3-startIndex;
}else{
if(seed.endsWith("/>")){
this.acceptHeaderAndTailNode(seed, startIndex, k+1);
skipSize = k+1-startIndex;
}else{
this.acceptHeaderNode(seed, startIndex, k+1);
skipSize = k+1-startIndex;
}
}
break;
}
}
break;
}
if(c=='/'){
char nextChar = bodyText.charAt(i+1);
if(nextChar=='>'){
this.acceptHeaderAndTailNode(bodyText.substring(startIndex, i+2), startIndex, i+2);
skipSize = i+2-startIndex;
break;
}else{
for(int k=i+1;k<bodyText.length();k++){
char tempChar = bodyText.charAt(k);
if(tempChar=='>'){
acceptTailNode(bodyText.substring(i-1, k+1), i-1, k+1);
skipSize = k+1-(i-1);
break;
}
}
}
break;
}
if(c=='>'){
this.acceptHeaderNode(bodyText.substring(startIndex, i+1), startIndex, i+1);
skipSize = i+1-startIndex;
break;
}
}
return skipSize;
}
private void acceptHeaderNode(String srcXML, int startIndex, int endIndex) throws Exception{
XMLNode node = new XMLNode();
node.setId(UUID.randomUUID().toString());
node.build(srcXML.trim(), XMLNode.TYPE_HEAD, startIndex, endIndex);
matchIdCache.put(node.getName(), node.getId());
if(rootNode==null){
currentParentNode = node;
rootNode = currentParentNode;
}else{
node.setParent(currentParentNode);
currentParentNode.getChildren().add(node);
currentParentNode = node;
}
}
private void acceptHeaderAndTailNode(String srcXML, int startIndex, int endIndex) throws Exception{
XMLNode node = new XMLNode();
node.setId(UUID.randomUUID().toString());
node.build(srcXML.trim(), XMLNode.TYPE_HEAD_ADN_TAIL, startIndex, endIndex);
node.setClosed(true);
node.setParent(currentParentNode);
currentParentNode.getChildren().add(node);
}
private void acceptCDATA(String srcXML, int startIndex, int endIndex) throws Exception{
XMLNode node = new XMLNode();
node.setId(UUID.randomUUID().toString());
node.build(srcXML.trim(), XMLNode.TYPE_CDATA, startIndex, endIndex);
node.setClosed(true);
currentParentNode.setCdata(true);
currentParentNode.getChildren().add(node);
}
private void acceptNote(String srcXML, int startIndex, int endIndex) throws Exception{
XMLNode node = new XMLNode();
node.setId(UUID.randomUUID().toString());
node.build(srcXML.trim(), XMLNode.TYPE_NOTE, startIndex, endIndex);
node.setClosed(true);
if(currentParentNode==null){
}else{
currentParentNode.getChildren().add(node);
}
}
private void acceptTailNode(String srcXML, int startIndex, int endIndex) throws Exception{
XMLNode node = new XMLNode();
node.build(srcXML.trim(), XMLNode.TYPE_TAIL, startIndex, endIndex);
String headerUID = matchIdCache.get(node.getName());
node.setId(headerUID);
matchIdCache.remove(node.getName());
node.setClosed(true);
if(currentParentNode.getParent()==null){
}else{
currentParentNode.getParent().getChildren().add(node);
}
if(currentParentNode!=null){
if(currentParentNode.getChildren().size()>0){
String text = "";
XMLNode firstChild = currentParentNode.getChildren().get(0);
text += bodyText.substring(currentParentNode.getEndIndex(), firstChild.getStartIndex()).trim();
XMLNode lastChild = currentParentNode.getChildren().get(currentParentNode.getChildren().size()-1);
text += bodyText.substring(lastChild.getEndIndex(), startIndex).trim();
currentParentNode.setText(text.trim());
}else{
String text = bodyText.substring(currentParentNode.getEndIndex(), startIndex);
currentParentNode.setText(text.trim());
}
currentParentNode.setClosed(true);
currentParentNode = currentParentNode.getParent();
}
}
public String getId() {
return id;
}
public XMLDeclaration getDeclaration() {
return declaration;
}
public void setDeclaration(XMLDeclaration declaration) {
this.declaration = declaration;
}
public XMLDoctype get
没有合适的资源?快使用搜索试试~ 我知道了~
普通xml,soap-xml文本解析
共11个文件
java:11个
5星 · 超过95%的资源 需积分: 42 104 下载量 8 浏览量
2015-03-30
11:44:16
上传
评论 3
收藏 11KB ZIP 举报
温馨提示
突破jdk与wsdl第三方对xml文本解析领域割据趋势,该脚本既可以支持普通xml解析,也支持soap-xml第三方xml文本的解析; 极大的节省了引入冗余jar资源; 纯底层算法解析,不依赖任何第三方工具包,自主产权维护性强。
资源推荐
资源详情
资源评论
收起资源包目录
xml.zip (11个子文件)
common
RegularExpression.java 3KB
FileUtil.java 1KB
xml
XMLNode.java 4KB
XMLDeclaration.java 1KB
XMLElement.java 8KB
XMLUtil.java 3KB
XMLPoint.java 518B
XMLDocument.java 8KB
XMLHelper.java 1KB
XMLDoctype.java 1KB
XMLAttribute.java 664B
共 11 条
- 1
资源评论
- 二分之一的美好2018-01-03挺好的,很有用,
- gctianyou2018-10-12不错,挺好的,可以用
- 卡拉多纳之大马哈鱼2017-06-30还不错的,值得下载研究
清水依恋成
- 粉丝: 11
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功