用JAVA能做到吗?请教!

02-09-20 xn127
以前都是用JAVASCRIPT写用XSL显示XML:
var xml = new ActiveXObject("Microsoft.XMLDOM")
var XMLDom=form1.XMLDom.value
xml.async = false
xml.loadXML(XMLDom)
// Load the XSL
var xsl = new ActiveXObject("Microsoft.XMLDOM")
xsl.async = false
xsl.load("xsl/list1.xsl")
// Transform
document.write(xml.transformNode(xsl))

不知道用JAVA能否做到这点?

浆糊
2002-09-20 15:16
可以,一般的xml处理包都可以.apache xerce,jdom

xn127
2002-09-23 10:02
我也知道有关于这方面的包可以做,只是没用过,试着写了,但好象都不能正确显示出来,想看看有哪位写过的,可以指点指点!

浆糊
2002-09-23 10:32
什么错误阿,不说具体点怎么知道什么问题啊。
我写过一些

xn127
2002-09-23 15:31
那你能否把你写的代码给我看看呢?我用的是XALAN的包做的,但因为它现在的版本升级了,所以要重新写,想看看你是怎么做的,谢谢!我现在是这样的:在SERVLET程序中先通过一系列数据库的操作得到一个XML数据,然后用已经写好的相应的XSL文件把它显示到客户端。

浆糊
2002-09-23 15:34
你试试过没有啊,如果有请把错误贴出来。我现在手头没有这样的代码

alex_wong2
2002-09-23 16:12
1,开发环境,我用的是jb7.
2,设置jdk,我用的是jdk1.4
3,下载jdom. 或者在bang的jlive代码的lib中,包括进来。
4,由于代码屏蔽,我在jsp和xml中 %替换为jsp:

代码如下:
/*
代码说明
包的结构如下:
com.my.util.xml
要读的xml文件放在
WEB-INF/classes/下
在写的时候,在win下和unix下的写法不一样。
请看jlive的说明。
*/


DataUnformatFilter.java


package com.my.util.xml;

import java.util.Stack;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;


/**
* Filter for removing formatting from data- or field-oriented XML.
*
* <i>Code and comments adapted from DataWriter-0.2, written
* by David Megginson and released into the public domain,
* without warranty.</i>
*
* <p>This filter removes leading and trailing whitespace from
* field-oriented XML without mixed content. Note that this class will
* likely not yield appropriate results for document-oriented XML like
* XHTML pages, which mix character data and elements together.</p>
*
* @see DataFormatFilter
*/
class DataUnformatFilter extends XMLFilterBase
{

////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////

/**
* Create a new filter.
*/
public DataUnformatFilter()
{
}

/**
* Create a new filter.
*
* <p>Use the XMLReader provided as the source of events.</p>
*
* @param xmlreader The parent in the filter chain.
*/
public DataUnformatFilter(XMLReader xmlreader)
{
super(xmlreader);
}

////////////////////////////////////////////////////////////////////
// Public methods.
////////////////////////////////////////////////////////////////////

/**
* Reset the filter so that it can be reused.
*
* <p>This method is especially useful if the filter failed
* with an exception the last time through.</p>
*/
public void reset ()
{
state = SEEN_NOTHING;
stateStack = new Stack();
whitespace = new StringBuffer();
}

////////////////////////////////////////////////////////////////////
// Methods from com.jivesoftware.sax.ContentHandler.
////////////////////////////////////////////////////////////////////

/**
* Filter a start document event.
*
* <p>Reset state and pass the event on for further processing.</p>
*
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartDocument
*/
public void startDocument ()
throws SAXException
{
reset();
super.startDocument();
}

/**
* Filter a start element event.
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @param qName The element's qualified (prefixed) name.
* @param atts The element's attribute list.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartElement
*/
public void startElement (String uri, String localName,
String qName, Attributes atts)
throws SAXException
{
clearWhitespace();
stateStack.push(SEEN_ELEMENT);
state = SEEN_NOTHING;
super.startElement(uri, localName, qName, atts);
}

/**
* Filter an end element event.
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @param qName The element's qualified (prefixed) name.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerendElement
*/
public void endElement (String uri, String localName, String qName)
throws SAXException
{
if (state == SEEN_ELEMENT) {
clearWhitespace();
} else {
emitWhitespace();
}
state = stateStack.pop();
super.endElement(uri, localName, qName);
}

/**
* Filter a character data event.
*
* @param ch The characters to write.
* @param start The starting position in the array.
* @param length The number of characters to use.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlercharacters
*/
public void characters (char ch[], int start, int length)
throws SAXException
{
if (state != SEEN_DATA) {

/* Look for non-whitespace. */
int end = start + length;
while (end-- > start) {
if (!isXMLWhitespace(ch[end]))
break;
}

/*
* If all the characters are whitespace, save them for later.
* If we've got some data, emit any saved whitespace and update
* our state to show we've seen data.
*/
if (end < start) {
saveWhitespace(ch, start, length);
} else {
state = SEEN_DATA;
emitWhitespace();
}
}

/* Pass on everything inside a data field. */
if (state == SEEN_DATA) {
super.characters(ch, start, length);
}
}

/**
* Filter an ignorable whitespace event.
*
* @param ch The array of characters to write.
* @param start The starting position in the array.
* @param length The number of characters to write.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandler#ignorableWhitespace
*/
public void ignorableWhitespace (char ch[], int start, int length)
throws SAXException
{
emitWhitespace();
// ignore
}

/**
* Filter a processing instruction event.
*
* @param target The PI target.
* @param data The PI data.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandler#processingInstruction
*/
public void processingInstruction (String target, String data)
throws SAXException
{
emitWhitespace();
super.processingInstruction(target, data);
}

////////////////////////////////////////////////////////////////////
// Internal methods.
////////////////////////////////////////////////////////////////////

/**
* Saves trailing whitespace.
*/
protected void saveWhitespace (char[] ch, int start, int length) {
whitespace.append(ch, start, length);
}

/**
* Passes saved whitespace down the filter chain.
*/
protected void emitWhitespace ()
throws SAXException
{
char[] data = new char[whitespace.length()];
if (whitespace.length() > 0) {
whitespace.getChars(0, data.length, data, 0);
whitespace.setLength(0);
super.characters(data, 0, data.length);
}
}

/**
* Discards saved whitespace.
*/
protected void clearWhitespace () {
whitespace.setLength(0);
}

/**
* Returns <var>true</var> if character is XML whitespace.
*/
private boolean isXMLWhitespace (char c)
{
return c == ' ' || c == '\t' || c == '\r' || c == '\n';
}

////////////////////////////////////////////////////////////////////
// Constants.
////////////////////////////////////////////////////////////////////

private static final Object SEEN_NOTHING = new Object();
private static final Object SEEN_ELEMENT = new Object();
private static final Object SEEN_DATA = new Object();


////////////////////////////////////////////////////////////////////
// Internal state.
////////////////////////////////////////////////////////////////////

private Object state = SEEN_NOTHING;
private Stack stateStack = new Stack();

private StringBuffer whitespace = new StringBuffer();
}




XMLFilterBase.java
package com.my.util.xml;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.XMLFilterImpl;


/**
* Adds convenience methods to base SAX2 Filter implementation.
*
* <i>Code and comments adapted from XMLWriter-0.2, written
* by David Megginson and released into the public domain,
* without warranty.</i>
*
* <p>The convenience methods are provided so that clients do not have to
* create empty attribute lists or provide empty strings as parameters;
* for example, the method invocation</p>
*
* <pre>
* w.startElement("foo");
* </pre>
*
* <p>is equivalent to the regular SAX2 ContentHandler method</p>
*
* <pre>
* w.startElement("", "foo", "", new AttributesImpl());
* </pre>
*
* <p>Except that it is more efficient because it does not allocate
* a new empty attribute list each time.</p>
*
* <p>In fact, there is an even simpler convenience method,
* <var>dataElement</var>, designed for writing elements that
* contain only character data.</p>
*
* <pre>
* w.dataElement("greeting", "Hello, world!");
* </pre>
*
* <p>is equivalent to</p>
*
* <pre>
* w.startElement("greeting");
* w.characters("Hello, world!");
* w.endElement("greeting");
* </pre>
*
* @see com.jivesoftware.sax.helpers.XMLFilterImpl
*/
class XMLFilterBase extends XMLFilterImpl
{

////////////////////////////////////////////////////////////////////
// Constructors.
////////////////////////////////////////////////////////////////////

/**
* Construct an XML filter with no parent.
*
* <p>This filter will have no parent: you must assign a parent
* before you start a parse or do any configuration with
* setFeature or setProperty.</p>
*
* @see com.jivesoftware.sax.XMLReadersetFeature
* @see com.jivesoftware.sax.XMLReadersetProperty
*/
public XMLFilterBase()
{
}

/**
* Create an XML filter with the specified parent.
*
* <p>Use the XMLReader provided as the source of events.</p>
*
* @param xmlreader The parent in the filter chain.
*/
public XMLFilterBase(XMLReader parent)
{
super(parent);
}

////////////////////////////////////////////////////////////////////
// Convenience methods.
////////////////////////////////////////////////////////////////////

/**
* Start a new element without a qname or attributes.
*
* <p>This method will provide a default empty attribute
* list and an empty string for the qualified name.
* It invokes {@link
* startElement(String, String, String, Attributes)}
* directly.</p>
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartElement
*/
public void startElement (String uri, String localName) throws SAXException
{
startElement(uri, localName, "", EMPTY_ATTS);
}

/**
* Start a new element without a qname, attributes or a Namespace URI.
*
* <p>This method will provide an empty string for the
* Namespace URI, and empty string for the qualified name,
* and a default empty attribute list. It invokes
* startElement(String, String, String, Attributes)}
* directly.</p>
*
* @param localName The element's local name.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartElement
*/
public void startElement (String localName) throws SAXException
{
startElement("", localName, "", EMPTY_ATTS);
}

/**
* End an element without a qname.
*
* <p>This method will supply an empty string for the qName.
* It invokes {@link endElement(String, String, String)}
* directly.</p>
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerendElement
*/
public void endElement (String uri, String localName) throws SAXException
{
endElement(uri, localName, "");
}

/**
* End an element without a Namespace URI or qname.
*
* <p>This method will supply an empty string for the qName
* and an empty string for the Namespace URI.
* It invokes {@link endElement(String, String, String)}
* directly.</p>
*
* @param localName The element's local name.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerendElement
*/
public void endElement (String localName) throws SAXException
{
endElement("", localName, "");
}

/**
* Add an empty element.
*
* Both a {@link startElement startElement} and an
* {@link endElement endElement} event will be passed on down
* the filter chain.
*
* @param uri The element's Namespace URI, or the empty string
* if the element has no Namespace or if Namespace
* processing is not being performed.
* @param localName The element's local name (without prefix). This
* parameter must be provided.
* @param qName The element's qualified name (with prefix), or
* the empty string if none is available. This parameter
* is strictly advisory: the writer may or may not use
* the prefix attached.
* @param atts The element's attribute list.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartElement
* @see com.jivesoftware.sax.ContentHandlerendElement
*/
public void emptyElement (String uri, String localName, String qName,
Attributes atts) throws SAXException
{
startElement(uri, localName, qName, atts);
endElement(uri, localName, qName);
}

/**
* Add an empty element without a qname or attributes.
*
* <p>This method will supply an empty string for the qname
* and an empty attribute list. It invokes
* {@link emptyElement(String, String, String, Attributes)}
* directly.</p>
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see emptyElement(String, String, String, Attributes)
*/
public void emptyElement (String uri, String localName) throws SAXException
{
emptyElement(uri, localName, "", EMPTY_ATTS);
}

/**
* Add an empty element without a Namespace URI, qname or attributes.
*
* <p>This method will supply an empty string for the qname,
* and empty string for the Namespace URI, and an empty
* attribute list. It invokes
* {@link emptyElement(String, String, String, Attributes)}
* directly.</p>
*
* @param localName The element's local name.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see emptyElement(String, String, String, Attributes)
*/
public void emptyElement (String localName) throws SAXException
{
emptyElement("", localName, "", EMPTY_ATTS);
}

/**
* Add an element with character data content.
*
* <p>This is a convenience method to add a complete element
* with character data content, including the start tag
* and end tag.</p>
*
* <p>This method invokes
* {@link @see com.jivesoftware.sax.ContentHandlerstartElement},
* followed by
* {@link characters(String)}, followed by
* {@link @see com.jivesoftware.sax.ContentHandlerendElement}.</p>
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @param qName The element's default qualified name.
* @param atts The element's attributes.
* @param content The character data content.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartElement
* @see characters(String)
* @see com.jivesoftware.sax.ContentHandlerendElement
*/
public void dataElement (String uri, String localName, String qName,
Attributes atts, String content) throws SAXException
{
startElement(uri, localName, qName, atts);
characters(content);
endElement(uri, localName, qName);
}

/**
* Add an element with character data content but no attributes.
*
* <p>This is a convenience method to add a complete element
* with character data content, including the start tag
* and end tag. This method provides an empty string
* for the qname and an empty attribute list.</p>
*
* <p>This method invokes
* {@link @see com.jivesoftware.sax.ContentHandlerstartElement},
* followed by
* {@link characters(String)}, followed by
* {@link @see com.jivesoftware.sax.ContentHandlerendElement}.</p>
*
* @param uri The element's Namespace URI.
* @param localName The element's local name.
* @param content The character data content.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartElement
* @see characters(String)
* @see com.jivesoftware.sax.ContentHandlerendElement
*/
public void dataElement (String uri, String localName, String content)
throws SAXException
{
dataElement(uri, localName, "", EMPTY_ATTS, content);
}

/**
* Add an element with character data content but no attributes or
* Namespace URI.
*
* <p>This is a convenience method to add a complete element
* with character data content, including the start tag
* and end tag. The method provides an empty string for the
* Namespace URI, and empty string for the qualified name,
* and an empty attribute list.</p>
*
* <p>This method invokes
* {@link @see com.jivesoftware.sax.ContentHandlerstartElement},
* followed by
* {@link characters(String)}, followed by
* {@link @see com.jivesoftware.sax.ContentHandlerendElement}.</p>
*
* @param localName The element's local name.
* @param content The character data content.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see com.jivesoftware.sax.ContentHandlerstartElement
* @see characters(String)
* @see com.jivesoftware.sax.ContentHandlerendElement
*/
public void dataElement (String localName, String content)
throws SAXException
{
dataElement("", localName, "", EMPTY_ATTS, content);
}

/**
* Add a string of character data, with XML escaping.
*
* <p>This is a convenience method that takes an XML
* String, converts it to a character array, then invokes
* {@link @see com.jivesoftware.sax.ContentHandlercharacters}.</p>
*
* @param data The character data.
* @exception com.jivesoftware.sax.SAXException If a filter
* further down the chain raises an exception.
* @see @see com.jivesoftware.sax.ContentHandlercharacters
*/
public void characters (String data) throws SAXException
{
char ch[] = data.toCharArray();
characters(ch, 0, ch.length);
}

////////////////////////////////////////////////////////////////////
// Constants.
////////////////////////////////////////////////////////////////////
protected static final Attributes EMPTY_ATTS = new AttributesImpl();
}


XmlProperties.java

package com.my.util.xml;

import java.io.*;
import java.text.*;
import java.util.*;


import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;

/**
* Provides the the ability to use simple XML property files. Each property is
* in the form X.Y.Z, which would map to an XML snippet of:
* <pre>
* <X>
* <Y>
* <Z>someValue</Z>
* </Y>
* </X>
* </pre>
*
* The XML file is passed in to the constructor and must be readable and
* writtable. Setting property values will automatically persist those value
* to disk.
*/
public class XMLProperties {

private File file;
private Document doc;
/**
* Parsing the XML file every time we need a property is slow. Therefore,
* we use a Map to cache property values that are accessed more than once.
*/
private Map propertyCache = new HashMap();

/**
* Creates a new XMLProperties object.
*
* @parm file the full path the file that properties should be read from
* and written to.
*/
public XMLProperties(String file) {
this.file = new File(file);
try {
SAXBuilder builder = new SAXBuilder();
// Strip formatting
DataUnformatFilter format = new DataUnformatFilter();
builder.setXMLFilter(format);
doc = builder.build(new File(file));
}
catch (Exception e) {
System.err.println("Error creating XML parser in "
+ "PropertyManager.java");
e.printStackTrace();
}
}

/**
* Returns the value of the specified property.
*
* @param name the name of the property to get.
* @return the value of the specified property.
*/
public String getProperty(String name) {
if (propertyCache.containsKey(name)) {
return (String)propertyCache.get(name);
}

String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy.
Element element = doc.getRootElement();
for (int i = 0; i < propName.length; i++) {
element = element.getChild(propName);
if (element == null) {
// This node doesn't match this part of the property name which
// indicates this property doesn't exist so return null.
return null;
}
}
// At this point, we found a matching property, so return its value.
// Empty strings are returned as null.
String value = element.getText();
if ("".equals(value)) {
return null;
}
else {
// Add to cache so that getting property next time is fast.
value = value.trim();
propertyCache.put(name, value);
return value;
}
}

/**
* Return all children property names of a parent property as a String array,
* or an empty array if the if there are no children. For example, given
* the properties <tt>X.Y.A</tt>, <tt>X.Y.B</tt>, and <tt>X.Y.C</tt>, then
* the child properties of <tt>X.Y</tt> are <tt>A</tt>, <tt>B</tt>, and
* <tt>C</tt>.
*
* @param parent the name of the parent property.
* @return all child property values for the given parent.
*/
public String [] getChildrenProperties(String parent) {
String[] propName = parsePropertyName(parent);
// Search for this property by traversing down the XML heirarchy.
Element element = doc.getRootElement();
for (int i = 0; i < propName.length; i++) {
element = element.getChild(propName);
if (element == null) {
// This node doesn't match this part of the property name which
// indicates this property doesn't exist so return empty array.
return new String [] { };
}
}
// We found matching property, return names of children.
List children = element.getChildren();
int childCount = children.size();
String [] childrenNames = new String[childCount];
for (int i=0; i<childCount; i++) {
childrenNames = ((Element)children.get(i)).getName();
}
return childrenNames;
}

/**
* Sets the value of the specified property. If the property doesn't
* currently exist, it will be automatically created.
*
* @param name the name of the property to set.
* @param value the new value for the property.
*/
public void setProperty(String name, String value) {
// Set cache correctly with prop name and value.
propertyCache.put(name, value);

String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy.
Element element = doc.getRootElement();
for (int i=0; i<propName.length; i++) {
// If we don't find this part of the property in the XML heirarchy
// we add it as a new node
if (element.getChild(propName) == null) {
element.addContent(new Element(propName));
}
element = element.getChild(propName);
}
// Set the value of the property in this node.
element.setText(value);
// write the XML properties to disk
saveProperties();
}

/**
* Deletes the specified property.
*
* @param name the property to delete.
*/
public void deleteProperty(String name) {
String[] propName = parsePropertyName(name);
// Search for this property by traversing down the XML heirarchy.
Element element = doc.getRootElement();
for (int i=0; i<propName.length-1; i++) {
element = element.getChild(propName);
// Can't find the property so return.
if (element == null) {
return;
}
}
// Found the correct element to remove, so remove it...
element.removeChild(propName[propName.length-1]);
// .. then write to disk.
saveProperties();
}

/**
* Saves the properties to disk as an XML document. A temporary file is
* used during the writing process for maximum safety.
*/
private synchronized void saveProperties() {
OutputStream out = null;
boolean error = false;
// Write data out to a temporary file first.
File tempFile = null;
try {
tempFile = new File(file.getParentFile(), file.getName() + ".tmp");
// Use JDOM's XMLOutputter to do the writing and formatting. The
// file should always come out pretty-printed.
XMLOutputter outputter = new XMLOutputter(" ", true);
out = new BufferedOutputStream(new FileOutputStream(tempFile));
outputter.output(doc, out);
}
catch (Exception e) {
e.printStackTrace();
// There were errors so abort replacing the old property file.
error = true;
}
finally {
try { out.close(); }
catch (Exception e) {
e.printStackTrace();
error = true;
}
}
// No errors occured, so we should be safe in replacing the old
if (!error) {
// Delete the old file so we can replace it.
file.delete();
// Rename the temp file. The delete and rename won't be an
// automic operation, but we should be pretty safe in general.
// At the very least, the temp file should remain in some form.
tempFile.renameTo(file);
}
}

/**
* Returns an array representation of the given Jive property. Jive
* properties are always in the format "prop.name.is.this" which would be
* represented as an array of four Strings.
*
* @param name the name of the Jive property.
* @return an array representation of the given Jive property.
*/
private String[] parsePropertyName(String name) {
// Figure out the number of parts of the name (this becomes the size
// of the resulting array).
int size = 1;
for (int i=0; i<name.length(); i++) {
if (name.charAt(i) == '.') {
size++;
}
}
String[] propName = new String[size];
// Use a StringTokenizer to tokenize the property name.
StringTokenizer tokenizer = new StringTokenizer(name, ".");
int i = 0;
while (tokenizer.hasMoreTokens()) {
propName = tokenizer.nextToken();
i++;
}
return propName;
}
}

//读xml的
readxml.java

package com.my.util.xml;

//导入需要用到的包
import java.util.*;
import java.io.*;
import java.text.*;
import java.net.*;

public class readxml {
//设置xml的配置文件
private static final String JIVE_CONFIG_FILENAME="test.xml";

//所在的主要目录
public static String jiveHome = null;

private static XMLProperties properties = null;

private static Locale locale = null;
private static TimeZone timeZone = null;
private static DateFormat dateFormat = null;
private static DateFormat dateTimeFormat = null;



private synchronized static void loadLocale() {
String language = getJiveProperty("locale.language");
if (language == null) {
language = "";
}
String country = getJiveProperty("locale.country");
if (country == null) {
country = "";
}
// If no locale info is specified, default to Locale.china
if (language.equals("") && country.equals("")) {
locale = Locale.CHINA;
}
else {
locale = new Locale(language, country);
}
String timeZoneID = getJiveProperty("locale.timeZone");
if (timeZoneID == null) {
timeZone = TimeZone.getDefault();
}
else {
timeZone = TimeZone.getTimeZone(timeZoneID);
}
dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM, locale);
dateFormat.setTimeZone(timeZone);
dateTimeFormat.setTimeZone(timeZone);
}


public static Locale getLocale() {
if (locale == null) {
loadLocale();
}
return locale;
}


public static void setLocale(Locale newLocale) {
locale = newLocale;
// Save values to Jive properties.
setJiveProperty("locale.country", locale.getCountry());
setJiveProperty("locale.language", locale.getLanguage());
// Reset the date formatter objects
dateFormat = DateFormat.getDateInstance(DateFormat.MEDIUM, locale);
dateTimeFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM,
DateFormat.MEDIUM, locale);
dateFormat.setTimeZone(timeZone);
dateTimeFormat.setTimeZone(timeZone);
}

public static TimeZone getTimeZone() {
if (timeZone == null) {
loadLocale();
}
return timeZone;
}

public static void setTimeZone(TimeZone newTimeZone) {
timeZone = newTimeZone;
dateFormat.setTimeZone(timeZone);
dateTimeFormat.setTimeZone(timeZone);
setJiveProperty("locale.timeZone", timeZone.getID());
}

public static String formatDate(Date date) {
if (dateFormat == null) {
loadLocale();
}
return dateFormat.format(date);
}

public static String formatDateTime(Date date) {
if (dateTimeFormat == null) {
loadLocale();
}
return dateTimeFormat.format(date);
}




public static String getJiveProperty(String name) {
loadProperties();
return properties.getProperty(name);
}

public static void setJiveProperty(String name, String value) {
loadProperties();
properties.setProperty(name, value);
}

private synchronized static void loadProperties() {
//得到jive所在地目录
if (jiveHome == null) {
jiveHome = new InitPropLoader().getJiveHome();
}
//后面就是xml文件所在的目录
properties = new XMLProperties(jiveHome + File.separator +
JIVE_CONFIG_FILENAME);

}
}


class InitPropLoader {

public String getJiveHome() {
String jiveHome = null;
Properties initProps = new Properties();
InputStream in = null;
try {
in = getClass().getResourceAsStream("/my.properties");
initProps.load(in);
}
catch (Exception e) {
System.err.println("Error reading Jive properties "
+ "in JiveGlobals");
e.printStackTrace();
}
finally {
try {
if (in != null) { in.close(); }
} catch (Exception e) {}
}

if (initProps != null) {
jiveHome = initProps.getProperty("jiveHome");
if (jiveHome != null) {
jiveHome = jiveHome.trim();
//如果文件以/或者\\结尾
while (jiveHome.endsWith("/") || jiveHome.endsWith("\\")) {
jiveHome = jiveHome.substring(0, jiveHome.length()-1);
}
}
}
return jiveHome;
}
}


//在jsp中调用



INDEX.jsp

<%@ page contentType="text/html; charset=GB2312" %>
<%@ page import="com.my.util.xml.*" %>
<html>
<head>
<title>
INDEX
</title>
</head>
<body>
<% com.my.util.xml.readxml td=new com.my.util.xml.readxml();
String td_show=td.getJiveProperty("test.alex");
%>
<%=td_show%>
</body>
</html>


//test.xml的写法
test.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--
This file stores all properties need by Jive. Property names must be in the
format:
prop.name.is.blah=value
That will be stored as:
<prop>
<name>
<is>
<blah>value</blah>
</is>
</name>
</prop>

All properties must be under the "jive" element.

This file should live in your jiveHome directory. The path to that directory
should be specified in your jive_init.properties file. The
jive_init.properties must be in your appserver's classpath.
-->
<!-- root element, all properties must be under this element -->
<jlive234>
<test>
<alex>
sadfasdf
</alex>
</test>
</jlive234>