Results 1 to 3 of 3
Thread: get xml data from url
- 11-05-2009, 01:01 AM #1
Member
- Join Date
- Aug 2009
- Posts
- 24
- Rep Power
- 0
get xml data from url
I am trying to get xml data from a url. When I use this to parse the xml from a file, it works perfectly. but when I replace thing.xml with a url, it doesn't work. here is the code:
import java.io.File;
import org.w3c.dom.Document;
import org.w3c.dom.*;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
public class XMLReader{
public static void main (String argv []){
try {
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse (new File("thing.xml"));
// normalize text representation
doc.getDocumentElement ().normalize ();
System.out.println ("Root element of the doc is " +
doc.getDocumentElement().getNodeName());
NodeList listOfPersons = doc.getElementsByTagName("person");
int totalPersons = listOfPersons.getLength();
System.out.println("Total no of people : " + totalPersons);
for(int s=0; s<listOfPersons.getLength() ; s++){
Node firstPersonNode = listOfPersons.item(s);
if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE){
Element firstPersonElement = (Element)firstPersonNode;
//-------
NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
Element firstNameElement = (Element)firstNameList.item(0);
NodeList textFNList = firstNameElement.getChildNodes();
System.out.println("First Name : " +
((Node)textFNList.item(0)).getNodeValue().trim());
//-------
NodeList lastNameList = firstPersonElement.getElementsByTagName("last");
Element lastNameElement = (Element)lastNameList.item(0);
NodeList textLNList = lastNameElement.getChildNodes();
System.out.println("Last Name : " +
((Node)textLNList.item(0)).getNodeValue().trim());
//----
NodeList ageList = firstPersonElement.getElementsByTagName("age");
Element ageElement = (Element)ageList.item(0);
NodeList textAgeList = ageElement.getChildNodes();
System.out.println("Age : " +
((Node)textAgeList.item(0)).getNodeValue().trim()) ;
//------
}//end of if clause
}//end of for loop with s var
}catch (SAXParseException err) {
System.out.println ("** Parsing error" + ", line "
+ err.getLineNumber () + ", uri " + err.getSystemId ());
System.out.println(" " + err.getMessage ());
}catch (SAXException e) {
Exception x = e.getException ();
((x == null) ? e : x).printStackTrace ();
}catch (Throwable t) {
t.printStackTrace ();
}
//System.exit (0);
}//end of main
}
when I use a url i get this error:
java.io.FileNotFoundException: /Users/jforce/http:/developerlife.com/xmljavatutorial1/AddressBook.xml (No such file or directory)
- 11-05-2009, 04:18 AM #2
how about separating the url and the file from the document, such as using a function to read a document from an input stream.
so, to read from a file,Java Code:/** * Constructs a Document object by reading from an input stream. */ public static Document parse (InputStream is) { Document ret = null; DocumentBuilderFactory domFactory; DocumentBuilder builder; try { domFactory = DocumentBuilderFactory.newInstance(); domFactory.setValidating(false); domFactory.setNamespaceAware(false); builder = domFactory.newDocumentBuilder(); ret = builder.parse(is); } catch (Exception ex) { System.error.println("unable to load XML: " + ex); } return ret; }
and, to read from a urlJava Code:InputStream in = new FileInputStream(new File("myfile.xml")); Document doc = parse(in);
Java Code:URL xmlUrl = new URL("file:///home/me/myfile.xml"); InputStream in = xmlUrl.openStream(); Document doc = parse(in);
- 02-02-2010, 10:35 PM #3
Member
- Join Date
- Aug 2009
- Posts
- 24
- Rep Power
- 0
Similar Threads
-
data structure and data base??
By ahmed13 in forum Advanced JavaReplies: 8Last Post: 03-27-2009, 05:48 AM -
how to store the data in data base
By eclipse3.4ide in forum New To JavaReplies: 5Last Post: 02-03-2009, 04:25 AM -
error while retrieving data from data base
By kirtesh4u in forum New To JavaReplies: 5Last Post: 11-15-2008, 04:10 PM -
Data Pipeline 2 - Data Transformation Toolkit for Java Released
By dele in forum Java SoftwareReplies: 0Last Post: 10-31-2008, 02:13 PM -
Data Sorting in a .data file using java
By stutiger99 in forum New To JavaReplies: 2Last Post: 10-08-2008, 02:52 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks