Application of Name Space in XML
We may have written this simple XML document:
XML Version = "1.0" encoding = "GB2312"?>
Teacher>
Data Structure Course>
We can find that the above XML document element is relatively simple, and the structure is relatively simple, it is better to achieve, but when we want to make this structure more more complex, if you add this to this school When this class, which is the class, etc., I have to solve the element by adding elements, so it is necessary to increase the hierarchical structure. It is very inconvenient. So what is the way to solve this problem?
The answer is that we can solve this problem through the namespace.
So maybe someone will ask: "What is the name space?". The English name of the name space is NameSpace, and the as the name is to allow you to identify your label through a URL. E.g:
Where XMLNs is an abbreviation of XHTML Namespace, called "Name Space" declaration.
Why use the namespace? Because we all know that XML is allowed to customize the label, so it is possible that the label we define is the same, but the meaning of the representation may be different. Errors are easily generated when file exchange or sharing.
For example, Zhang San and Li Si defined a "label of school>, Zhang San's" school "tag refers to Beihang, Li Si's
So how do you implement the following XML documentation through the namespace?
XML Version = "1.0" encoding = "GB2312"?>
Teacher: Data Structure Course>
University: Teacher>
The source program is as follows:
Package XML_Study;
Import Org.jdom. *;
Import org.jdom.output.xmloutputter;
Import java.math.biginteger;
Import java.io.ioException;
Public class prefixedfibonacci {
Public prefixedfibonacci () {
}
Public static void main (String [] args) {
Element root = new element ("teacher", "university", "http://www.beihang.com");
// Refers to the teacher of Beihang
Element Mrow = New Element ("Data Structure Course", "Teacher", "http://www.beihang.com/computer/zhang"); // Refers to Zhang Teacher Zhang, North Airlines
Element mi = new element ("title", "teacher", "http://www.beihang.com/computer/zhang");
Mi.Settext ("Professor");
MROW.ADDCONTENT (MI);
Element Mo = New Element ("Name", "Teacher", "http://www.beihang.com/computer/zhang");
Mo.setText ("Teacher Zhang");
MROW.ADDCONTENT (MO);
Element mm = new element ("Phone", "Teacher", "http://www.beihang.com/computer/zhang");
Mm.Settext ("98989988");
Mrow.AddContent (mm);
Root.addContent (MROW);
Document Doc = New Document (root);
Try {
XMloutPutter Serializer = New Xmloutputter (", TRUE);
Serializer.setencoding ("GB2312");
Serializer.Output (DOC, System.out);
}
Catch (IOException E) {
System.err.Print (e);
}
}
}
The program succeeded in JBuilder9 and Tomcat4.0.
The basic JDOM rules for the namespace are: When the element or attribute is in the namespace instead of specifying a fully qualified name, you must specify local name, prefix, and URI in turn. If the element is in the default namespace, the prefix is omitted.