当前位置: 技术问答>java相关
XML 中文处理问题
来源: 互联网 发布时间:2015-11-20
本文导语: String strxml= 我 //为清楚起见,以上给strxml直接附了一个字符串,下面是解析的代码 DocumentBuilder builder; Document document = null; StringBufferInputStream strBuIn = new StringBufferInputStream(strxml); DocumentBuilderFactory factory = D...
String strxml=
我
//为清楚起见,以上给strxml直接附了一个字符串,下面是解析的代码
DocumentBuilder builder;
Document document = null;
StringBufferInputStream strBuIn = new StringBufferInputStream(strxml);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
builder = factory.newDocumentBuilder();
document = builder.parse(strBuIn);
}
catch (SAXException se)
{
}
catch (IOException ioe)
{
}
catch (ParserConfigurationException pce)
{
}
问题是,为什么总是无法正确解析,是不是直接用手写的中文,java解析器不认识.
我
//为清楚起见,以上给strxml直接附了一个字符串,下面是解析的代码
DocumentBuilder builder;
Document document = null;
StringBufferInputStream strBuIn = new StringBufferInputStream(strxml);
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
builder = factory.newDocumentBuilder();
document = builder.parse(strBuIn);
}
catch (SAXException se)
{
}
catch (IOException ioe)
{
}
catch (ParserConfigurationException pce)
{
}
问题是,为什么总是无法正确解析,是不是直接用手写的中文,java解析器不认识.
|
// 程序如下:
package org.javaresearch.jfml.test;
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
public class ChineseXmlParser
{
public static void main(String[] args)
{
try
{
String strXml =
"我";
StringReader stringReader = new StringReader(strXml);
InputSource inputSource = new InputSource(stringReader);
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document document =
documentBuilder.parse(inputSource);
System.out.println(document.getDocumentElement().getFirstChild().getFirstChild().toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}
package org.javaresearch.jfml.test;
import java.io.*;
import org.xml.sax.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.xml.transform.*;
public class ChineseXmlParser
{
public static void main(String[] args)
{
try
{
String strXml =
"我";
StringReader stringReader = new StringReader(strXml);
InputSource inputSource = new InputSource(stringReader);
DocumentBuilderFactory documentBuilderFactory =
DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder =
documentBuilderFactory.newDocumentBuilder();
Document document =
documentBuilder.parse(inputSource);
System.out.println(document.getDocumentElement().getFirstChild().getFirstChild().toString());
}
catch (Exception e)
{
e.printStackTrace();
}
}
}