把字符串解析成xml的时候报错。
接收的字符串:<?xml version="1.0" encoding="utf-8"?>
<holiday><yearday id="2008">
<weekendwork>2008-01-31;2008-05-01;2008-10-03</weekendwork>
<vacation>2008-04-30;2008-12-30;2008-12-31</vacation>
</yearday>
</holiday>解析xml报错的代码
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
try {
//从dom工厂获取dom解析器
DocumentBuilder docb= docbf.newDocumentBuilder();
//解析xml文档的输入流。转化成一个document
try {
Document doc = docb.parse(in_string);//这一行报的错。。
//获取文档的根节点
Element root = doc.getDocumentElement();
//获取根节点的子节点
NodeList childNodes = root.getChildNodes();
接收的字符串:<?xml version="1.0" encoding="utf-8"?>
<holiday><yearday id="2008">
<weekendwork>2008-01-31;2008-05-01;2008-10-03</weekendwork>
<vacation>2008-04-30;2008-12-30;2008-12-31</vacation>
</yearday>
</holiday>解析xml报错的代码
DocumentBuilderFactory docbf = DocumentBuilderFactory.newInstance();
try {
//从dom工厂获取dom解析器
DocumentBuilder docb= docbf.newDocumentBuilder();
//解析xml文档的输入流。转化成一个document
try {
Document doc = docb.parse(in_string);//这一行报的错。。
//获取文档的根节点
Element root = doc.getDocumentElement();
//获取根节点的子节点
NodeList childNodes = root.getChildNodes();
但是换成String就报错了。这个是什么原因啊?
不是string字符串
用这个
Document doc = docb.parse(new java.io.ByteArrayInputStream(in_string.getBytes()));
可能会有乱码问题,
换下编码方式就行