Win a copy of Succeeding with AI this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
  • Junilu Lacar
Sheriffs:
  • Tim Cooke
  • Jeanne Boyarsky
  • Knute Snortum
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:
  • salvin francis
  • fred rosenberger
  • Frits Walraven

Reading XML data using DOM

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I am new to XML .Can any one point out to me what wrong with the following code.
-------------------------------------------
<%@ page contentType="text/html"%>
<%@ page import="javax.xml.parsers.DocumentBuilderFactory,javax.xml.parsers.DocumentBuilder " %>
<%@ page import="org.w3c.dom.*" %>
<%
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse("C:\\xml\\message.xml");
NodeList n1 = doc.getElementsByTagName("messages");

%>

<html>
<body>
<%=n1.item(0).getFirstChild().getNodeValue() %>
</body>
</html>
------------------------------------------
<?xml version="1.0" encoding="UTF-8"?>
<message>
<messages>Hello XML .. </messages>
</message>
------------------------------------------

When I run the code above , I getting following error:
java.net.MalformedURLException: unknown protocol: c
at java.net.URL.<init>(URL.java:480)
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)
 
Rancher
Posts: 1449
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I had to guess (which I do) I'd think that it didn't like "C:\\xml\\message.xml". Check the API and see what it says about the URLs forms it will accept.
BTW, please change your displayed name to follow the JavaRanch naming policy. You can do this here.
Thanks,
 
Ranch Hand
Posts: 120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I ran this and it worked fine check your path the code is right, you are getting a malformed URL check the path.
Good Luck I am using tomcat 4.0
Ray
 
Are you okay? You look a little big. Maybe this tiny ad will help:
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic