XML Section1. What are the parsing techniques of XML? What is the difference?
A: There are dom,sax,stax, etc.
DOM: When working with large files, its performance drops very badly. This problem is caused by the tree structure of the DOM, which takes up more memory, and the DOM has to load the entire document into memory before parsing the file, which is appropriate for random access to the XML sax: Dom,sax is an event-driven XML parsing method. It reads the XML file sequentially and does not need to load the entire file at once. When it encounters a file beginning with a document ending, or the beginning of a label with the end of the label, it triggers an event that the user processes the XML file by writing the processing code in its callback event, which is appropriate for sequential access to the XML
Stax:streaming API for XML (STAX)
Explaining these differences is not a special comparison, just like talking about the difference between the podcast and other training institutions, we just have to say clearly what the characteristics and advantages of the Intelligence podcast, which has indirectly answered each other's differences.
2 , what aspects of XML technology do you use in your project?
A: The use of data storage, information configuration two aspects. In the data exchange platform, the data source can not be assembled into an XML file, and then the XML file is compressed and packaged and encrypted over the network to the receiver, receive decryption and decompression, and then the corresponding information in the XML file for processing. In the software configuration, the use of XML can be easily carried out, the various configuration parameters of the software are stored in the XML file.
3 , how to solve the Chinese problem when parsing xml file with Jdom?
A: look at the following code to resolve it by encoding
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 ("Teacher");
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 , programming the way in which Java parses XML.
A: Parsing the Xml,xml file in sax is as follows:
<?xml version=1.0 encoding=gb2312?>
<person>
<name> Wang Xiaoming </name>
<college> Information College </college>
<telephone>6258113</telephone>
<notes> Male, 1955, Ph. D. in 95, Hainan University </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);
}
}
JSP content display source code, SAXXML.JSP:
<HTML>
<HEAD>
<TITLE> Parsing XML Files 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 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 (<table border=2><caption> Teacher Information sheet </CAPTION>);
Out.println (<TR><TD> name </TD> + <TD> +
(string) Hashtable.get (new string (name)) + </TD></TR>);
Out.println (<TR><TD> college </TD> + <TD> +
(String) Hashtable.get (new String (College)) +</td></tr>);
OUT.PRINTLN (<TR><TD> phone </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 , there are several forms of XML document definition? What are the essential differences between them? What are the different ways to parse an XML document?
A: Two forms of DTD schema,
B: The essential difference: The schema itself is XML and can be parsed by the XML parser (which is also the fundamental purpose of developing schema from the DTD), C: dom,sax,stax, etc.
DOM: When working with large files, its performance drops very badly. This problem is caused by the tree structure of the DOM, which takes up more memory, and the DOM has to load the entire document into memory before parsing the file, which is appropriate for random access to the XML SAX: Dom,sax is an event-driven XML parsing method. It reads the XML file sequentially and does not need to load the entire file at once. When it encounters a file beginning with the end of a document, or at the end of a label, it triggers an event where the user processes the XML file by writing the processing code in its callback event, which is suitable for sequential access to XML stax:streaming API for XML (STAX)
The XML part of the Java pen question