?? xmlparser.java
字號:
package org.gggeye.easymf.xml;
import java.util.Enumeration;
import java.util.Vector;
/** *//**
* @author shizhongqin
* @date 2006-5-1
* mail:shizhongqin@gmail.com
*/
public class XMLParser {
private Node root=null;
private Node exeNode=null;
private int offset=0;
private String xml="";
private int xmlLength=0;
private String version="1.1";
private String encoding="UTF-8";
public XMLParser(String xml){
this.xml=xml;
this.xmlLength=xml.length();
}
public char getNextCharacter(){
char rt= xml.charAt(offset);
offset++;
return rt;
}
/** *//**
* 判斷下一字符是否為指定字符token
* @param token
* @return
*/
private boolean match(char token){
for(int i=offset;i<this.xmlLength;i++){
char tc=xml.charAt(i);
if (tc!=' '){
if (tc==token){
return true;
}else{
return false;
}
}
}
return false;
}
private String getDescription(){
skipSpace();
StringBuffer desc=new StringBuffer();
while(offset<this.xmlLength-2){
char tc1=this.getNextCharacter();
if (tc1=='-'){
if ((xml.charAt(offset)=='-')&&(xml.charAt(offset+1)=='>')){
offset+=2;
return desc.toString();
}
}else{
desc.append(tc1);
}
}
return null;
}
/** *//**
* 獲取Node名稱
* @return
*/
private String getNodeName(){
skipSpace();
char[] name=new char[120];//
int i=0;
while(i<120){
char tc=getNextCharacter();
if ((tc==' ')||(tc=='>')||(tc=='/')){
if (i>0)
return new String(name).trim();
}else {
name[i]=tc;
i++;
if (i>120){
System.err.println("NODE NAME長度只能小于120");
return null;
}
}
}
return null;
}
/** *//**
* 獲取屬性信息
*
*/
private void getAttributes(){
skipSpace();
StringBuffer name=new StringBuffer();
StringBuffer value=new StringBuffer();
boolean isAttribute=false;
while(offset<this.xmlLength){
char tc1=this.getNextCharacter();
if (tc1=='='){
skipSpace();
char tc2=this.getNextCharacter();
if (tc2=='"'){//獲取屬性值
isAttribute=true;
while(offset<this.xmlLength){
char tc3=this.getNextCharacter();
if (tc3=='"'){
this.exeNode.setAttribute(name.toString(),value.toString());
this.skipSpace();
value.delete(0,value.length());
name.delete(0,name.length());
break;
}else
value.append(tc3);
}
}
}else if (tc1=='/'){
skipSpace();
char tc2=this.getNextCharacter();
if (tc2!='>'){
System.err.println("/必須使用>來封閉");
}else{
this.exeNode=this.exeNode.getParent();
break;
}
}else if (tc1=='>'){
break;
}else{
name.append(tc1);
}
}
}
private int skipSpace(){
int skipCount=0;
while(offset<xml.length()){
char tc=xml.charAt(offset);
if ((tc!=' ')&&(tc!=' ')&&(tc!=' ')){
return skipCount;
}else{
offset++;
skipCount++;
}
}
return skipCount;
}
private String getValue(){
StringBuffer value=new StringBuffer();
value.append(xml.charAt(offset-1));
while(offset<xml.length()){
char tc=this.getNextCharacter();
value.append(tc);
if (xml.charAt(offset)=='<'){
return value.toString().trim();
}
}
return null;
}
private void getXMLHeader(){
this.skipSpace();
if ((this.xml.charAt(offset)=='<')&&(this.xml.charAt(offset+1)=='?')){
int idx=this.xml.indexOf("version");
if (idx>0){
boolean start=false;
StringBuffer tmp=new StringBuffer();
for(int i=idx+8;i<this.xmlLength;i++){
char tc=this.xml.charAt(i);
if (tc=='"'){
if (start==false){
start=true;
}else{
break;
}
}else{
if (start)
tmp.append(tc);
}
}
this.version=tmp.toString();
}
idx=this.xml.indexOf("encoding");
if (idx>0){
boolean start=false;
StringBuffer tmp=new StringBuffer();
for(int i=idx+9;i<this.xmlLength;i++){
char tc=this.xml.charAt(i);
if (tc=='"'){
if (start==false){
start=true;
}else{
break;
}
}else{
if (start)
tmp.append(tc);
}
}
this.encoding=tmp.toString();
}
int end=this.xml.indexOf("?>");
offset=end+2;
}
}
public Node parse(){
getXMLHeader();
while(offset<this.xmlLength){
this.skipSpace();
char token=getNextCharacter();
if (token=='<'){
if (match('!')){
getNextCharacter();
char tc=getNextCharacter();
if (tc=='-'){
tc=getNextCharacter();
if (tc=='-'){
//System.out.println("注釋行");
String desc=getDescription();
if (this.exeNode!=null)
this.exeNode.setDescription(desc);
}else{
System.err.println("語法錯誤在"+offset);
return null;
}
}
}else if (match('/')){
String nodeName=this.getNodeName();
if (exeNode.getName().equalsIgnoreCase(nodeName))
exeNode=exeNode.getParent();
else{
System.err.println("期望封閉標簽為:"+exeNode.getName()+",實際標簽為:"+nodeName);
return null;
}
}else{
String name=this.getNodeName();
Node newNode=new Node(name);
if (root==null){
root=newNode;
exeNode=root;
}else{
exeNode.addChild(newNode);
exeNode=newNode;
}
char tc=this.xml.charAt(offset-1);
if (tc==' ')
getAttributes();
else{
if (tc!='>')
System.err.println(exeNode.getName()+"期待關閉");
}
}
}else{
exeNode.setValue(getValue());
}
}
return root;
}
public static void main(String[] args){
String xml="<?xml version=\"1.0\" encoding=\"GB2312\"?>" +
"<!--注釋行--><root desc=\"一個測試的例子\"><book name=\"test\" " +
"value=\"我的\"/><book name=\"跌而\">我的值</book></root>";
XMLParser parser=new XMLParser(xml);
Node root=parser.parse();
System.out.println(root.getName());
Vector nodes = root.getChildNodes();
load(nodes);
// System.out.println(root.toString());
}
static void load(Vector _nodes){
System.out.println(_nodes);
for(int i=0; i<_nodes.size(); i++){
Node tNode = (Node) _nodes.elementAt(i);
System.out.println(tNode.getName());
Enumeration keys=tNode.getAttributes().keys();
while(keys.hasMoreElements()){
String key=(String)keys.nextElement();
String value= tNode.getAttribute(key);
System.out.println(" "+ key+ "=" + value + " ");
}
System.out.println(tNode.getValue());
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -