xml通过xsl在jsp页面上展示
第一次接触xsl,还是很简单易用的
xml格式
<?xml version="1.0" encoding="utf-8"?>
<Case>
<CaseID></CaseID>
<TrademarkName></TrademarkName>
<ColorProtectionClaimed></ColorProtectionClaimed>
<SeriesMark></SeriesMark>
<NumberOfSeriesMark>0</NumberOfSeriesMark>
<Attachments>
<Item NumberOfItems="1">
<NumberOfItems></NumberOfItems>
<AttachmentName></AttachmentName>
<MIMEType></MIMEType>
</Item>
</Attachments>
<Priority></Priority>
<ListOfPriorities/>
<ListOfGoodsAndServices>
<Item>
<Class></Class>
<Text></Text>
</Item>
</ListOfGoodsAndServices>
<ListOfApplicants>
<Applicant>
<Priority></Priority>
<Name></Name>
<Address></Address>
<City></City>
<ApplicantRegistrationNumber></ApplicantRegistrationNumber>
</Applicant>
</ListOfApplicants>
</Case>
xsl文件
就是根据xml的标签把xml的数据在xsl文件里展示出来
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2 style="text-align:center;">外代订单详情</h2>
<table border="1" class="listTable" style="table-layout:fixed;overflow:hidden;wrap:false;padding-left:0px;padding-right:0px;text-align:center;">
<tr>
<td>CaseID</td>
<td><xsl:value-of select="Case/CaseID"/></td>
</tr>
<tr>
<td>TrademarkName</td>
<td><xsl:value-of select="Case/TrademarkName"/></td>
</tr>
<tr>
<td>TrademarkType</td>
<td><xsl:value-of select="Case/TrademarkType"/></td>
</tr>
<tr>
<td>DescriptionOfMark</td>
<td><xsl:value-of select="Case/DescriptionOfMark"/></td>
</tr>
<tr>
<td>ColorProtectionClaimed</td>
<td><xsl:value-of select="Case/ColorProtectionClaimed"/></td>
</tr>
<tr>
<td>DescriptionOfTheClaimedColor</td>
<td><xsl:value-of select="Case/DescriptionOfTheClaimedColor"/></td>
</tr>
<tr>
<td>SeriesMark</td>
<td><xsl:value-of select="Case/SeriesMark"/></td>
</tr>
<tr>
<td>NumberOfSeriesMark</td>
<td><xsl:value-of select="Case/NumberOfSeriesMark"/></td>
</tr>
<tr>
<td>Attachments</td>
<td>
<table border="1">
<tr>
<th align="left">NumberOfItems</th>
<th align="left">AttachmentName</th>
<th align="left">MIMEType</th>
<th align="left">Base64</th>
</tr>
<xsl:for-each select="Case/Attachments/Item">
<tr>
<td><xsl:value-of select="NumberOfItems"/></td>
<td><xsl:value-of select="AttachmentName"/></td>
<td><xsl:value-of select="MIMEType"/></td>
<td><xsl:value-of select="Base64"/></td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
<tr>
<td>Country</td>
<td><xsl:value-of select="Case/Country"/></td>
</tr>
<tr>
<td>Priority</td>
<td><xsl:value-of select="Case/Priority"/></td>
</tr>
<tr>
<td>ListOfPriorities</td>
<td>
<table border="1">
<tr>
<th align="left">Country</th>
<th align="left">PriorityDate</th>
<th align="left">ApplicationNumber</th>
</tr>
<xsl:for-each select="Case/ListOfPriorities/Priority">
<tr>
<td><xsl:value-of select="Country"/></td>
<td><xsl:value-of select="PriorityDate"/></td>
<td><xsl:value-of select="ApplicationNumber"/></td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
<tr>
<td>ListOfGoodsAndServices</td>
<td>
<table border="1">
<tr>
<th align="left">Class</th>
<th align="left">Text</th>
</tr>
<xsl:for-each select="Case/ListOfGoodsAndServices/Item">
<tr>
<td><xsl:value-of select="Class"/></td>
<td><xsl:value-of select="Text"/></td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
<tr>
<td>ListOfApplicants</td>
<td>
<table border="1">
<tr>
<th align="left">Priority</th>
<th align="left">Name</th>
<th align="left">Address</th>
<th align="left">PostalNumber</th>
<th align="left">City</th>
<th align="left">Country</th>
<th align="left">ApplicantRegistrationNumber</th>
</tr>
<xsl:for-each select="Case/ListOfApplicants/Applicant">
<tr>
<td><xsl:value-of select="Priority"/></td>
<td><xsl:value-of select="Name"/></td>
<td><xsl:value-of select="Address"/></td>
<td><xsl:value-of select="PostalNumber"/></td>
<td><xsl:value-of select="City"/></td>
<td><xsl:value-of select="Country"/></td>
<td><xsl:value-of select="ApplicantRegistrationNumber"/></td>
</tr>
</xsl:for-each>
</table>
</td>
</tr>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
JAVA后台
/**
* 将XML信息加载出来
* @return
*/
public String toHtml() {
String str = "";
// 实例化 DocumentBuilderFactory 对象
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//将你要加载的xml信息转换成byte[]
byte[] ist = obj.getBytes("xmlInfo");
String content = new String(ist);
try {
StringReader sr = new StringReader(content);
InputSource is = new InputSource(sr);
// 得到 DocumentBuilder 对象
DocumentBuilder db = dbf.newDocumentBuilder();
// 加载 test.xml,并将其转换为 Document 对象
Document doc = db.parse(is);
// 实例 DOMSource 对象
DOMSource source = new DOMSource(doc);
// 加载 xsl 文件
StreamSource ss = new StreamSource(new File("D:\\indicatingDetail.xsl"));
// 实例化 TransformerFactory 对象
TransformerFactory tff = TransformerFactory.newInstance();
Transformer tf = tff.newTransformer(ss);
// 定义写个字符串
StringWriter sw = new StringWriter();
Result resulted = new StreamResult(sw);
// 设定字符编码方式
tf.setOutputProperty("encoding", "UTF-8");
// 将定义好的 .xsl 格式 转换
tf.transform(source, resulted);
// 将结果写为一个字符串,然后打印在控制台上
str = sw.toString();
// System.out.println(str);
//PrintWriter out = response.getWriter();
//out.print(str);
//out.close();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
return str;
}
jsp文件
这里为了方便就用小脚本展示
<%
XmlToHtml toHtml = new XmlToHtml();
String detail = toHtml.toHtml();
%>
<%=detail %>