当前位置: 技术问答>java相关
------ 求助DOM中文解决 ------
来源: 互联网 发布时间:2015-06-09
本文导语: public Document getdocument(){ DataInputStream dis=new DataInputStream( new BufferedInputStream( new StringBufferInputStream(this.domStr))); DocumentBu...
public Document getdocument(){
DataInputStream dis=new DataInputStream(
new BufferedInputStream(
new StringBufferInputStream(this.domStr)));
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder builder=factory.newDocumentBuilder();
this.document=builder.newDocument()
this.document=builder.parse(dis));
}catch(ParserConfigurationException pce){pce.printStackTrace();}
catch(IOException ioe){ioe.printStackTrace();}
catch(SAXException sae){sae.printStackTrace();}
return this.document;
}
说明:domStr为XML文档字符串,如:
domStr=""
+""
+"16"
+"中文问题"
+"";
如果将“中文问题”改由字母表示,则执行时没有问题。builder.parse()方法只能是InputStream ,或者InputSource ,或者File ,我想得字符串流。好像用StringReader可以解决中文问题,但builder.parse()方法的参数不能传递为Reader,请提供一个解决的方法?
谢谢!!!
DataInputStream dis=new DataInputStream(
new BufferedInputStream(
new StringBufferInputStream(this.domStr)));
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
try{
DocumentBuilder builder=factory.newDocumentBuilder();
this.document=builder.newDocument()
this.document=builder.parse(dis));
}catch(ParserConfigurationException pce){pce.printStackTrace();}
catch(IOException ioe){ioe.printStackTrace();}
catch(SAXException sae){sae.printStackTrace();}
return this.document;
}
说明:domStr为XML文档字符串,如:
domStr=""
+""
+"16"
+"中文问题"
+"";
如果将“中文问题”改由字母表示,则执行时没有问题。builder.parse()方法只能是InputStream ,或者InputSource ,或者File ,我想得字符串流。好像用StringReader可以解决中文问题,但builder.parse()方法的参数不能传递为Reader,请提供一个解决的方法?
谢谢!!!
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource=new InputSource(new StringReader(inputString));
Document document = builder.parse(inputSource);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource inputSource=new InputSource(new StringReader(inputString));
Document document = builder.parse(inputSource);