Java 怎么检索给定Reader指定的XML文档中第一个标记的名称,而不分析完整的文件/字符串

2019年8月3日23:28:28Java 怎么检索给定Reader指定的XML文档中第一个标记的名称,而不分析完整的文件/字符串已关闭评论 267 1678字阅读5分35秒

1.简介

Java 怎么检索给定Reader指定的XML文档中第一个标记的名称,而不分析完整的文件/字符串?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */



import java.io.IOException;

import java.io.Reader;

public class Main {
    
    public static String getFirstTagName(Reader xml) {
        final int OUTSIDE = 0; 
        final int BRACKET = 1; 
        final int COMMENT = 2; 
        final int IGNORE = 3; 
        final int TAG = 4; 

        int state = OUTSIDE;
        String commentMatch = null;
        StringBuffer tagBuffer = null;
        boolean sawBang = false;

        try {
            int c = xml.read();
            for (;;) {
                
                if (c == -1) {
                    return null;
                }
                switch (state) {
                case OUTSIDE:
                    
                    if (c == '<') {
                        state = BRACKET;
                        commentMatch = "!--";
                        sawBang = false;
                        c = xml.read();

                        
                    } else if (!Character.isWhitespace((char) c)) {
                        return null;

                        
                    } else {
                        c = xml.read();
                    }
                    break;

                case BRACKET:
                    
                    if (commentMatch != null) {
                        if (c == commentMatch.charAt(0)) {
                            
                            if (commentMatch.length() == 1) {
                                c = xml.read();
                                commentMatch = "-->";
                                state = COMMENT;
                            } else {
                                
                                
                                commentMatch = commentMatch.substring(1,
                                        commentMatch.length());
                            }
                        } else {
                            
                            commentMatch = null;
                        }
                    }

                    
                    if (Character.isWhitespace((char) c)) {
                        c = xml.read();
                        break;
                    }

                    switch (c) {
                    case '?':
                        c = xml.read();
                        state = IGNORE;
                        break;
                    case '!':
                        
                        c = xml.read();
                        sawBang = true;
                        if (commentMatch == null) {
                            state = IGNORE;
                        }
                        break;
                    case '-':
                        
                        c = xml.read();
                        if (commentMatch == null) {
                            state = IGNORE;
                        }
                        break;
                    case '>':
                        
                        c = xml.read();
                        state = OUTSIDE;
                        break;
                    default:
                        
                        if (sawBang) {
                            state = IGNORE;
                        } else {
                            state = TAG;
                        }
                        tagBuffer = new StringBuffer();
                    }
                    break;

                case COMMENT:
                    
                    if (c == commentMatch.charAt(0)) {
                        c = xml.read();
                        if (commentMatch.length() == 1) {
                            
                            state = OUTSIDE;
                        } else {
                            commentMatch = commentMatch.substring(1,
                                    commentMatch.length());
                        }
                    } else {
                        
                        c = xml.read();
                        commentMatch = "-->";
                    }
                    break;

                case IGNORE:
                    
                    if (c == '>') {
                        c = xml.read();
                        state = OUTSIDE;
                    } else {
                        c = xml.read();
                    }
                    break;

                case TAG:
                    
                    
                    if (Character.isWhitespace((char) c) || c == '>'
                            || c == '/') {
                        return tagBuffer.toString();
                    } else {
                        tagBuffer.append((char) c);
                        c = xml.read();
                    }
                    break;
                }
            }
        } catch (IOException ex) {
            
            return null;
        }
    }
}

 
继续阅读