首先,解析xml,我们不是解析字符串,而是把xml转换成byte。由byte进行截取节点里的内容。最后返回byte。将所有数据类型转换成最原始的数据类型。这样操作起来速度方面会提高很高。
获取XML节点主体方法
/** * 获取XML节点主体方法 * @author slz * */ public class XMLString { //内容 byte[] connect; //是否存在节点 int flag = 1; //错误内容 byte[] error; //每一个节点之后的XML内容 byte[] xml; // 通用XML解析(字符数组方法) public void XMLNode(byte[] xmldoc,String XmlNodeName,int StartPos) throws Exception { try{ boolean finded; int p; //XML总长度 int p0=xmldoc.length; //头标签长度 int p1=XmlNodeName.length()+2; //尾标签长度 int p2=p1+1; int k=0; //头标签在XML的坐标 int bpos=0; //尾标签在XML的坐标 int epos=0; byte[] bTagbytes=new byte[p1]; byte[] eTagbytes=new byte[p2]; bTagbytes[0]='<'; System.arraycopy(XmlNodeName.getBytes(),bTagbytes,1,XmlNodeName.length()); bTagbytes[p1-1]='>'; eTagbytes[0]='<'; eTagbytes[1]='/'; System.arraycopy(XmlNodeName.getBytes(),eTagbytes,2,XmlNodeName.length()); eTagbytes[p1]='>'; //查找开始标记 finded=false; for(p=StartPos;p<p0-p2;p++){ finded=false; if (xmldoc[p]=='<'){ finded=true; for(k=0;k<p1;k++){ if (xmldoc[k+p]!=bTagbytes[k]){ finded=false; break; } } } if (finded){ bpos=p+p1; break; } } if (!finded){ error="查找不到开始标记符".getBytes(); //System.out.println(new String(error)); flag = 0; } //查找结束标记 finded=false; for(p=bpos;p<p0-p2;p++){ finded=false; if (xmldoc[p]=='<' && xmldoc[p+1]=='/'){ finded=true; for(k=0;k<p2;k++){ if (xmldoc[k+p]!=eTagbytes[k]){ finded=false; break; } } } if (finded){ epos=p; break; } } if (!finded){ error="查找不到结束标记符".getBytes(); //System.out.println(new String(error)); flag = 0; } //开始与结束标记中间无内容判断 /*if (epos==bpos){ error="查找结果为NULL".getBytes(); System.out.println(new String(error)); flag = 0; }*/ byte[] dest = new byte[epos-bpos]; System.arraycopy(xmldoc,bpos,dest,epos-bpos); connect = dest; byte[] xmlStr = new byte[p0-epos-p2]; System.arraycopy(xmldoc,epos+p2,xmlStr,p0-epos-p2); xml = xmlStr; }catch (Exception ex){ if(ex.getMessage()==null){ error="出现异常".getBytes(); System.out.println(new String(error)); } flag = 0; } } public byte[] getConnect() { return connect; } public void setConnect(byte[] connect) { this.connect = connect; } public byte[] getXml() { return xml; } public void setXml(byte[] xml) { this.xml = xml; } public int getFlag() { return flag; } public void setFlag(int flag) { this.flag = flag; } public byte[] getError() { return error; } public void setError(byte[] error) { this.error = error; } }
Main:
public class Test { public static void main(String[] args) { try { String xmldoc = "<EdorDetails><EdorDetail>"+ "<Date></Date><OtherNo></Otherno><OperateType></OperateType><Money></Money><InsuaccBala></InsuaccBala>"+ "<Status></Status><Info></Info></EdorDetail><EdorDetail>"+ "<Date></Date><OtherNo></Otherno><OperateType></OperateType>"+ "<Money></Money><InsuaccBala></InsuaccBala><Status></Status><Info></Info>"+ "</EdorDetail></EdorDetails>"; String sb = XmlNode(xmldoc,"EdorDetail",0); System.out.println(sb.replaceAll(",","\r\n")); } catch (Exception e) { e.printStackTrace(); } } /** * 获取XML节点内容 * @param xmldoc XML内容 * @param XmlNodeName 节点名称 * @param NodeNum 获取第几个节点里的内容 * @return * @throws Exception */ private static String XmlNode(String xmldoc,int NodeNum) throws Exception{ XMLString xml = new XMLString(); xml.XMLNode(xmldoc.getBytes(),XmlNodeName,0); int flag = xml.getFlag(); StringBuffer sb = new StringBuffer(); while(flag!=0){ sb.append(new String(xml.getConnect())); xml.XMLNode(xml.getXml(),0); flag = xml.getFlag(); if(flag==0){ break; } sb.append(","); } String xmlStr = sb.toString(); if(NodeNum!=0){ String[] str = xmlStr.split(","); if(NodeNum<=str.length&&NodeNum>=0){ xmlStr = str[NodeNum-1]; } } return xmlStr; } }