六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網(wǎng)中心:是一個免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺!

使用xsl來動態(tài)生成java代碼

[摘要]xsl本身就是一個構(gòu)型良好的xml,它能夠把一個xml文檔轉(zhuǎn)換成另外一個xml文檔,或者轉(zhuǎn)換成文本文件、html文件等等。這里就是利用xsl來動態(tài)的生成我們想要的java文件(從某種角度看,java...
xsl本身就是一個構(gòu)型良好的xml,它能夠把一個xml文檔轉(zhuǎn)換成另外一個xml文檔,或者轉(zhuǎn)換成文本文件、html文件等等。這里就是利用xsl來動態(tài)的生成我們想要的java文件(從某種角度看,java代碼其實(shí)也就是一個文本文件),希望能夠通過這篇文章,看到xml以及相關(guān)的技術(shù)所具有的強(qiáng)大能力!

這里首先給一個xml例子,我們將通過一個xsl從該xml文件中抽取有用的信息來生成java代碼(實(shí)際上是一個javabean):
以下內(nèi)容為程序代碼:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<bean>
<name>Product</name>
<comments>This bean represents a product that the company
offers to its customers</comments>
<property>
<name>code</name>
<type>int</type>
<comments>the product inventory code</comments>
</property>
<property>
<name>name</name>
<type>String</type>
<comments>the product name</comments>
</property>
<property>
<name>testedOnAnimals</name>
<type>boolean</type>
<comments>the flag that indicates if the product was
tested on animals</comments>
</property>
<property>
<name>availableSince</name>
<type>java.util.Date</type>
<comments>the date when the company started offering this
product to its customers</comments>
</property>
</bean>


下面我就直接給出轉(zhuǎn)換的xsl,如果大家對xsl不是很了解的話,可以先看一些資料,了解之后就會明白了。我在里面稍微做了些注釋:
以下內(nèi)容為程序代碼:

<?xml version="1.0"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0" 
xmlns:java="http://xml.apache.org/xslt/java"
exclude-result-prefixes="java">

<!--這里就是指定通過這個xsl所轉(zhuǎn)換的結(jié)果的類型,是text格式-->
<xsl:output method = "text"/>

<!--xslt使用模版來處理xml中的節(jié)點(diǎn)-->
<xsl:template match="bean">

/**
* <xsl:value-of select="comments"/>//這里是獲取xml文檔中的節(jié)點(diǎn)值
* This class has been generated by the XSLT processor from the
metadata
*/
public class <xsl:value-of select="name"/> {

/**
 * Creates a new instance of the <xsl:value-of
select="name"/> bean
 */
public <xsl:value-of select="name"/>() {}
 
<xsl:apply-templates select="property"/>
}
</xsl:template>

<xsl:template match="property">
private <xsl:value-of select="type"/>
<xsl:text> </xsl:text>//輸出文本,這里是輸出一個空格
<xsl:value-of select="name"/>;
<xsl:variable name="name" select="name"/>//定義xsl中要使用的變量
<xsl:variable name="cname" select="java:Capitalizer.capitalize($name)"/>//這里使用了xslt extensions,它可以允許在xslt中直接引用java中方法,非常方便。
/**
 * Sets <xsl:value-of select="comments"/>
 * @param <xsl:value-of select="name"/> is <xsl:value-of
select="comments"/>
 */
public void set<xsl:value-of select="$cname"/>(<xsl:value-
of select="type"/> <xsl:text> </xsl:text><xsl:value-
of select="name"/>) {
this.<xsl:value-of select="name"/> = <xsl:value-of
select="name"/>;
}

/**
 * Returns <xsl:value-of select="comments"/>
 * @return <xsl:value-of select="comments"/>
 */
public <xsl:value-of select="type"/><xsl:text></xsl:text>
<xsl:apply-templates select="type"/><xsl:value-of
select="$cname"/>() {
return <xsl:value-of select="name"/>;
}
</xsl:template>

<xsl:template match="type">
<xsl:variable name="type" select="."/>
<xsl:choose>
<xsl:when test="$type='boolean'">is</xsl:when>
<xsl:otherwise>get</xsl:otherwise>
</xsl:choose>
</xsl:template>

</xsl:stylesheet>


好了,完成以上工作之后,只要在cmd中,輸入如下的命令行,就可以獲得我們想要的結(jié)果了:
java org.apache.xalan.xslt.Process -in xmlSource-xsl stylesheet -out outputfile

這里列出結(jié)果:
以下內(nèi)容為程序代碼:

/**
* This bean represents a product that the company offers to its
customers
* This class has been generated by the XSLT processor from the
metadata
*/
public class Product {

/**
 * Creates a new instance of the Product bean
 */
public Product() {}
 
 
private int code;
 
/**
 * Sets the product inventory code
 * @param code is the product inventory code
 */
public void setCode(int code) {
this.code = code;
}

/**
 * Returns the product inventory code
 * @return the product inventory code
 */
public int getCode() {
return code;
}

private String name;
 
/**
 * Sets the product name
 * @param name is the product name
 */
public void setName(String name) {
this.name = name;
}

/**
 * Returns the product name
 * @return the product name
 */
public String getName() {
return name;
}

private boolean testedOnAnimals;
 
/**
* Sets the flag that indicates if the product was tested on animals
* @param testedOnAnimals is the flag that indicates if the product
was tested on animals
*/
public void setTestedOnAnimals(boolean testedOnAnimals) {
this.testedOnAnimals = testedOnAnimals;
}

/**
 * Returns the flag that indicates if the product was tested on
animals
 * @return the flag that indicates if the product was tested on
animals
 */
public boolean isTestedOnAnimals() {
return testedOnAnimals;
}

private java.util.Date availableSince;
 
/**
 * Sets the date when the company started offering this product to
its customers
 * @param availableSince is the date when the company started
offering this product to its customers
 */
public void setAvailableSince(java.util.Date availableSince) {
this.availableSince = availableSince;
}

/**
 * Returns the date when the company started offering this product
to its customers
 * @return the date when the company started offering this product
to its customers
 */
public java.util.Date getAvailableSince() {
return availableSince;
}

}


總結(jié):
1. 在熟悉了xsl的基本使用之后,理解以上的內(nèi)容并不是困難;
2. 這樣做是比較適合預(yù)先知道了某些邏輯功能,但由于某種原因,需要動態(tài)生成,或者是為了節(jié)省不必要的重復(fù)工作,可以通過它自動生成代碼;
3. 修改這個xsl比較方便。

sjoy


主站蜘蛛池模板: 日本欧美中文字幕人在线 | 四虎精品视频 | 日本成人小视频 | 婷婷三级 | 亚洲高清网站 | 午夜在线视频 | 在线污污视污免费 | 日日摸夜夜欧美一区二区 | 啪啪五月 | 日韩欧美三区 | 青青草视频免费看 | 欧美专区视频 | 日韩三级小视频 | 亚洲国产成人久久 | 亚洲欧洲在线视频 | 欧美性视频一区二区三区 | 一级一级一级毛片免费毛片 | 天堂在线中文网 | 色天天天综合色天天碰 | 青娱乐免费在线视频 | 日韩 欧美 综合 在线 制服 | 色老久久精品偷偷鲁一区 | 亚洲综合a | 日韩美女中文字幕 | 日韩精品一区二区三区中文精品 | 亚洲综合影视 | 亚洲一级大黄大色毛片 | 中文字幕女教师julia视频 | 亚洲午夜精品久久久久久抢 | 日韩免费高清一级毛片在线 | 青草香蕉视频 | 亚洲成a人v欧美综合天堂下载 | 四虎成人免费影院网址 | 在线亚洲精品国产成人二区 | 色噜噜在线观看 | 亚洲欧美日韩图片 | 色播激情 | 日韩中文字幕在线看 | 青青爽国产手机在线观看免费 | 亚洲欧美视频在线播放 | 色www免费视频 |