Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Please Help.SAX Readers and DOM

  1. #1
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Please Help.SAX Readers and DOM

    Hi People.I was trying to access the data from an URL.Below are my codes.Can anybody correct me? I am sort of new to this.

    package com.java.connect.dom4j;
     
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.List;  
    import java.util.jar.Attributes.Name;
    import org.dom4j.*;
    import org.dom4j.io.SAXReader;
     
    //import org.dom4j.Document;
    //import org.dom4j.DocumentException;
    //import org.dom4j.Node;
    //import org.dom4j.io.SAXReader;
     
     
    public class Document 
    {
     
        public static void main(String[] args)
        {
            // Get the SAXReader object
            SAXReader reader = new SAXReader();
            // Get the xml document object by sax reader.
            org.dom4j.Document document = null;
    	try
           {
    			document = reader.read((new URL("http://www.fao.org/countryprofiles/geoinfo/ws/allCountries/EN.xml")));
    	}
    	catch (MalformedURLException e)
           {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    	}
            catch (DocumentException e)
           {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    	}
            // Define the xpath
            String xpathExpression = "//Data/self_governing";
            // Get the list of nodes on given xPath
    	private org.dom4j.Document document;
            List <Node> nodes = document.selectNodes(xpathExpression);
     
            // Read all the node inside xpath nodes and print the value of each
            for (Node node : nodes) {
                Node codeISO3= node.selectSingleNode("codeISO3");
                System.out.println("CodeISO3 : " + codeISO3.getText());
     
                Node name = node.selectSingleNode("codeISO2");
                System.out.println("CodeISO2 : " + name.getText());
     
                Node officialname = node.selectSingleNode("nameOfficialEN");
                System.out.println("Official Name : " + officialname.getText());
     
                Node shortname = node.selectSingleNode("nameShortEN");
                System.out.println("Short Name : " + shortname.getText());
     
                Node namelist = node.selectSingleNode("nameListEN");
                System.out.println("Name List: " + namelist.getText());
     
                Node fao = node.selectSingleNode("FAO_MEMBERS");
                System.out.println("FAO members : " + fao.getText());
     
            }
        }
     
    }


    These are the errors I get.



    The import node cannot be resolved line 5
    node cannot be resolved to a type line 9
    The method read(URL) is undefined for the type SAXReader line 26
    The method selectNodes(String) from the type Document refers to the missing type node line 39


    Somebody please help


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Please Help.SAX Readers and DOM

    Your package name should be something like "myproject". You shouldn't bother with reversed domain names unless you're planning to produce masses of code for use by other people and want to 'brand' your packages. "myproject" or "saxthingumy" are fine. "com.java.anything" seems like a recipe for disaster: you risk a name-space collision with somebody who has some genuine claim to the com.java domain.

    Your URL is wrong in your code. If you open it in a browser, you get a big warning that it's wrong. Here's a correct one:
    http://www.fao.org/countryprofiles/g...llCountries/EN

    Your error messages don't appear to match up with the code you've posted. Make sure you don't edit your code between copy-pasting it and copy-pasting the errors.

    Use code tags - follow the link below to learn more about them:
    BB Code List - Java Programming Forums

  3. #3
    Junior Member
    Join Date
    Sep 2011
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Please Help.SAX Readers and DOM

    Thank You very much.

    I have removed all the errots except one.

    String xpathExpression = "//Data/self_governing/";
    List <Node> nodes = feed.selectNodes(xpathExpression); //Error Here...

    The error for this is

    Type mismatch: cannot convert from List<capture#1-of ? extends Node> to List<Node>

    Can you correct this?

  4. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Please Help.SAX Readers and DOM

    Can you correct this?
    Yes. Can you use code tags? =p