请问在解析xml文件时报错是什么原因?
|
异常:java.net.MalformedURLException: no protocol: test.xml
win2000,tomcat
请帮忙!
|
请帮忙!
真倒霉。换成jdom也不行。
SAXBuilder sb = new SAXBuilder(); |
为什么会提示no protocol?和文件在什么地方和 protocol有什么关系呢?
db.parse("{path}/test.xml");
现在使用jdom可以访问xml文件了。但在访问的文件的路径是绝对路径才行的,如何换成相对路径?
xml文件中<?xml version="1.0" encoding="GBK"?>,则提示error in building:invalid encoding name"GBK".
xml文件中如果有中文,不管是GBK还是uft-8都报错error in building:invalid byte 1 of 1-byte UTF-8 sequence..
XMLFileName.java
|
|
sb = new SAXBuilder();
doc = sb.build((Reader)FileReader(filename));
试试
(Reader)FileReader(filename)这个我看不懂
谢谢
xml中有中文,已经可以访问了。
doc = sb.build((Reader)FileReader(filename));
我改成这样了。
doc = sb.build((Reader)new FileReader(filename));
参考声明:
javax.xml.parsers.DocumentBuilder.parse(java.lang.String url)
parse(String) 要求一个完整的路径名,前面必须有 http/file 这样的前缀。用:
Document doc=db.parse(new FileInputStream("test.xml"));
应该可以。
另外,发现用 Weblogic 8 时出错:
org.xml.sax.SAXParseException: Invalid encoding name "GBK".
at weblogic.apache.xerces.parsers.DOMParser.parse(DOMParser.java:271)
必须用下面的写法:
Document doc=db.parse(new FileInputStream("test.xml"));
java.io.Reader reader = new FileReader("test.xml");
org.xml.sax.InputSource source = new InputSource(reader);
Document doc=db.parse(source);
真是麻烦啊!Weblogic 7 没有这个问题。
上面多写了一行,应该是:
必须用下面的写法:
java.io.Reader reader = new FileReader("test.xml");
org.xml.sax.InputSource source = new InputSource(reader);
Document doc=db.parse(source);
异常:java.net.MalformedURLException: no protocol: test.xml?
你是不是进行DTD的验证了?
如果是的话,会连接到你的DTD说明的地址,估计会出现这个错误
这种情况下的解决办法就是不验证DTD,把一个方法的参数改为false就行了。
另外,如果是在容器中使用的话,
尽量采用InputStream is = this.getClass().getAsStream(String filename);方法来获得输入流,
this.getClass() 可被Thread.getCurrentThread().getClassLoader(好象是这个)代替