2012-03-02 48 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