判断XML文件中的token
要写一段java代码,判断XML文件的的token是tag还是word.以下是要求:
An XML file is a text file made up of tokens. A token is either a word or it is a tag.
1. An token is a tag if and only if it is bounded by angle brackets, e.g., <para>.
2. If the second character is a forward slash (e.g., </para>), this a closing tag.
3. If the second to last character is a forward slash (e.g., <br/>), this is a self-closing tag.
4. If a tag is neither of the above, it is an opening tag
5. Except for the slashes mentioned above, the only characters that may appear between the brackets are letters, numerals, and hyphens. This means, for example, that the string <<para>> is neither a tag nor a word. It should not appear in an XML file.
我已经找出1-4的表达方式,但问题在第5点,如何找出既不是tag又不是word的token。请大家帮帮忙,以下是我写的代码
public class XMLToken {
private String token;
XMLToken(String token) {token = this.token;} //constructor
public static boolean isOpeningTag(){ //找出 opening tag
String token =new String();
return token.matches("<\\w+>");
}
public static boolean isClosingTag(){ //找出closing tag
String token =new String();
return token.matches("</\\w+>");
}
public static boolean isSelfClosingTag(){ //找出self closing tag
String token =new String();
return token.matches("<\\w+/>");
}
public static boolean isTag(){ //找出 tag
String token =new String();
return token.matches("<\\w+>") || token.matches("</\\w+>") || token.matches("<\\w+/>");
}
public static boolean malformedTag(){
//找出既不是word也不是tag的token
}
}