2012-03-02 60 views
-2

我是新來的java我想存儲散列表或特定的CSS文件的所有類名(只有名稱)請幫助我我想存儲BODY,.title,.surveyinstruct,.parthdr在HashMap中需要從CSS獲取類

BODY{ 
font-family: sans-serif; 
color: #cccccc; 
font-size: medium; 
font-style: normal; 
font-weight: bold; 
background-color: black; 
background-image: url(images/BLACKbody_whitemarble.jpg); 
} 
.title{ 
font-family: sans-serif; 
color: Navy; 
font-size: medium; 
font-style: normal; 
font-weight: bold; 
background-color: Transparent; 
border: no; 
} 
.surveyinstruct{ 
font-family: Arial; 
color: black; 
font-size: small; 
font-style: normal; 
font-weight: normal; 
background-color: Transparent; 
border: no; 
} 
.parthdr{ 
color: Navy; 
font-size: x-small; 
font-style: normal; 
font-weight: bold; 
background-color: #999999; 
border: no; 
font-family: Arial; 
} 

現在我已經存儲身體,.title僞,.surveyinstruct,.parthdr在HashMap中或載體 即HashMap中應包含哪些我可以用進一步的工作 感謝所有的類名提前

回答

0

您可以使用CSS Parser。這不是完美的,那很簡單,但它給你更多...

public Map<String, CSSStyleRule> parseCSS() throws IOException { 
    Map<String, CSSStyleRule> rules = new HashMap<String, CSSStyleRule>(); 

    InputSource inputSource = new InputSource(new FileReader("bootstrap.css")); 
    CSSStyleSheet styleSheet = new CSSOMParser().parseStyleSheet(inputSource, null, null); 

    CSSRuleList ruleList = styleSheet.getCssRules(); 
    for (int i = 0; i < ruleList.getLength(); i++) { 
     CSSRule rule = ruleList.item(i); 
     if (rule.getType() == CSSRule.STYLE_RULE) { 
      CSSStyleRule styleRule = (CSSStyleRule) rule; 
      rules.put(styleRule.getSelectorText(), styleRule); 
     } 
    } 

    return rules; 
} 
+0

CSSParser不支持CSS3,例如, CSS3媒體查詢,CSS3選擇器和特定於瀏覽器的選擇器,如@ -webkit-keyframes。所以要小心。否則,它是好的..! – 2012-03-02 07:09:45

+0

我認爲這個限制來自[SAC](http://www.w3.org/Style/CSS/SAC/),它剛剛開發。不知道是否在這個時候... – 2012-03-02 07:16:22

+0

它的錯誤在CSSOMParser() – sunshah1290 2012-03-02 09:59:35