HTML for conversion of Java and XML (iv) XML

Source: Internet
Author: User
Tags xsl

XML transformations are a necessary and common task. Here is an example of XML conversion to HTML:

Myxml.xml:
<?xml version= "1.0"?>
<WorkShop>
<layer>
<Title>a</Title>
<Content>aa</Content>
</layer>
<layer>
<Title>a1</Title>
<Content>aa1</Content>
</layer>
</WorkShop>

Myxsl.xsl:
<?xml version= "1.0"?>
<xsl:stylesheet version= "1.0"
Xmlns:xsl= "Http://www.w3.org/1999/XSL/Transform" >
<xsl:template match= "/" >

  <HTML>
  <HEAD>
 <title>this is a xsl2xml test</title>
  <meta http-equiv= "Content-type" content= "text/html"/>
  <style>
. Table2{border: #000000 Solid;border-width:1 0 0 1;background: #ffffff}
. Td2{border: #000000 solid;border-width:0 1 1 0;word-break:break-all;}
TextArea {
 font-family: "XXFarEastFont-Arial";
 font-size:12px;
 font-style:normal;
 line-height:normal;
 font-weight:normal;
 font-variant:normal;
 border:none #000000;
}
td {
 font-family: "XXFarEastFont-Arial";
 font-size:12px;
 font-style:normal;
 line-height:normal;
 font-weight:normal;
 font-variant:normal;
 border:none #000000;
}
</style>

<body>
<xsl:for-each select= "Workshop/layer"
  <table width= "100%" border= "0" cellspacing= "0" cellpadding= "0" style= "border: #000000 1 solid"
<tr>
<td>
 <table Width= "98%" height= "98%" cellspacing= "0" cellpadding= "0" border= "0" class= "table2" align= "center"
 < Tr>
 <td class= "TD2"
 <b> title: </b>
 <xsl:value-of select= "title"/
 </td>
 </tr>
 <br/>
 
 <tr>
  <TD class= "TD2"
 <b> content: </b>
 <br/>
 <textarea readonly= " ReadOnly "Name=" textarea1 "style=" width:99%;overflow:visible;readonly "wrap=" "VIRTUAL"
 <xsl: value-of select= "Content"/>
 </textarea>
 </td>
 </tr>
</ Table>
</td>
</tr>
</table>
</xsl:for-each>

</body>
</xsl:template>
</xsl:stylesheet>

Note that the above two files, in the save to use Unicode encoding, or the Java program may error, and you can not find out exactly where the problem.

Conversion program Xsl2xml.java:
Package src;
Import java.io.*;
Import org.w<?xml:namespace prefix = st1 ns = "Urn:schemas-microsoft-com:office:smarttags"/>3c.dom.document;
Import javax.xml.transform.*;
Import javax.xml.transform.stream.*;
Import Javax.xml.transform.dom.DOMSource;
Import javax.xml.parsers.*;

public class Xsl2xml {
Private String Xmlfile,xslfile,outputfile;

Public Xsl2xml (String xmlfile,string xslfile,string outputfile) {
This.xmlfile=xmlfile;
This.xslfile=xslfile;
This.outputfile=outputfile;
}

public void translate () {
try{
Documentbuilderfactory dbf=documentbuilderfactory.newinstance ();
Documentbuilder Db=dbf.newdocumentbuilder ();
Document Doc=db.parse (xmlfile);
Transformerfactory factory=transformerfactory.newinstance ();

Transformer Transformer=factory.newtransformer (New Streamsource (Xslfile));
Transformer.setoutputproperty (outputkeys.encoding, "GB2312");
PrintWriter pw=new PrintWriter (New FileOutputStream (outputfile));
Streamresult result=new Streamresult (PW);

Transformer.transform (New Domsource (DOC), result);

}catch (Exception exp) {
Exp.printstacktrace ();
}
}

public static void Main (string[] args) {
Xsl2xml myto=new Xsl2xml (
"F://tomcat5//webapps//myxml//xmldata//myxml.xml",
"F://tomcat5//webapps//myxml//xmldata//myxsl.xsl",
"F://tomcat5//webapps//myxml//xmldata//xsl2xml.html"
);
Myto.translate ();

}
}

This conversion is quite experimental, but the actual meaning is not significant. Because if the myxml.xml and myxsl.xsl are associated, the conversion is automatically realized when you open the Myxml.xml in a browser.
However, browsers cannot convert XML files into other formats. Therefore, we can use this as a lease to learn the transformation of XML. I will continue to convert this topic in (v) ――xml2pdf.

<?xml:namespace prefix = o ns = "Urn:schemas-microsoft-com:office:office"/>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.