Trong hướng dẫn này, chúng ta hãy chuyển đổi XML sang HTML bằng ngôn ngữ XSLT (Extensible Stylesheet Language Transformations). Rất đơn giản để chuyển đổi XML sang bất kỳ định dạng nào khác như HTML, văn bản thuần túy, v.v. sử dụng XSLT. Để tạo XML từ dữ liệu bảng, bạn có thể truy cập hướng dẫn Java trước đó của tôi để chuyển đổi dữ liệu bảng thành XML và ngược lại Show Bây giờ chúng ta hãy chuyển đổi XML bên dưới thành html bằng XSLT XML
<ProductList> <Product> <productId>I1</productId> <productName>Samsung LED Tv</productName> <price>40000.00</price> <stock>5</stock> </Product> <Product> <productId>I3</productId> <productName>SONY LCD TV</productName> <price>30000.00</price> <stock>7</stock> </Product> </ProductList> Lưu trữ XML trong một tệp có tên là sản phẩm. xml Bây giờ, hãy để chúng tôi tạo XSLT để tạo html để tạo đầu ra bên dưới XSLT
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <head> <style type="text/css"> table.tfmt { border: 1px ; } td.colfmt { border: 1px ; background-color: white; color: black; text-align:right; } th { background-color: #2E9AFE; color: white; } </style> </head> <body> <table class="tfmt"> <tr> <th style="width:250px">Product Code:</th> <th style="width:350px">Product Name:</th> <th style="width:250px">Price:</th> <th style="width:250px">Stock:</th> </tr> <xsl:for-each select="ProductList/Product"> <tr> <td class="colfmt"> <xsl:value-of select="productId" /> </td> <td class="colfmt"> <xsl:value-of select="productName" /> </td> <td class="colfmt"> <xsl:value-of select="price" /> </td> <td class="colfmt"> <xsl:value-of select="stock" /> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet> Lưu trữ xslt ở trên trong một tệp có tên là sản phẩm. xsl Bây giờ chúng ta hãy viết mã Java để chuyển đổi XML ở trên sang HTML XmlToHtml. java
package net.javaonline.db2xml; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.io.StringWriter; import javax.xml.transform.Source; 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; public class XmlToHtml { public static void main(String args[]) { Source xml = new StreamSource(new File("D:\\template\\product.xml")); Source xslt = new StreamSource("D:\\template\\product.xsl"); convertXMLToHTML(xml, xslt); } public static void convertXMLToHTML(Source xml, Source xslt) { StringWriter sw = new StringWriter(); try { FileWriter fw = new FileWriter("D:\\template\\product.html"); TransformerFactory tFactory = TransformerFactory.newInstance(); Transformer trasform = tFactory.newTransformer(xslt); trasform.transform(xml, new StreamResult(sw)); fw.write(sw.toString()); fw.close(); System.out .println("product.html generated successfully at D:\\template "); } catch (IOException | TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } Trong chương trình java ở trên, phương thức convertXMLToHTML chấp nhận hai đối số i. e xml và xslt. Tạo một thể hiện của lớp Transformer bằng TransformerFactory. phương pháp newTransformer dựa trên xslt. Sử dụng phương thức biến đổi của đối tượng biến áp để chuyển đổi XML đầu vào sang bất kỳ định dạng nào khác như html XSLT là một dạng rút gọn của 'Chuyển đổi ngôn ngữ biểu định kiểu có thể mở rộng', công nghệ mạnh mẽ và được sử dụng để chuyển đổi XML thành các định dạng khác nhau như HTML. Trong bài đăng này, chúng ta sẽ xem cách chuyển đổi tệp XML sang HTML với sự trợ giúp của XSLT trong Java Những điểm chính
Tệp tiên quyết 1. Tệp XML (giả sử báo cáo. xml) 2. Tệp XSL (giả sử biểu định kiểu. xl)
Tệp XML mẫu. bài báo cáo. xml Tệp XML XHTML1 2 3 4 5 6 7 8 9 10 11 12 13 xml phiên bản="1. 0"mã hóa="UTF-8"?>xml version="1.0" encoding="UTF-8"?>
Tệp XSL mẫu. biểu định kiểu. xsl Tệp XSL XHTML1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 xml phiên bản="1. 0"mã hóa="UTF-8"?>xml version="1.0" encoding="UTF-8"?>
Kết quả trường hợp thử nghiệm
Dưới đây là mã Java để tạo tệp HTML bằng các tệp XML và XSL ở trên. (nói báo cáo. html) Mã Java để tạo tệp HTML 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 nhập java. io. FileOutputStream;
nhập java. io. Luồng đầu ra;
nhập javax. xml. biến đổi. Nguồn;
nhập javax. xml. biến đổi. Máy biến áp;
nhập javax. xml. biến đổi. Nhà máy biến áp;
nhập javax. xml. biến đổi. luồng. Kết quả luồng;
nhập javax. xml. biến đổi. luồng. Nguồn phát trực tiếp;
lớp công khai Xmltohtml {
công khai tĩnh vô hiệu chính(String[] args) throws Exception {
thử {
TransformerFactory tFactory = TransformerFactory.newInstance();
Nguồn xslDoc = mới StreamSource("/Users/jradmin/Documents/stylesheet.xsl");
Nguồn xmlDoc = mới StreamSource("/Users/jradmin/Documents/report.xml");
Chuỗi tên tệp đầu ra = "/Users/jradmin/ . html";
Luồng đầu ra htmlTệp = mới FileOutputStream(outputFileName);
Máy biến áp trasform = tNhà máy.newTransformer(xslDoc);
chuyển đổi. chuyển đổi(xmlDoc, mới StreamResult(htmlFile));
} bắt (Ngoại lệ e)
{
e. printStackTrace();
}
}
} Dưới đây là báo cáo HTML được tạo (báo cáo. html)
Thẩm quyền giải quyết
Tác giả
|