Results 1 to 3 of 3
  1. #1
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

    Default 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)

  2. #2
    travishein's Avatar
    travishein is offline Senior Member
    Join Date
    Sep 2009
    Location
    Canada
    Posts
    684
    Rep Power
    10

    Default

    how about separating the url and the file from the document, such as using a function to read a document from an input stream.
    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;
        }
    so, to read from a file,

    Java Code:
    InputStream in = new FileInputStream(new File("myfile.xml"));
    Document doc = parse(in);
    and, to read from a url
    Java Code:
    URL xmlUrl = new URL("file:///home/me/myfile.xml");
    InputStream in = xmlUrl.openStream();
    Document doc = parse(in);

  3. #3
    jforce93 is offline Member
    Join Date
    Aug 2009
    Posts
    24
    Rep Power
    0

Similar Threads

  1. data structure and data base??
    By ahmed13 in forum Advanced Java
    Replies: 8
    Last Post: 03-27-2009, 05:48 AM
  2. how to store the data in data base
    By eclipse3.4ide in forum New To Java
    Replies: 5
    Last Post: 02-03-2009, 04:25 AM
  3. error while retrieving data from data base
    By kirtesh4u in forum New To Java
    Replies: 5
    Last Post: 11-15-2008, 04:10 PM
  4. Replies: 0
    Last Post: 10-31-2008, 02:13 PM
  5. Data Sorting in a .data file using java
    By stutiger99 in forum New To Java
    Replies: 2
    Last Post: 10-08-2008, 02:52 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •