java.net.MalformedURLException: no protocol: links.xml异常
点击右边红色标题查看本文完整版:java.net.MalformedURLException: no protocol: links.xml异常
我编了一个javabean实现从xml文件中读内容:
? 源码如下:
? package xmlwriter;
?
? import javax.xml.parsers.*;
? import org.w3c.dom.*;
?
? public class xmldisplay {
? public static void main(String args[]){
? try{
? DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
? DocumentBuilder builder=factory.newDocumentBuilder();
? Document doc=builder.parse("links.xml");
? //doc.normalize();
? NodeList links =doc.getElementsByTagName("link");
? for (int i=0;i<links.getLength();i++){
? Element link=(Element) links.item(i);
? System.out.print("Content: ");
? System.out.println(link.getElementsByTagName("text").item(0).getFirstChild().getNodeValue());
? System.out.print("URL: ");
? System.out.println(link.getElementsByTagName("url").item(0).getFirstChild().getNodeValue());
? System.out.print("Author: ");
? System.out.println(link.getElementsByTagName("author").item(0).getFirstChild().getNodeValue());
? System.out.print("Date: ");
? Element linkdate=(Element) link.getElementsByTagName("date").item(0);
? String day=linkdate.getElementsByTagName("day").item(0).getFirstChild().getNodeValue();
? String month=linkdate.getElementsByTagName("month").item(0).getFirstChild().getNodeValue();
? String year=linkdate.getElementsByTagName("year").item(0).getFirstChild().getNodeValue();
? System.out.println(day+"-"+month+"-"+year);
? System.out.print("Description: ");
? System.out.println(link.getElementsByTagName("description").item(0).getFirstChild().getNodeValue());
? System.out.println();
? }
? }catch(Exception e){
? e.printStackTrace();
? }
?
? }
? }
? 出错提示为:
? D:\JBuilder4\jdk1.3\bin\javaw -classpath "D:\xmlwriter\classes\xmlwriter;D:\JBuilder4\lib\jaxp.jar;D:\JBuilder4\lib\parser.jar;D:\JBuilder4\lib\servlet.jar;D:\JBuilder4\jdk1.3\demo\jfc\Java2D\Java2Demo.jar;D:\JBuilder4\jdk1.3\jre\lib\i18n.jar;D:\JBuilder4\jdk1.3\jre\lib\jaws.jar;D:\JBuilder4\jdk1.3\jre\lib\rt.jar;D:\JBuilder4\jdk1.3\jre\lib\sunrsasign.jar;D:\JBuilder4\jdk1.3\lib\dt.jar;D:\JBuilder4\jdk1.3\lib\tools.jar" xmlwriter.xmldisplay
? java.net.MalformedURLException: no protocol: links.xml
? at java.net.URL.<init>(URL.java:473)
? at java.net.URL.<init>(URL.java:376)
? at java.net.URL.<init>(URL.java:330)
? at com.sun.xml.parser.InputEntity.init(InputEntity.java:140)
? at com.sun.xml.parser.Parser.parseInternal(Parser.java:463)
? at com.sun.xml.parser.Parser.parse(Parser.java:284)
? at com.sun.xml.parser.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:95)
? at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:100)
? at xmlwriter.xmldisplay.main(xmldisplay.java:11)