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.
Code Java:
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
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
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?
Re: Please Help.SAX Readers and DOM
Quote:
Can you correct this?
Yes. Can you use code tags? =p