html 字节转换的程序有很多,,只选一种public class Test{
public static String Replace(
String source,
String oldString,
String newString) {
if (source == null) {
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart;
int pos;
for (posStart = 0;
(pos = source.indexOf(oldString, posStart)) >= 0;
posStart = pos + lengOfold) {
output.append(source.substring(posStart, pos));
output.append(newString);
} if (posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}
public static String HtmlWrite(String s) {
s = Replace(s, "&", "&;");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, " ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, " ", " ");
return s;
} //测试
public static void main(String args[]) {
String s = HtmlWrite("<asdfad & aa>");
System.out.println(s);
}}
public static String Replace(
String source,
String oldString,
String newString) {
if (source == null) {
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart;
int pos;
for (posStart = 0;
(pos = source.indexOf(oldString, posStart)) >= 0;
posStart = pos + lengOfold) {
output.append(source.substring(posStart, pos));
output.append(newString);
} if (posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
}
public static String HtmlWrite(String s) {
s = Replace(s, "&", "&;");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, " ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, " ", " ");
return s;
} //测试
public static void main(String args[]) {
String s = HtmlWrite("<asdfad & aa>");
System.out.println(s);
}}
解决方案 »
- struts2.0验证框架问题
- 请问JBOSS 2.4X 连接SQL SERVER的方式,请教细节
- 求助关于HIBERNATE删除(缓存)的问题
- tomcat配置文件的一个疑惑
- 问一个JSP中插入Applet的问题
- 关于hibernate 一对多的问题,有兴趣的朋友可以进来看一下!
- 关于接受和发送soap消息的几个问题,100<=分数 hehe ~急啊
- struts <html:select> taglib的问题--急~
- 请问各朋友怎样写这监听器
- 深切同情我吧:总值超过5000升大米,于昨晚不幸遇难失窃
- 请问在JAVA中调用WIN32的API函数,有没有什么比较好的解决方案?
- 初学JAVA,请给指点一点!
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;
import org.jdom.transform.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.*;public class XmlToHtml {
public static String Replace(
String source,
String oldString,
String newString) {
if (source == null) {
return null;
}
StringBuffer output = new StringBuffer();
int lengOfsource = source.length();
int lengOfold = oldString.length();
int posStart;
int pos;
for (posStart = 0;
(pos = source.indexOf(oldString, posStart)) >= 0;
posStart = pos + lengOfold) {
output.append(source.substring(posStart, pos));
output.append(newString);
} if (posStart < lengOfsource) {
output.append(source.substring(posStart));
}
return output.toString();
} public static String HtmlWrite(String s) {
s = Replace(s, "&", "&");
s = Replace(s, "<", "<");
s = Replace(s, ">", ">");
s = Replace(s, " ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, " ", " ");
return s;
}
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: XmlToHtml [some.xml] [some.xsl]");
return;
} String docname = args[0];
String sheetname = args[1]; SAXBuilder builder = new SAXBuilder();
org.jdom.Document doc = builder.build(new java.io.FileInputStream(docname)); Transformer transformer = TransformerFactory.newInstance().newTransformer(new StreamSource(new java.io.FileInputStream(sheetname))); JDOMSource source = new JDOMSource(doc);
JDOMResult result = new JDOMResult(); java.util.Properties properties = transformer.getOutputProperties();
properties.setProperty(OutputKeys.ENCODING,"gb2312");
properties.setProperty(OutputKeys.METHOD,"html");
properties.setProperty(OutputKeys.VERSION,"4.0");
transformer.setOutputProperties(properties); transformer.transform(source, result);
Document doc2 = result.getDocument(); XMLOutputter outp = new XMLOutputter();
outp.setTextNormalize(false);
outp.setEncoding("gb2312");
outp.setIndent(" ");
outp.setNewlines(true);
outp.setTrimAllWhite(true); java.io.StringWriter Sw=new java.io.StringWriter();
outp.output(doc2,Sw); String shtml=HtmlWrite(Sw.toString()); System.out.print(shtml);
//java.io.FileOutputStream stout=new java.io.FileOutputStream("d:\\inetpub\\wwwroot\\x\\aaa.htm");
//stout.write(shtml.getBytes());
}
}