Use JDOM to process XML data XSLT articles (2)
We use a JavaBean to implement the formatting of the XML document.
Xsltfilter.java
Package XML;
Import java.io. *;
Import java.util. *;
Import Org.jdom. *;
Import Org.jdom.Output. *;
Import org.jdom.transform. *;
Import javax.xml.transform. *;
Import javax.xml.transform.stream. *;
Public class xsltfilter {
PRIVATE DOCUMENT OUTPUTDOC;
Private string stylesheetpath;
PRIVATE DOCUMENT DOC = NULL;
Public xsltfilter () {}
Public void setputdoc (Document S) {
this.outputdoc = S;
}
Public void setStypath (string s) {
THIS.StylesheetPath = S;
}
Public void startfilter () {
Try {
TransformerFactory myfactory = transformerfactory.newinstance ();
TRANSFORMER MyTransformer = MyFactory.NewTransformer (New StreamSource (StylesheetPath);
JDOMRESULT RESULT = New JDOMRESULT ();
MyTransformer.Transform (New JDomsource (Outputdoc), Result;
DOC = result.getdocument ();
} catch (exception e) {
System.out.println ("Error Was:" E.GetMessage ());
}
}
Public void destroy () {}
Public document getXmldoc () {
Return (DOC);
}
}
This JavaBean enters a JDOM document format, enters the location of an XSLT document, and outputs a formatted document.
Then use a JSP to call this Javabean
<% @ 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. *"%>
<%
String filename = "watchlist.xml";
String aa = getServletContext (). GetRealPath ("/") "jdom //";
String trace = aa filename;
String stylename = "watchlist.xsl"; string style = aa stylename;
String resultname = "aa.html";
String resulttrace = aa resultname;
Xml.readxml readxmlbean = new xml.readxml ();
Document doc;
Readxmlbean.readXML (Trace);
Doc = readxmlbean.getxmldoc ();
Xml.xsltfilter xsltfilter = new xml.xsltfilter ();
Xsltfilter.setOutputDoc (DOC);
Xsltfilter.SetStylePath (Styletrace);
Xsltfilter.startfilter ();
DOC = xsltfilter.getxmldoc ();
Xml.writexml writexmlbean = new xml.writexml ();
Writexmlbean.writexml (DOC, Resulttrace);
%>
See the READXML and WRITEXML used in this article:
http://www.9cbs.net/develop/read_article.asp?id=20720