📄 xmlutil.java
字号:
package com.seavision.PermissionManage.common;
/**
*
* @author xutao.huang
*
* @version 1.0
*/
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;
import javax.servlet.ServletContext;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Templates;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
/**
* @author av
* XML Utilities
*/
public class XMLUtil {
private static final String WEBKEY = XMLUtil.class.getName();
private ServletContext context;
private XMLUtil(ServletContext context)
{
this.context = context;
}
// Hashtable is syncronized
private Hashtable templatesCache = new Hashtable();
/**
* creates a transformer for a xsl stylesheet
* @param xslUri the uri of the xslt stylesheet, e.g. "/WEB-INF/mystyle.xsl"
* @param xslCache cache the transformer
* @return 转换器
*/
public synchronized Transformer getTransformer
(
String xslUri,
boolean xslCache)
{
try
{
Templates templates = null;
if (xslCache)
templates = (Templates) templatesCache.get(xslUri);
if (templates == null)
{
TransformerFactory tf = TransformerFactory.newInstance();
URL url = context.getResource(xslUri);
if (url == null)
throw new IllegalArgumentException("stylesheet \"" + xslUri + "\" not found");
templates = tf.newTemplates(new StreamSource(url.toExternalForm()));
if (xslCache)
templatesCache.put(xslUri, templates);
}
return templates.newTransformer();
}
catch (TransformerConfigurationException e)
{
e.printStackTrace();
return null;
}
catch (MalformedURLException e)
{
e.printStackTrace();
return null;
}
}
/**
* creates a transformer for a xsl stylesheet
* @param context for resource lookup and stylesheet caching
* @param xslUri the uri of the xslt stylesheet, e.g. "/WEB-INF/mystyle.xsl"
* @param xslCache cache the transformer
* @return 转换器
*/
public static Transformer getTransformer(ServletContext context, String xslUri, boolean xslCache)
{
return instance(context).getTransformer(xslUri, xslCache);
}
public static DocumentBuilder getParser()
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
dbf.setExpandEntityReferences(true);
return dbf.newDocumentBuilder();
}
catch (FactoryConfigurationError e)
{
e.printStackTrace();
return null;
}
catch (ParserConfigurationException e)
{
e.printStackTrace();
return null;
}
}
public static Document createDocument()
{
try
{
return getParser().newDocument();
}
catch (FactoryConfigurationError e)
{
e.printStackTrace();
return null;
}
}
public static void print(Node node, Writer out)
{
try
{
TransformerFactory tf = TransformerFactory.newInstance();
Source src = new DOMSource(node);
Result dest = new StreamResult(out);
tf.newTransformer().transform(src, dest);
}
catch (TransformerConfigurationException e)
{
e.printStackTrace();
}
catch (TransformerFactoryConfigurationError e)
{
}
catch (TransformerException e)
{
e.printStackTrace();
}
}
public static XMLUtil instance(ServletContext context)
{
XMLUtil service = (XMLUtil) context.getAttribute(WEBKEY);
if (service == null)
{
service = new XMLUtil(context);
context.setAttribute(WEBKEY, service);
}
return service;
}
/**
* The Document. node itself may be a Document node in which case
* @param node node.getOwnerDocument() will return null
* @return 文档对象
*/
public static Document getDocument(Node node)
{
if (node.getNodeType() == Node.DOCUMENT_NODE)
return (Document) node;
return node.getOwnerDocument();
}
public static Document parse(URL url)
{
try
{
//InputSource src = new InputSource(url.openStream());
InputSource src = new InputSource(url.toExternalForm());
return getParser().parse(src);
}
catch (IOException e)
{
e.printStackTrace();
return null;
}
catch (SAXException e)
{
e.printStackTrace();
return null;
}
}
public static Transformer getTransformer(String path)
{
try
{
TransformerFactory tf = TransformerFactory.newInstance();
Templates templates = null;
URL url = new URL(path);
if (url == null)
throw new IllegalArgumentException("stylesheet \"" + path + "\" not found");
templates = tf.newTemplates(new StreamSource(url.toExternalForm()));
return templates.newTransformer();
}
catch (Exception e)
{
return null;
}
}
public static void main(String[] args)
{
String xslPath = "file:///D:/JBProject/InsteadCocoon/index.xsl";
Transformer transformer = getTransformer(xslPath);
//获得domcment
String xmlPath = "file:///D:/JBProject/InsteadCocoon/index.xml";
URL xmlURL = null;
try
{
xmlURL = new URL(xmlPath);
}
catch (java.net.MalformedURLException e)
{
e.printStackTrace();
}
Document document = parse(xmlURL);
DOMSource source = new DOMSource(document);
try
{
StringWriter sw = new StringWriter();
StreamResult result = new StreamResult(sw);
transformer.transform(source, result);
sw.flush();
System.out.println(sw.toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -