org.dom4j.DocumentException: no protocol

代码如下:

 public Document getDocumentByString(String xml) {
  SAXReader sax = new SAXReader();
  Document xmlDoc = null;
  try {
   xmlDoc = sax.read(xml);
  } catch (UnsupportedEncodingException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (DocumentException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  return xmlDoc;
 }

DOM4J 解析XML时出现以下问题:

org.dom4j.DocumentException: no protocol:
 at org.dom4j.io.SAXReader.read(SAXReader.java:484)
 at org.dom4j.io.SAXReader.read(SAXReader.java:321)
 at com.mobile.parse.xml.ParseTest.getDocumentByString(ParseTest.java:45)
 at com.mobile.parse.xml.ParseTest.main(ParseTest.java:105)
Nested exception:
java.net.MalformedURLException: no protocol:

 at java.net.URL.<init>(Unknown Source)
 at java.net.URL.<init>(Unknown Source)
 at java.net.URL.<init>(Unknown Source)
 at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
 at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
 at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
 at org.dom4j.io.SAXReader.read(SAXReader.java:465)
 at org.dom4j.io.SAXReader.read(SAXReader.java:321)
 at com.mobile.parse.xml.ParseTest.getDocumentByString(ParseTest.java:45)
 at com.mobile.parse.xml.ParseTest.main(ParseTest.java:105)
Nested exception: java.net.MalformedURLException: no protocol:
 at java.net.URL.<init>(Unknown Source)
 at java.net.URL.<init>(Unknown Source)
 at java.net.URL.<init>(Unknown Source)
 at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
 at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
 at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
 at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)null

 at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
 at org.dom4j.io.SAXReader.read(SAXReader.java:465)
 at org.dom4j.io.SAXReader.read(SAXReader.java:321)
 at com.mobile.parse.xml.ParseTest.getDocumentByString(ParseTest.java:45)
 at com.mobile.parse.xml.ParseTest.main(ParseTest.java:105)

怎么解决哦?????

 原因是XML文件和开发环境的编码问题.

将xmlDoc = sax.read(xml)改成:

xmlDoc = sax.read(new ByteArrayInputStream(xml.getBytes("UTF-8")));

这里保证编码的一致.就可以了!

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/xu283900277xiu/archive/2009/09/05/4521317.aspx

你可能感兴趣的:(exception,xml,String,null,nested)