Why does this chapter appear? Because XML is so important and widely used! Whether it is data storage or other aspects, such as configuration files. XML is a mechanism for encoding data independent from any programming language. In the field of data exchange, it is becoming very popular! Because of its node-based storage format, it can be used to describe many suitable scenarios. Of course, Java also provides great support for packaging and parsing XML files! This chapterThe beauty of Java [from cainiao to masters] SeriesAfter completing this chapter, you will master most of the XML operations!
If you have any questions during reading, please contact egg in time.
Mailbox: xtfggef@gmail.com Weibo: http://weibo.com/xtfggef
Reprint please explain the source: http://blog.csdn.net/zhangerqing
I. Features of XML
1. XML is independent of any programming language and allows people to encode complex data in a way that is easy to parse by recipient. Let's take a look at a simple XML file:
<note><to>George</to><from>John</from>
For programmers, it is easy to understand what this means. Obviously, this is a note passed to others. If we write the following:
George
John
Reminder
Don't forget the meeting!
Although the above content is finally extracted from XML, we cannot see what this is.
2. Making data into XML helps to change its content
If we want to add another one: time. In XML, we can do this:
<note><to>George</to><from>John</from>
During our parsing, we only need to find the corresponding tag "time", which is very convenient! This feature helps organize complex data.
3. Main differences between XML and HTML
XML is not an alternative to HTML. xml and HTML are designed for different purposes. XML is designed to transmit and store data, and its focus is on data content. HTML is designed to display data. Its focus is on the appearance of the data. HTML is designed to display information, while XML is designed to transmit information. Not all HTML tags need to appear in pairs. XML requires that all tags must appear in pairs. HTML tags are case insensitive and XML tags are case sensitive.
4. XML document is a tree structure
Represents a book in the following 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>
Ii. XML parsing Technology
1. Well-known dom Technology
Dom is the standard API for W3C to process XML. It is the basis of many other standards related to XML processing, not only Java, but also JavaScript, PHP, MS. net and other languages have implemented this standard and become the most widely used XML processing method. Of course, to provide more and more powerful functions, Java has many tool classes for Dom direct extension, such as JDOM and dom4j which many Java programmers are familiar, they are basically an extension of the functions of the DOM interface, retaining many dom api features, many of the original dom
Programmers can even master the use of the other two without any obstacles. The intuitive and easy-to-operate method has become a favorite of Java programmers.
2. Environmentally Friendly Sax
The emergence of sax has special requirements. Why is it green? This is because it uses the least amount of system resources and the fastest Parsing Method to support XML processing. However, the complicated search method also brings a lot of troubles to the majority of programmers, which is often a headache. At the same time, the support for the XPath query function makes people love and hate it.
Comparison of the two technologies:
Dom
Advantages and disadvantages: the W3C standard is implemented, and multiple programming languages support this resolution method. This method is easy to use and easy for beginners to master. The processing method is to read XML into the memory as a tree structure for operation and parsing. Therefore, the application can modify the content and structure of XML data, however, because it needs to read the entire XML file into the memory for analysis at the beginning of processing, therefore, when parsing XML files with large amounts of data, it may encounter risks similar to memory leaks and program crashes. Please pay attention to this.
Applicability: small XML file parsing, full parsing, or most XML parsing, XML tree content needs to be modified to generate its own object model
Sax
Sax fundamentally solves the problem that Dom occupies a large amount of resources when parsing XML documents. The implementation is to read the entire XML document tree through a technology similar to stream parsing, and respond to the needs of programmers for XML data parsing through an event processor. Because it does not need to read the entire XML file into the memory, it is very obvious to save system resources, it plays an important role in some cases where large XML documents need to be processed and high performance requirements are required. The support for xpath query of sax makes developers more flexible and easier to process XML. However, at the same time, there are still some shortcomings that plague the majority of developers: first, it is very complicated
The API interface is daunting. Because it is a file scanning method similar to stream parsing, modification to the XML tree content structure by applications is not supported, which may cause inconvenience.
Applicability: Large XML file parsing, partial parsing, or partial XML tree content, XPath query, and specific XML tree object model generation.
Iii. instance application
First, create an XML document: (books. XML is placed under the project root path, not SRC)
<?xml version="1.0" encoding="UTF-8"?> <books> <book id="01" name="book1"> <title>Harry Potter</title> <author>J K. Rowling</author> </book> <book id="02" name="book2"> <title>Thinking in Java</title> <author>Bruke</author> </book> </books>
Use dom for parsing:
/** * the DOM Parser Example * @author egg * email:xtfggef@gmail.com * microblog:http://weibo.com/xtfggef */public class DOMTest {/* build a DocumentBuilderFactory */DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();public static void main(String[] args) {DOMTest parser = new DOMTest();Document document = parser.parse("books.xml");/* get root element */Element rootElement = document.getDocumentElement();/* get all the nodes whose name is book */NodeList nodeList = rootElement.getElementsByTagName("book");if (nodeList != null) {for (int i = 0; i < nodeList.getLength(); i++) {/* get every node */Node node = nodeList.item(i);/* get the next lever's ChildNodes */NodeList nodeList2 = node.getChildNodes();for (int j = 0; j < nodeList2.getLength(); j++) {Node node2 = nodeList2.item(j);if (node2.hasChildNodes()) {System.out.println(node2.getNodeName() + ":"+ node2.getFirstChild().getNodeValue());}}}}}/* Load and parse XML file into DOM */public Document parse(String filePath) {Document document = null;try {/* DOM parser instance */DocumentBuilder builder = builderFactory.newDocumentBuilder();/* parse an XML file into a DOM tree */document = builder.parse(new File(filePath));} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return document;}}
Output:
Title: Harry Potter
Author: j k. Rowling
Title: Thinking in Java
Author: Bruke
To understand XML parsing, you must first understand several concepts: nodes and attributes, such:
In this way, it is easy to understand the above example. Let's give an example for everyone to practice on their own:
<?xml version="1.0" encoding="UTF-8"?> <books> <name>Think in C++</name> <price>100</price> <author>brucl</author> </books>
Use Sax for parsing:
public class SAXTest {class BookHandler extends DefaultHandler {private List<String> nameList;private boolean title = false;public List<String> getNameList() {return nameList;}// Called at start of an XML document@Overridepublic void startDocument() throws SAXException {System.out.println("Start parsing document...");nameList = new ArrayList<String>();}// Called at end of an XML document@Overridepublic void endDocument() throws SAXException {System.out.println("End");}@Overridepublic void startElement(String uri, String localName, String qName,Attributes atts) throws SAXException {// Using qualified name because we are not using xmlns prefixes// here.if (qName.equals("title")) {title = true;}}@Overridepublic void endElement(String namespaceURI, String localName,String qName) throws SAXException {// End of processing current elementif (title) {title = false;}}@Overridepublic void characters(char[] ch, int start, int length) {// Processing character data inside an elementif (title) {String bookTitle = new String(ch, start, length);System.out.println("Book title: " + bookTitle);nameList.add(bookTitle);}}}public static void main(String[] args) throws SAXException, IOException {XMLReader parser = XMLReaderFactory.createXMLReader();BookHandler bookHandler = (new SAXTest()).new BookHandler();parser.setContentHandler(bookHandler);parser.parse("books.xml");System.out.println(bookHandler.getNameList());}}
XML Packaging
Public class saxgeneratorxml {public static void main (string [] ARGs) {string outputpath = "persons. XML "; generatexml (outputpath);} public static void generatexml (string outputpath) {try {person [] arr = new person [] {new person (" egg ", 22 ), new person ("niu", 21) }; list <person> List = arrays. aslist (ARR); // convert the array to listdocument Doc = generatexml (list); // generate the XML file outputxml (Doc, outputpath ); // output the file to the specified path} Ca Tch (exception e) {system. err. println ("exception ");}} /*** output the XML file to the specified path * @ Param Doc * @ Param filename * @ throws exception */Private Static void outputxml (document DOC, string filename) throws exception {transformerfactory TF = transformerfactory. newinstance (); transformer = TF. newtransformer (); domsource source = new domsource (DOC); transformer. setoutputproperty (outputkeys. encoding, "UTF-8 "); Transformer. setoutputproperty (outputkeys. indent, "yes"); // set the line feed and indentation of the document. printwriter PW = new printwriter (New fileoutputstream (filename); streamresult result = new streamresult (PW); transformer. transform (source, result); system. out. println ("XML file generated successfully! ");}/*** Generate the XML file * @ Param list * @ return */public static document generatexml (list <person> List) {document DOC = NULL; element root = NULL; try {documentbuilderfactory factory = documentbuilderfactory. newinstance (); documentbuilder builder = factory. newdocumentbuilder (); Doc = builder. newdocument (); root = Doc. createelement ("person"); Doc. appendchild (Root);} catch (exception e) {e. printstacktrace (); return NULL; // if an exception occurs, it will not be executed.} int Len = List. size (); element; For (INT I = 0; I <Len; I ++) {person = List. get (I); element = Doc. createelement ("person" + (I + 1); element. setattribute ("Age", "" + person. getage (); element. setattribute ("name", person. getname (); root. appendchild (element) ;}return Doc ;}}
Iv. xml verification: DTD and XSD
Updating...