Use the XML files to be used as follows: Examplea.xml
XML Version = "1.0" encoding = "GB2312"?>
book>
book>
booklist>
xml-stylesheet href = "booklist.html.xsl" type = "text / xsl"?>
The next is to call these two JavaBean implementations to the XML operation, the code of the JSP file is as follows:
<% @ Page ContentType = "text / html; charset = GB2312"%>
<% @ page import = "org.jdom. *"%>
<% @ page import = "org.jdom.output. *"%>
<% @ page import = "org.jdom.input. *"%>
<% @ Page Import = "java.io. *"%>
<% @ Page Import = "java.util. *"%>
<% @ page import = "xml. *"%>
<%
// XML file path (absolute path) string filename = "examplea.xml";
String aa = getServletContext (). GetRealPath ("/") "jdom //";
String trace = aa filename;
// Initialization Read and write bean
Xml.readxml readxmlbean = new xml.readxml ();
Xml.writexml writexmlbean = new xml.writexml ();
// Get relevant data from the XML file
Document doc;
Readxmlbean.readXML (Trace);
Doc = readxmlbean.getxmldoc ();
/ / Add a processing instruction
Processinginstruction PI = New ProcessinginstructionTRuction
("XML-Stylesheet", "HREF = /" BookList.html.xsl / "Type = /" text / xsl / ");
Doc.addContent (pi);
// Get the root element
Element root = doc.getrootelEment ();
// Get a collection of root elements all child elements
Java.util.list books = root.getchildren ();
// Get the first BOOK element
Element book = (element) books.get (0);
/ / Add a property for the first book
Attribute A = New Attribute ("Hot1", "True");
Book.setttribute (a);
/ / Get the specified word element
Element author = book.getchild ("author");
/ / Change the author to Wang 5
Author.Settext ("Wang 5");
/ / Get the specified word element
ELEMENT price = book.getchild ("price");
//Modify the price
Price.Settext (float.tostring (50.0f));
// A stack display all elements
Iterator it = books.iterator ();
While (it.hasnext ()) {
ELEMENT E = (ELEMENT) IT.NEXT ();
Out.println (E.GETCHILD ("name"). getTextTrim () "
");
List priceelements = E.GETCHILDREN ("price");
Itrator it2 = priceelements.Item ();
While (it2.hasnext ()) {
ELEMENT PE = (Element) it2.next ();
Out.println (PE.GetaTributevalue ("currency") "
");
Out.println (Pe.GetaTributevalue ("Amount") "
");
}
}
// instruction operation
String target = pi.gettarget ();
String Data = pi.getdata ();
String type = pi.getValue ("type"); out.println (target "
" "
" Type "
");
// Delete properties
Book.removettribute ("hot");
// Delete the instruction
Doc.removeContent (PI);
// New node
Element add = new element; "add");
A = New Attribute ("SKU", "123456");
Add.setttribute (a);
Add.addContent ("add");
Book.addContent (add);
// There is no content that only attributes
Element attr = new element ("attribute");
A = New Attribute ("SKU", "123456");
Attr.setttribute (a);
A = New Attribute ("SF234", "123456");
Attr.setttribute (a);
Book.addContent (attr);
// Delete the node
Book.removecontent (attr);
Book.removeContent (add);
// Write XML file
Writexmlbean.writexml (DOC, TRACE);
%>
This example includes general addition, deletion, modifying node, and attributes, and general XML file operations such as display XML data. If it is just a pure display of data, it is recommended to operate all the data to the Javabean. The JSP page is just displayed, which can improve the speed of the run, reduce the probability of error :)