Instructions on XML Technology

Source: Internet
Author: User

Instructions on XML Technology

1. What parsing technologies does xml provide? What is the difference?

2. What aspects of xml technology have you used in your project? How to implement it?

3. How can I solve the Chinese problem when parsing xml files using jdom? How to resolve it?

4. Use JAVA to parse XML for programming.

5. What parsing technologies does xml provide? What is the difference?

6. Functions and usage of XML!

A: DOM, SAX, STAX, etc.
DOM: when processing large files, its performance drops sharply. This problem is caused by the DOM tree structure. This structure occupies a large amount of memory and the DOM must load the entire file into the memory before parsing the file. It is suitable for Random Access to the XML: unlike DOM, SAX is an event-driven XML parsing method. It reads XML files sequentially without loading the entire file at a time. When a file starts with a file, the document ends, or the tag starts with a tag, it triggers an event. You can write the processing code in the callback event to process the XML file, suitable for sequential access to xml stax: Streaming API for XML (StAX)

2. What aspects of xml technology have you used in your project? How to implement it?

A: data storage and information configuration are used. On the Data Exchange Platform, assemble data that cannot be used as data sources into XML files, compress and compress the XML files, compress the files, and encrypt the files, and send them to the receiver over the network, after receiving the decryption and decompression, restore the relevant information in the XML file for processing. XML can be easily used for software configuration. Various configuration parameters of the software are stored in XML files.

 

 

3. How can I solve the Chinese problem when parsing xml files using jdom? How to resolve it?

A: Check the following code and use the encoding method to solve the problem.
 

Package test; import java. io. *; public class DOMTest {private String inFile = c: \ people. xml; private String outFile = c: \ people. xml; public static void main (String args []) {new DOMTest ();} public DOMTest () {try {javax. xml. parsers. documentBuilder builder = javax. xml. parsers. documentBuilderFactory. newInstance (). newDocumentBuilder (); org. w3c. dom. document doc = builder. newDocument (); org. w3c. dom. element root = doc. createElement_x (Instructor); org. w3c. dom. element wang = doc. createElement_x (Wang); org. w3c. dom. element liu = doc. createElement_x (Liu); wang. appendChild (doc. createTextNode (I am Miss Wang); root. appendChild (wang); doc. appendChild (root); javax. xml. transform. transformer transformer = javax. xml. transform. transformerFactory. newInstance (). newTransformer (); transformer. setOutputProperty (javax. xml. transform. outputKeys. ENCODING, gb2312); transformer. setOutputProperty (javax. xml. transform. outputKeys. INDENT, yes); transformer. transform (new javax. xml. transform. dom. DOMSource (doc), new javax. xml. transform. stream. streamResult (outFile);} catch (Exception e) {System. out. println (e. getMessage ());}}}

 

 

 

 

 

4. Use JAVA to parse XML for programming.
A: Parse XML in the SAX mode. The XML file is as follows:

Wang Xiaoming
Information Institute
6258113
Male, born in 1955, Doctor, transferred to Hainan University in 95 years

Event Callback class SAXHandler. java
 

 import java.io.*;  import java.util.Hashtable;  import org.xml.sax.*;  public class SAXHandler extends HandlerBase  {   private Hashtable table = new Hashtable();   private String currentElement = null;   private String currentValue = null;   public void setTable(Hashtable table)   {    this.table = table;   }   public Hashtable getTable()   {    return table;   }   public void startElement(String tag, AttributeList attrs)   throws SAXException   {    currentElement = tag;   }   public void characters(char[] ch, int start, int length)   throws SAXException   {   currentValue = new String(ch, start, length);   }   public void endElement(String name) throws SAXException   {    if (currentElement.equals(name))     table.put(currentElement, currentValue);   }  } 

 

 

 

Source code of JSP content display, SaxXml. jsp:

 <% @ Page errorPage = ErrPage. jsp contentType = text/html; charset = GB2312 %> <% @ page import = java. io. * %> <% @ page import = java. util. hashtable %> <% @ page import = org. w3c. dom. * %> <% @ page import = org. xml. sax. * %> <% @ page import = javax. xml. parsers. SAXParserFactory %> <% @ page import = javax. xml. parsers. SAXParser %> <% @ page import = SAXHandler %> <% File file = new File (c: people. xml); FileReader reader = new FileReader (file); Parser parser; SAXParserFactory spf = SAXParserFactory. newInstance (); SAXParser sp = spf. newSAXParser (); SAXHandler handler = new SAXHandler (); sp. parse (new InputSource (reader), handler); Hashtable hashTable = handler. getTable (); out. println (); out. println (+); out. println (+); out. println (+); out. println (+); out. println ( 
Instructor information table
Name + (String) hashTable. get (new String (name) +
School + (String) hashTable. get (new String (college) +
Phone number + (String) hashTable. get (new String (telephone) +
Remarks + (String) hashTable. get (new String (notes) +
); %>

 

 

 

 

The role and purpose of XML!

1. Data Exchange
XML uses elements and attributes to describe data. During data transmission, XML always retains data structures such as parent/child relationships. Several applications can share and parse the same XML file without having to use the traditional String Parsing or disassembling process. On the contrary, normal files do not describe each data segment (except in the header file), and do not retain the data link structure. Using XML for data exchange can make the application more flexible, because XML data can be accessed by location (the same as a common file) or element name (from the database.
2. Web Services
Web services are one of the most exciting revolutions that allow people using different systems and programming languages to stream and share data with each other. The Foundation is that the Web server uses XML to exchange data between systems. Exchange data can be standardized and consistent with common XML tags, for example, on the Simple Object Access Protoc ol (SOAP) platform. SOAP can transmit messages between objects constructed in different programming languages. This means that a C # object can communicate with a Java object. This communication can even happen between objects running on different operating systems. DCOM, CORBA, or Java RMI can only transmit messages between tightly coupled objects, while SOAP can transmit messages between loosely coupled objects.
3. Content Management
XML uses only elements and attributes to describe data, rather than data display methods. In this way, XML provides an excellent way to mark content independent of platform and language. Using languages like XSLT, you can easily convert XML files into various formats, such as HTML, WML, PDF, flat file, EDI, and so on. XML has the ability to run on different system platforms and convert to different formats of target files, making it an excellent choice in the content management application system.
4. Web Integration
More and more devices now support XML. This allows Web developers to use XML to transmit data between personal e-assistants and browsers. Why is XML text directly sent to such a device? The purpose of this operation is to allow users to learn more about the data display mode and experience the joy of practice. The conventional CLIENT/service (C/S) method must send an application to the server to obtain data sorting or change the display format. XML can directly process data, you do not have to go through a two-way "Journey" such as submitting a query to the server. You do not need to prepare a database on the device. You can even modify the XML file on the device and return the result to the server. Imagine how a refrigerator with the Internet feature and XML support will have a big impact on the market. You don't have to get up early to get milk
Now!
5. Preparation
Many applications store the prepared data in various files, such as. ini files. Although such a file format has been used for many years and has been very useful, XML still marks the preparation of data for the application in a better way. Use classes in. NET, such as XmlDocument and XmlTextReader, to mark the prepared data as an XML format to make it more readable and easily integrated into the application system. Applications that use XML file preparation can easily process the required data, and do not need to be re-compiled as other applications to modify and maintain the application system. As mentioned above, the five ways to use XML do not cover all scenarios.

 

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.