B
bluebird
Unregistered / Unconfirmed
GUEST, unregistred user!
我写的demo,抛砖引玉而已。
目的:XSL transformer , LOG4J DEMO
4个文件,demo.conf, demo.xml, demo.xsl , XSLDemo.java
1 XSLDemo.java
package com.demo.xsl;
import org.apache.log4j.PropertyConfigurator ;
import org.apache.log4j.Logger ;
import org.apache.log4j.Level ;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.util.*;
import java.io.*;
class XSLDemo{
static Logger log = Logger.getLogger(XSLDemo.class.getName ()) ;
Transformer transformer ;
public XSLDemo(){
}
public XSLDemo(String XSLFile){
initXSL(XSLFile);
}
public void initXSL(String XSLFile){
//
try{
TransformerFactory tFactory = TransformerFactory.newInstance();
transformer = tFactory.newTransformer( new StreamSource(XSLFile));
// 格式化输出
Properties props = transformer.getOutputProperties();
props.put( "{http://xml.apache.org/xslt}use-url-escaping", "no" );
transformer.setOutputProperties( props );
}catch (Exception e){
log.debug("initing error,可能xsl文件不存在!");
}
}
/**
* 转换
*/
public voiddo
Trans(Properties params,String sourceXML,String outFile){
try{
//设置参数
java.util.Enumeration enumKeys = params.keys();
while( enumKeys.hasMoreElements() ) {
String paramName = (String) ( enumKeys.nextElement() );
String paramValue = (String)params.get(paramName);
transformer.setParameter(paramName, new String(paramValue.getBytes("8859_1"),"gb2312") );
}
//转换
transformer.transform(new StreamSource(sourceXML) , new StreamResult(outFile));
}catch(Exception ex){
ex.printStackTrace();
log.debug("Transforming exception");
}
}
/**
* test xsldemo
*/
public static void main(String []args){
PropertyConfigurator.configure(".//log.conf");
log.info("Demo begin
ing...");
//XSL file url
String XSL = ".//demo.xsl";
String srcXML = ".//demo.xml";
String outFile = "out.html";
//
XSLDemo demo = new XSLDemo(XSL);
java.util.Properties params =new Properties();
params.setProperty("param1","David");
params.setProperty("param2","descending");
demo.doTrans(params,srcXML,outFile);
}
}
2 demo.xml
<?xml version="1.0" ?>
<vehicles>
<vehicle year="1996" make="Land Rover" model="Discover" >
<mileage>36500</mileage>
<color>black</color>
<price>22100</price>
</vehicle>
<vehicle year="1997" make="Toyota" model="Land Cruiser" >
<mileage>34800</mileage>
<color>black</color>
<price>35995.5</price>
</vehicle>
<vehicle year="1998" make="chevrolet" model="Suburban 2500" >
<mileage>3550</mileage>
<color>pewter</color>
<price>31995</price>
</vehicle>
</vehicles>
3 demo.xsl
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xslaram name="param1" select="'default value'" />
<xslaram name="param2" select="'ascending'" />
<xsl:template match="/">
<html>
<head>
<title>Used Vehicles</title>
</head>
<body>
<h1 style="text-align:center"><xsl:value-of select="$param1"/> Used Vehicles</h1>
<table align="center" border="1">
<tr>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Mileage</th>
<th>Color</th>
<th>Price</th>
</tr>
<xsl:for-each select="vehicles/vehicle" >
<xsl:sort select="price" data-type="number" order="{$param2}"/>
<tr>
<td><xsl:value-of select="@year" /></td>
<td><xsl:value-of select="@make" /></td>
<td><xsl:value-of select="@model" /></td>
<td><xsl:value-of select="mileage" /></td>
<td><xsl:value-of select="color" /></td>
<td>$<xsl:value-of select="price" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4 为了使用LOG4j 写的一个log.conf
log4j.rootLogger=ALL, A1, A2
#setting for appender A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
#setting for appender A2
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=demo.log
log4j.appender.A2.MaxFileSize=100KB
log4j.appender.A2.MaxBackupIndex=1
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%p %t %c - %m%n
目的:XSL transformer , LOG4J DEMO
4个文件,demo.conf, demo.xml, demo.xsl , XSLDemo.java
1 XSLDemo.java
package com.demo.xsl;
import org.apache.log4j.PropertyConfigurator ;
import org.apache.log4j.Logger ;
import org.apache.log4j.Level ;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;
import java.util.*;
import java.io.*;
class XSLDemo{
static Logger log = Logger.getLogger(XSLDemo.class.getName ()) ;
Transformer transformer ;
public XSLDemo(){
}
public XSLDemo(String XSLFile){
initXSL(XSLFile);
}
public void initXSL(String XSLFile){
//
try{
TransformerFactory tFactory = TransformerFactory.newInstance();
transformer = tFactory.newTransformer( new StreamSource(XSLFile));
// 格式化输出
Properties props = transformer.getOutputProperties();
props.put( "{http://xml.apache.org/xslt}use-url-escaping", "no" );
transformer.setOutputProperties( props );
}catch (Exception e){
log.debug("initing error,可能xsl文件不存在!");
}
}
/**
* 转换
*/
public voiddo
Trans(Properties params,String sourceXML,String outFile){
try{
//设置参数
java.util.Enumeration enumKeys = params.keys();
while( enumKeys.hasMoreElements() ) {
String paramName = (String) ( enumKeys.nextElement() );
String paramValue = (String)params.get(paramName);
transformer.setParameter(paramName, new String(paramValue.getBytes("8859_1"),"gb2312") );
}
//转换
transformer.transform(new StreamSource(sourceXML) , new StreamResult(outFile));
}catch(Exception ex){
ex.printStackTrace();
log.debug("Transforming exception");
}
}
/**
* test xsldemo
*/
public static void main(String []args){
PropertyConfigurator.configure(".//log.conf");
log.info("Demo begin
ing...");
//XSL file url
String XSL = ".//demo.xsl";
String srcXML = ".//demo.xml";
String outFile = "out.html";
//
XSLDemo demo = new XSLDemo(XSL);
java.util.Properties params =new Properties();
params.setProperty("param1","David");
params.setProperty("param2","descending");
demo.doTrans(params,srcXML,outFile);
}
}
2 demo.xml
<?xml version="1.0" ?>
<vehicles>
<vehicle year="1996" make="Land Rover" model="Discover" >
<mileage>36500</mileage>
<color>black</color>
<price>22100</price>
</vehicle>
<vehicle year="1997" make="Toyota" model="Land Cruiser" >
<mileage>34800</mileage>
<color>black</color>
<price>35995.5</price>
</vehicle>
<vehicle year="1998" make="chevrolet" model="Suburban 2500" >
<mileage>3550</mileage>
<color>pewter</color>
<price>31995</price>
</vehicle>
</vehicles>
3 demo.xsl
<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xslaram name="param1" select="'default value'" />
<xslaram name="param2" select="'ascending'" />
<xsl:template match="/">
<html>
<head>
<title>Used Vehicles</title>
</head>
<body>
<h1 style="text-align:center"><xsl:value-of select="$param1"/> Used Vehicles</h1>
<table align="center" border="1">
<tr>
<th>Year</th>
<th>Make</th>
<th>Model</th>
<th>Mileage</th>
<th>Color</th>
<th>Price</th>
</tr>
<xsl:for-each select="vehicles/vehicle" >
<xsl:sort select="price" data-type="number" order="{$param2}"/>
<tr>
<td><xsl:value-of select="@year" /></td>
<td><xsl:value-of select="@make" /></td>
<td><xsl:value-of select="@model" /></td>
<td><xsl:value-of select="mileage" /></td>
<td><xsl:value-of select="color" /></td>
<td>$<xsl:value-of select="price" /></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
4 为了使用LOG4j 写的一个log.conf
log4j.rootLogger=ALL, A1, A2
#setting for appender A1
log4j.appender.A1=org.apache.log4j.ConsoleAppender
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r %-5p [%t] %37c %3x - %m%n
#setting for appender A2
log4j.appender.A2=org.apache.log4j.RollingFileAppender
log4j.appender.A2.File=demo.log
log4j.appender.A2.MaxFileSize=100KB
log4j.appender.A2.MaxBackupIndex=1
log4j.appender.A2.layout=org.apache.log4j.PatternLayout
log4j.appender.A2.layout.ConversionPattern=%p %t %c - %m%n