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, "&amp", "&;");
s = Replace(s, "&lt;", "<");
s = Replace(s, "&gt;", ">");
s = Replace(s, "    ", "\t");
s = Replace(s, "\n", "\r\n");
s = Replace(s, "<br>", "\n");
s = Replace(s, " &nbsp;", "  ");
return s;
}         //测试  
         public static void main(String args[]) {
                   String s = HtmlWrite("<asdfad & aa>");
                   System.out.println(s);  
         }}

解决方案 »

  1.   

    可以了,以下是我的测试程序。
    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, "&amp;", "&");
    s = Replace(s, "&lt;", "<");
    s = Replace(s, "&gt;", ">");
    s = Replace(s, "    ", "\t");
    s = Replace(s, "\n", "\r\n");
    s = Replace(s, "<br>", "\n");
    s = Replace(s, " &nbsp;", "  ");
    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());
      }
    }