1. What parsing technologies does XML provide? What is the difference?
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)
There is no need to compare these differences. Just like talking about the differences between Chuanzhi podcast and other training institutions, we only need to clarify the characteristics and advantages of Chuanzhi podcast, this has indirectly answered the differences between them.
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 (" ");
Org. W3C. Dom. Element Wang = Doc. createelement ("King ");
Org. W3C. Dom. Element Liu = Doc. createelement ("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:
<? XML version = 1.0 encoding = gb2312?>
<Person>
<Name> Wang Xiaoming </Name>
<College> Information College </College>
<Telephone> 6258113 </telephone>
<Notes> male, born in 1955, Doctor, transferred to Hainan University in 95 years </Notes>
</Person>
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:
<HTML>
<Head>
<Title> analyze the XML file people. xml </title>
</Head>
<Body>
<% @ 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;
Saxparserfactory SPF = saxparserfactory. newinstance ();
Saxparser sp = SPF. newsaxparser ();
Saxhandler handler = new saxhandler ();
Sp. parse (New inputsource (Reader), Handler );
Hashtable = handler. gettable ();
Out. println (<Table border = 2> <caption> instructor information table </caption> );
Out. println (<tr> <TD> name </TD> + <TD> +
(String) hashtable. Get (new string (name) + </TD> </tr> );
Out. println (<tr> <TD> school </TD> + <TD> +
(String) hashtable. Get (new string (college) + </TD> </tr> );
Out. println (<tr> <TD> phone number </TD> + <TD> +
(String) hashtable. Get (new string (telephone) + </TD> </tr> );
Out. println (<tr> <TD> remarks </TD> + <TD> +
(String) hashtable. Get (new string (notes) + </TD> </tr> );
Out. println (</table> );
%>
</Body>
</Html>
5. How many forms does the XML document define? What are the essential differences between them? How can I Parse XML documents?
A: Two forms of DTD schema,
B: essential difference: the schema itself is XML and can be parsed by the XML Parser (this is also the fundamental purpose of developing the schema from the DTD). C: There are 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)