| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1889 人关注过本帖
标题:判断XML文件中的token
取消只看楼主 加入收藏
UAPOPPING
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2015-1-21
结帖率:100%
收藏
 问题点数:0 回复次数:2 
判断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
          }

}
搜索更多相关主题的帖子: character neither forward second angle 
2015-02-15 03:05
UAPOPPING
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2015-1-21
收藏
得分:0 
自己结贴,已找到办法。

public class XMLToken {
   
    private String token;
    XMLToken(String token) {token = this.token;} //constructor
   
    public boolean isOpeningTag(){   
        return token.matches("<\\w+>");
      }

      public boolean isClosingTag() {
         return token.matches("</\\w+>");
      }
      
      public boolean isSelfClosingTag() {
          return token.matches("<\\w+/>");  
      }
      
      public boolean isTag(){
          return token.matches("<\\w+>") || token.matches("</\\w+>") || token.matches("<\\w+/>");
      }
      
      public boolean isMalFormedTokens(){
          return token.matches("<^\\w+>") || token.matches("</^\\w+>") || token.matches("<^\\w+/>");
      }
      
      public boolean isWord(){
          return !isTag() && !isMalFormedTokens();
      }  
}
2015-02-15 10:33
UAPOPPING
Rank: 1
等 级:新手上路
帖 子:13
专家分:5
注 册:2015-1-21
收藏
得分:0 
回复 4楼 日知己所无
厉害!很有用,多谢!
2015-03-11 02:39
快速回复:判断XML文件中的token
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012336 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved