weblogic8.1环境中解析xml出现Invalid encoding name "GBK"的问题.
JBuilder中解析XML格式的字符串,开始可正常解析,但当加入Weblogic8.x client包后就无法解析了,出错信息:[Fatal Error] :1:37:Invalid encoding name "GBK".
网上已有相关解答,如下:
---------------------------------------------------------------
在weblogic环境下要对字符集进行显式的说明,否则weblogic平台不识别,方法如下:
SAXParserFactory sf = SAXParserFactory.newInstance();
SAXParser sp = sf.newSAXParser();
ParseClass reader = new ParseClass();
FileInputStream inputStream = new FileInputStream(file);
Charset charset = Charset.forName("GBK"); // 在此处声明GBK方式
Reader r = new InputStreamReader(inputStream, charset);
InputSource source = new InputSource(r);
sp.parse(source,reader);
---------------------------------------------------------------
但我还是无法解决此问题, ParseClass是什么包中的类? Charset是哪个包中的类? 望有朋友能帮忙解决这个问题.
我原来的解析程序如下:
package untitled1;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.io.InputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
public class Untitled2 {
public Untitled2() {
}
public static void main(String[] args){
String xmlString = "<?xml version=\"1.0\" encoding=\"GBK\"?><Root><message>香港紅磡環海街11號</message></Root>";
Untitled2 t = new Untitled2();
String message = t.parse(xmlString);
System.out.println("message:" + message);
}
public String parse(String xmlString) {
DocumentBuilderFactory xdf = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = xdf.newDocumentBuilder();
}
catch (ParserConfigurationException ex) {
System.out.println("ParserConfigurationException:"+ex.getMessage());
return "-1";
}
String message = "";
byte[] b = xmlString.getBytes();
InputStream inputStream = new ByteArrayInputStream(b);
String s = new String();
Document doc = null;
try {
doc = builder.parse(inputStream);
}
catch (IOException ex2) {
System.out.println("IOException:"+ex2.getMessage());
return "-1";
}
catch (SAXException ex2) {
System.out.println("SAXException :"+ex2.getMessage());
return "-1";
}
Element root = doc.getDocumentElement();
NodeList Order = root.getChildNodes();
if (Order != null) {
for (int i = 0; i < Order.getLength(); i++) {
Node book = Order.item(i);
if (book.getNodeName().equals("message")) {
System.out.println("message:" + book.getFirstChild().getNodeValue());
message = book.getFirstChild().getNodeValue();
}
}
}
return message;
}
}