Initially, the XML language was only intended to be a substitute for the HTML language. However, as the language continues to develop and improve, people are increasingly discovering its advantages: such as extensible markup language and strict syntax. Provisions, the ability to use meaningful markup, the separation of content storage and presentation, and other advantages destined the language to be brilliant from the day it was born. The XML language has entered a period of rapid development after becoming a W3C standard. Of course, its own series of advantages and advantages have also destined major technology manufacturers to favor it. Java, as a development technology in the software industry, has also rapidly made In response, a variety of tools have emerged to support XML. This article will introduce several mainstream technologies for processing XML in Java from this perspective. I hope it will be helpful to you.
What excellent class libraries and tools does Java have to facilitate programmers to process XML?
An introduction to the three XML parsing methods: the famous DOM
It is no exaggeration to say that it is famous. DOM is the W3C standard API for processing XML. It is the basis for many other standards related to XML processing, not only Java, but also other languages such as Javascript, PHP, MS.NET and so on. This standard has become the most widely used XML processing method. Of course, in order to provide more and more powerful functions, Java has many direct extension tool classes for DOM, such as JDOM, DOM4J, etc., which are familiar to many Java programmers. They are basically expansions of DOM interface functions and retain a lot of DOM Due to the characteristics of the API, many original DOM programmers have mastered the use of the other two without any obstacles. The intuitive and easy-to-operate method makes it deeply loved by the majority of Java programmers.
Green and environmentally friendly SAX
The emergence of SAX has its special needs. Why is it said to be green and environmentally friendly? This is because SAX uses the least system resources and the fastest parsing method to provide support for XML processing. However, the tedious search methods that come with it also bring a lot of troubles to the majority of programmers, often causing headaches. At the same time, its support for XPath query function makes people love and hate it.
The obscure Digester: JavaBeanization of XML
Digester is an open source project under the Apache Foundation. The author's understanding of it comes from the research on the Struts framework. Are there many programmers who want to understand the design of major open source frameworks or even want to write a powerful framework themselves? You will encounter such a problem: What technology is used at the bottom of the framework to parse these various framework configuration files marked with XML language? DOM parsing is time-consuming, SAX parsing is too cumbersome, and the system overhead of each parsing will be too high. Therefore, everyone thought that JavaBeans corresponding to the XML structure should be used to load this information, and Digester came into being. Its emergence brings a convenient operation interface to the need to convert XML into JavaBean objects, so that more similar needs can get a more perfect solution, and programmers no longer need to implement such cumbersome parsing procedures themselves. At the same time, SUN also launched the XML and JavaBean conversion tool class JAXB, and interested readers can learn about it on their own.
Let's look at an example of Java dom parsing XML:
import java.io.*; import org.w3c.dom.*; import org.xml.sax.SAXException; import javax.xml.parsers.*; public class Xml { public static void main(String[] args) { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); try { DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(new File("E://work//test//xml1.xml")); NodeList nl = doc.getElementsByTagName("book"); for (int i = 0; i < nl.getLength( ); i++) { System.out.println(nl.item(i).getAttributes().item(0)); System.out.println(doc.getElementsByTagName("title").item(i) .getFirstChild().getNodeValue()); System.out.println(doc.getElementsByTagName("title").item(i) .getAttributes ().item(0)); System.out.println(doc.getElementsByTagName("author").item(i) .getFirstChild().getNodeValue()); System.out.println(doc.getElementsByTagName("year").item(i) .getFirstChild ().getNodeValue()); System.out.println(doc.getElementsByTagName("price").item(i) .getFirstChild().getNodeValue()); System.out.println(); } } catch (ParserConfigurationException e) { e.printStackTrace() ; } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
xml:
<bookstore> <book category="COOKING"> <title lang="en">Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price > </book> <book category="CHILDREN"> <title lang="en">Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99 </price> </book> <book category="WEB"> <title lang="en">Learning XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95< /price> </book> </bookstore>
result:
category="COOKING"Everyday Italianlang="en"Giada De Laurentiis200530.00category="CHILDREN"Harry Potterlang="en"J K. Rowling200529.99category="WEB"Learning XMLlang="en"Erik T. Ray200339.95