XML for the Java family

Source: Internet
Author: User

XML is an Extensible markup language that has been studied before learning HTML, and why should it be brought up here today? Because XML is one of the 13 specifications of the Java EE.

Re-review

Extensible Markup Language, a subset of standard generic markup languages, is a markup language used to tag electronic files to make them structurally.
In electronic computers, marking refers to the information symbols that can be understood by computers, which can be used to deal with various kinds of information such as articles. It can be used to tag data, define data types, and is a source language that allows users to define their own markup language. It is ideal for world Wide Web transport, providing a unified approach to describing and exchanging structured data that is independent of the application or vendor. is a cross-platform, content-dependent technology in the Internet environment and an effective tool for dealing with distributed structure information today. As early as 1998, the XML1.0 specification was released and used to simplify the transmission of document information over the Internet.

Deepen your understanding of what is Extensible Markup language?

Extensible Markup Language is a markup language that is much like a Hypertext markup language.
It is designed to transmit data instead of displaying data.
Its label is not pre-defined. You need to define your own labels.
It is designed to be self-descriptive.
It is a recommended standard for the consortium.

Ii. differences between extensible Markup language and Hypertext Markup language

It is not an alternative to Hypertext Markup language.
It complements the Hypertext Markup Language.

It and the Hypertext Markup language are designed for different purposes:

It is designed to transmit and store data, with the focus on the content of the data.
Hypertext Markup Language is designed to display data, focusing on the appearance of the data.
Hypertext Markup Language is designed to display information, and it is designed to transmit information.

The best description of it is:

It is a software and hardware-independent information transfer tool.

Two methods of parsing

There are two basic ways of parsing XML, one called Sax and the other called Dom. Sax is based on the parsing of event streams, and Dom is based on the parsing of XML document tree structure. Suppose we have the following XML content and structure:

    <?xml version="1.0" encoding="UTF-8"?>     <employees>       <employee>       <name>ddviplinux</name>       <sex>m</sex>       <age>30</age>       </employee>       </employees>
1.DOM Generating and parsing XML documents

Defines a set of interfaces for the parsed version of an XML document. The parser reads the entire document and then constructs a tree structure that resides in memory, and the code can then manipulate the tree structure using the DOM interface. Advantages: The entire document tree in memory, easy to operate, support delete, modify, rearrange and other functions; disadvantage: The entire document into memory (including useless nodes), waste time and space; Use cases: Once the document is parsed, the data must be accessed multiple times, and the hardware resources are sufficient (memory, CPU).

 Public void Parserxml(String fileName) {Try{Documentbuilderfactory dbf = documentbuilderfactory.newinstance ();           Documentbuilder db = Dbf.newdocumentbuilder ();           Document document = Db.parse (FileName); NodeList employees = Document.getchildnodes (); for(inti =0; I < employees.getlength ();               i++) {Node employee = Employees.item (i); NodeList EmployeeInfo = Employee.getchildnodes (); for(intj =0; J < Employeeinfo.getlength ();                   J + +) {node node = Employeeinfo.item (j); NodeList Employeemeta = Node.getchildnodes (); for(intK =0; K < Employeemeta.getlength (); k++) {System. out. println (Employeemeta.item (k). Getnodename () +":"+ Employeemeta.item (k). Gettextcontent ()); }}} System. out. println ("Parse Complete"); }Catch(Exception e) {System. out. println (E.getmessage ()); }  }
2.SAX Generating and parsing XML documents

Sax has emerged to solve the DOM problem. SAX, event-driven. When the parser discovers the start of an element, the end of an element, text, the beginning or end of a document, and so on, the programmer writes the code that responds to these events and saves the data. Pros: No need to transfer the entire document in advance, consume less resources, SAX parser code is smaller than DOM parser code, suitable for applets, download. Cons: not persistent; After the event, if the data is not saved, then the data is lost, stateless, only the text can be obtained from the event, but the text is not known which element; use case: Applet; only a small amount of XML documents, little back access, less machine memory;

publicvoidparserXml(String fileName) {               SAXParserFactory saxfac = SAXParserFactory.newInstance();       try {                SAXParser saxparser = saxfac.newSAXParser();                isnew FileInputStream(fileName);                saxparser.parse(isnew MySAXHandler());           catch (Exception e) {               e.printStackTrace();           }       }  

XML for the Java family

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.