weblogic8.1环境中解析xml出现Invalid encoding name "GBK"的问题.

szmaomao 2006-10-27 06:13:55
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;
}
}
...全文
1029 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
szmaomao 2006-10-29
  • 打赏
  • 举报
回复
只你一个人回复过,分就全给你吧。
szmaomao 2006-10-29
  • 打赏
  • 举报
回复
谢谢楼上这位朋友,我好像找到解决办法了,也贴出来吧,解析的过程如下:
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();

java.io.Reader reader=new StringReader(xmlString);
InputSource inputSource=new InputSource(reader);

Document doc=db.parse(inputSource);

对应的包有:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
import java.io.IOException;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
import java.io.StringReader;
lbx19822004 2006-10-28
  • 打赏
  • 举报
回复
你还可以去weblogic专门的论坛去问一下:
http://dev2dev.bea.com.cn/bbs/index.jspa
lbx19822004 2006-10-28
  • 打赏
  • 举报
回复
ParseClass类 --> groovy.lang.GroovyClassLoader.parseClass
Charset类 --> java.nio.charset.Charset
这个问题的确有些难度,可惜我没太多时间帮你,你装的weblogic的有没有带一些接口,类包之类的东西啊,估计ParseClass是这个什么groovy包里的类
lbx19822004 2006-10-28
  • 打赏
  • 举报
回复
不是,是会的人没在线
兄弟不会,只能是帮你顶一下~~
szmaomao 2006-10-28
  • 打赏
  • 举报
回复
是不是问题提的不好啊?
szmaomao 2006-10-27
  • 打赏
  • 举报
回复
自己顶一下!

67,515

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧