Java parses xml using DOM

Source: Internet
Author: User

Recently, our project is configuring the color of the system interface, and uses java dom to parse xml files to automatically generate css. I checked some information online and listed the methods and examples of using DOM to parse XML documents in java in the article for my reference.

1. DOM (Document Object Model)

DOM is the official W3C standard for XML documents in a way unrelated to the platform and language. DOM is a set of nodes organized in hierarchies. Because it is based on information layers, DOM is regarded as tree-based or object-based. You can think of it as a DOM tree and operate XML documents through the relationship between nodes and nodes. However, DOM-based XML operations require loading the entire XML document to construct a hierarchy, which consumes a lot of resources.

The following XML file is used to generate the css interface.

                            #header                               margin-bottom:20px;             min-height: 70px;             padding: 28px 0px 0;             box-shadow: 0 5px 10px rgba(0, 0, 0, 0.4);             background-image: -moz-linear-gradient(top, rgba(255, 255, 255, 0.1) 0%, rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0.2) 98%, #FFFFFF 100%);                                   color-stop(40%, rgba(0, 0, 0, 0.1)), color-stop(98%, rgba(0, 0, 0, 0.2)),color-stop(100%, #FFFFFF));             background-image: -webkit-linear-gradient(top, rgba(255, 255, 255, 0.1) 0%, rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0.2) 98%, #FFFFFF 100%);             background-image: -o-linear-gradient(top, rgba(255, 255, 255, 0.1) 0%, rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0.2) 98%, #FFFFFF 100%);                filter: progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr='#22FFFFFF', EndColorStr='#33000000');             background-image: linear-gradient(to bottom, rgba(255, 255, 255, 0.1) 0%, rgba(0, 0, 0, 0.1) 40%, rgba(0, 0, 0, 0.2) 98%, #FFFFFF 100%);                               background-color:               

 

Java calls DOM to parse XML

                                  String IMPORTANT = "important";         String CSS_CLASS_ID = "cssClass_id";         String CONFIG_CSS = "configCss";         String INHERENT_CSS = "inherentCss";         String CSS_CLASS = "cssClass";         String WRAPPER = "    ";        Map<String, String>       CssStyleBuilderAdv(Map<String, String>         .cssElementMap =                                 buildCssFile(String currentTheme)                                 String rebuiltCss = ""         DocumentBuilderFactory domfac =                      DocumentBuilder domBuilder =             String cssTemplateUrl = SystemParameters.getConfigPath() + "/" + currentTheme + "/cssTemplate.xml";              File file =              InputStream is =              Document doc =             NodeList cssItemList = doc.getElementsByTagName("cssItem");               (cssItemList !=                   ( i = 0; i < cssItemList.getLength(); i++) {                       StringBuffer cssClassList =  StringBuffer();                       StringBuffer inherentCssList =  StringBuffer();                      StringBuffer configCssList =  StringBuffer();                       Node cssItem =                     NodeList childNodes = cssItem.getChildNodes();                        ( j = 0; j < childNodes.getLength(); j++) {                           StringBuffer cssClassItem =                          StringBuffer inherentCss =                          StringBuffer configCss =                          Node cssNode =                          (cssNode.getNodeType() ==                             NodeList elementNodes = cssNode.getChildNodes();                               ( z = 0; z < elementNodes.getLength(); z++) {                                  Node elementNode =                                  (elementNode.getNodeType() ==                                                                              cssClassItem.append(elementNode.getTextContent());                                          cssClassItem.append(",\n"                                                                                inherentCss.append(elementNode.getTextContent());                                          inherentCss.append("\n"                                                                               configCss = configCss.append(switchValueByAttrId(elementNode, configCss));                              rebuiltCss += cssClassList.toString().substring(0, cssClassList.length() - 2) + "{\n"                             + inherentCssList + configCssList + "}\n\n";            }              LOGGER.warn("ParserConfigurationException : "         }              LOGGER.warn("FileNotFoundException : "         }              LOGGER.warn("SAXException : "         }              LOGGER.warn("IOException : "           rebuiltCss;                                 String packageName = currentTheme.substring(0, 1).toLowerCase() + currentTheme.substring(1         String url = SystemParameters.getCssPath() + "/" + packageName + "/CSS_Build.css"                      File f =                               BufferedWriter output =  BufferedWriter(                 f.setWritable(               }                                       f.setWritable(                     BufferedWriter output =  BufferedWriter(                   }                      LOGGER.info("Create Failed"           }              LOGGER.warn("Exception : "                                 NamedNodeMap elementAttMap =         String attributeValue = elementAttMap.getNamedItem(CSS_CLASS_ID).getTextContent();          String importantValue = ""         String nodeTextContext = WRAPPER + elementNode.getTextContent() + " "          (elementAttMap.getNamedItem(IMPORTANT) !=              importantValue = "!important";           configCss.append(nodeTextContext + cssElementMap.get(attributeValue));            configCss.append(";\n"          configCss;   }

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.