Read From XML file

Can anybody tell me how to read the contents of a particular tag within an XML file?

[91 byte] By [paul_carrona] at [2007-11-27 10:54:29]
# 1

http://java.sun.com/xml/tutorial_intro.html

CeciNEstPasUnProgrammeura at 2007-7-29 11:50:32 > top of Java-index,Java Essentials,New To Java...
# 2

http://www.developerfusion.co.uk/show/2064/

gracecheaha at 2007-7-29 11:50:32 > top of Java-index,Java Essentials,New To Java...
# 3

The above example uses:

import org.w3c.dom.Document;

and I have already used the following in my program:

import org.jdom.Document;

Using both causes a conflict and if I change to w3c I get errors. Can anybody tell me what is the difference here?

paul_carrona at 2007-7-29 11:50:32 > top of Java-index,Java Essentials,New To Java...
# 4

Cheers guys, those helped. I used that program from the developerfusion and changed it to suit my own program. Give me an idea of how the XML is read. I now have this:

import java.io.File;

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 FindFile{

public static void main (String args []){

try {

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();

DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

Document doc = docBuilder.parse (new File("Files.xml"));

// normalize text representation

doc.getDocumentElement ().normalize ();

System.out.println ("Root element of the doc is : " +

doc.getDocumentElement().getNodeName());

//number of files

NodeList fileDetails = doc.getElementsByTagName("file_details");

int totalFiles = fileDetails.getLength();

System.out.println("Total no of files : " + totalFiles);

for(int s=0; s<fileDetails.getLength() ; s++){

Node countryNode = fileDetails.item(s);

if(countryNode.getNodeType() == Node.ELEMENT_NODE){

Element fileElement = (Element)countryNode;

//Get the country each file applys to

NodeList countryList = fileElement.getElementsByTagName("country");

Element countryNameElement = (Element)countryList.item(0);

NodeList textCountryList = countryNameElement.getChildNodes();

System.out.println("Country Name : " +

((Node)textCountryList.item(0)).getNodeValue().trim());

//Get the letter each file begins with

NodeList fileBeginsWith = fileElement.getElementsByTagName("starts_with");

Element lastNameElement = (Element)fileBeginsWith.item(0);

NodeList textBeginsWithList = lastNameElement.getChildNodes();

System.out.println("File Name Begins With : " +

((Node)textBeginsWithList.item(0)).getNodeValue().trim());

//find the extension type

NodeList extensionType = fileElement.getElementsByTagName("extension");

Element extensionElement = (Element)extensionType.item(0);

NodeList textExtensionList = extensionElement.getChildNodes();

System.out.println("The extension type is : " +

((Node)textExtensionList.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 ();

}

//Was commented out

System.exit (0);

}

}

I also have this piece of code which allows me to find a file. Do you know how I could include it in the above program so that instead of hard coding the startsWith and endsWith the startsWith and endsWith are read in from my XML?

import java.io.File;

public class FindLatestFile {

public static File getLatest(File thisDir){

long latestModDate = -1;

File latestFile = null;

File[] fileList = thisDir.listFiles();

for(int i=0; i >< fileList.length; i++){

File file = fileList[i];

if (file.lastModified() > latestModDate & (file.getName().startsWith("A") & file.getName().endsWith(".txt"))) {

latestModDate = file.lastModified();

latestFile = file;

}

}

return latestFile;

}

}

By the way this is my XML:

<?xml version="1.0" encoding="UTF-8"?>

<files>

<file_details>

<country>Ireland</country>

<starts_with>A</starts_with>

<extension>.txt</extension>

</file_details>

<file_details>

<country>UK</country>

<starts_with>B</starts_with>

<extension>.txt</extension>

</file_details>

</files>

paul_carrona at 2007-7-29 11:50:32 > top of Java-index,Java Essentials,New To Java...