I am working on a Java assignment in which I need to read from an XML file, print and organize the data to a text file. I have been working on this for 3 weeks and while it is far from finished this is where I am at.
I can access the file, find a specific tag (such as <TITLE>) and print (currently to the console) the results, however after finding the selected data, I receive at the end of the print what I call a "data dump" of currently unneeded information. I am using the Java Scanner class to read through the file, the "findWithinHorizion" method to find the tag and "useDelimiter" to tell the scanner where to stop in pulling information.
My first question for this post is where and in my code can I improve to fix this issue?
Thank you,
Mister_Mac
Java Code below
package org.ToFile;
import java.io.*;//TG - Needed for PrintWriter and IOExecption
import java.util.Scanner; //TG - Needed for Scanner
public class FileIO {
/**This file read and writes information from/to an external file
* This program was created by @author Mister_Mac (MM)
* based off of code from @author Tony Gaddis (TG) from his book "Starting out with Java"
* @version 2012.Feb.07
* @param Read and display results from a file.
*/
public static void main(String[] args) throws IOException //M_M -needed to prevent program from crashing(?) in an event the file isn't available.
{
//TG "Create a scanner object for keyboard input
Scanner keyboard = new Scanner(System.in);
//TG 'Get the file name"
System.out.print("Enter the filename: ");
String filename = keyboard.nextLine();
//TG "Make sure the file exists.
File file = new File(filename);
if (!file.exists())
{
//TG "Display an error message.
System.out.println("The file " + filename + " does not exist.");
//TG "exit the program.
System.exit(0);
}
//TG - Open the file.
Scanner inputFile = new Scanner(file); //works but being temp. disabled for new approach
// Scanner inputFile = new Scanner(new FileReader(file));
while (inputFile.hasNext()) //works but being temp. disabled for new approach
{
//M_M - find in the file a specific string, with 0 being the entire file.
inputFile.findWithinHorizon("<TITLE>", 0); //M_M This works but is being temp. disabled for a new approach.
inputFile.useDelimiter("</"); //M_M - This works but is being temp. disabled for a new approach //M_M - Tells the program what is the end of the entry.
// if (inputFile.hasNext());
// inputFile.findWithinHorizon("<TITLE>", 0);
// System.out.print(inputFile.next() + ", ");
//
//
// if (inputFile.hasNext()); //ERROR EXECPTION THROWN
// inputFile.findWithinHorizon("<ARTIST>", 0);
// System.out.print(inputFile.next() + ", ");
if (inputFile.hasNext()) //M_M this works but is being temp. disabled for a new approach
System.out.print(inputFile.next() + ", ");
// System.out.print("The song titles include: " + inputFile.next() + ", ");//M_M - Print all entries on one line, separated by a comma, then a space.
else
System.out.println("String not found");
}
//TG "Close the file
inputFile.close();
} //M_M "End main method"
} //M_M "End Class ToFile"
The following is the XML file I need to read from
<?xml version="1.0" ?> <!-- Edited by XMLSpy® --> <!--obtained from http://www.w3schools.com/xml/cd_catalog.xml--> <CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tyler</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> <CD> <TITLE>Greatest Hits</TITLE> <ARTIST>Dolly Parton</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>RCA</COMPANY> <PRICE>9.90</PRICE> <YEAR>1982</YEAR> </CD> <CD> <TITLE>Still got the blues</TITLE> <ARTIST>Gary Moore</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Virgin records</COMPANY> <PRICE>10.20</PRICE> <YEAR>1990</YEAR> </CD> <CD> <TITLE>Eros</TITLE> <ARTIST>Eros Ramazzotti</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>BMG</COMPANY> <PRICE>9.90</PRICE> <YEAR>1997</YEAR> </CD> <CD> <TITLE>One night only</TITLE> <ARTIST>Bee Gees</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Polydor</COMPANY> <PRICE>10.90</PRICE> <YEAR>1998</YEAR> </CD> <CD> <TITLE>Sylvias Mother</TITLE> <ARTIST>Dr.Hook</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS</COMPANY> <PRICE>8.10</PRICE> <YEAR>1973</YEAR> </CD> <CD> <TITLE>Maggie May</TITLE> <ARTIST>Rod Stewart</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Pickwick</COMPANY> <PRICE>8.50</PRICE> <YEAR>1990</YEAR> </CD> <CD> <TITLE>Romanza</TITLE> <ARTIST>Andrea Bocelli</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Polydor</COMPANY> <PRICE>10.80</PRICE> <YEAR>1996</YEAR> </CD> <CD> <TITLE>When a man loves a woman</TITLE> <ARTIST>Percy Sledge</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Atlantic</COMPANY> <PRICE>8.70</PRICE> <YEAR>1987</YEAR> </CD> <CD> <TITLE>Black angel</TITLE> <ARTIST>Savage Rose</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Mega</COMPANY> <PRICE>10.90</PRICE> <YEAR>1995</YEAR> </CD> <CD> <TITLE>1999 Grammy Nominees</TITLE> <ARTIST>Many</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Grammy</COMPANY> <PRICE>10.20</PRICE> <YEAR>1999</YEAR> </CD> <CD> <TITLE>For the good times</TITLE> <ARTIST>Kenny Rogers</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Mucik Master</COMPANY> <PRICE>8.70</PRICE> <YEAR>1995</YEAR> </CD> <CD> <TITLE>Big Willie style</TITLE> <ARTIST>Will Smith</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>9.90</PRICE> <YEAR>1997</YEAR> </CD> <CD> <TITLE>Tupelo Honey</TITLE> <ARTIST>Van Morrison</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Polydor</COMPANY> <PRICE>8.20</PRICE> <YEAR>1971</YEAR> </CD> <CD> <TITLE>Soulsville</TITLE> <ARTIST>Jorn Hoel</ARTIST> <COUNTRY>Norway</COUNTRY> <COMPANY>WEA</COMPANY> <PRICE>7.90</PRICE> <YEAR>1996</YEAR> </CD> <CD> <TITLE>The very best of</TITLE> <ARTIST>Cat Stevens</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Island</COMPANY> <PRICE>8.90</PRICE> <YEAR>1990</YEAR> </CD> <CD> <TITLE>Stop</TITLE> <ARTIST>Sam Brown</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>A and M</COMPANY> <PRICE>8.90</PRICE> <YEAR>1988</YEAR> </CD> <CD> <TITLE>Bridge of Spies</TITLE> <ARTIST>T'Pau</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Siren</COMPANY> <PRICE>7.90</PRICE> <YEAR>1987</YEAR> </CD> <CD> <TITLE>Private Dancer</TITLE> <ARTIST>Tina Turner</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Capitol</COMPANY> <PRICE>8.90</PRICE> <YEAR>1983</YEAR> </CD> <CD> <TITLE>Midt om natten</TITLE> <ARTIST>Kim Larsen</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Medley</COMPANY> <PRICE>7.80</PRICE> <YEAR>1983</YEAR> </CD> <CD> <TITLE>Pavarotti Gala Concert</TITLE> <ARTIST>Luciano Pavarotti</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>DECCA</COMPANY> <PRICE>9.90</PRICE> <YEAR>1991</YEAR> </CD> <CD> <TITLE>The dock of the bay</TITLE> <ARTIST>Otis Redding</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Atlantic</COMPANY> <PRICE>7.90</PRICE> <YEAR>1987</YEAR> </CD> <CD> <TITLE>Picture book</TITLE> <ARTIST>Simply Red</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Elektra</COMPANY> <PRICE>7.20</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Red</TITLE> <ARTIST>The Communards</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>London</COMPANY> <PRICE>7.80</PRICE> <YEAR>1987</YEAR> </CD> <CD> <TITLE>Unchain my heart</TITLE> <ARTIST>Joe Cocker</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>EMI</COMPANY> <PRICE>8.20</PRICE> <YEAR>1987</YEAR> </CD> </CATALOG>
The results I receive
Enter the filename: Input.xml Empire Burlesque, Hide your heart, Greatest Hits, Still got the blues, Eros, One night only, Sylvias Mother, Maggie May, Romanza, When a man loves a woman, Black angel, 1999 Grammy Nominees, For the good times, Big Willie style, Tupelo Honey, Soulsville, The very best of, Stop, Bridge of Spies, Private Dancer, Midt om natten, Pavarotti Gala Concert, The dock of the bay, Picture book, Red, Unchain my heart, TITLE> <ARTIST>Joe Cocker, ARTIST> <COUNTRY>USA, COUNTRY> <COMPANY>EMI, COMPANY> <PRICE>8.20, PRICE> <YEAR>1987, YEAR> , CD> , CATALOG>,

New Topic/Question
Reply



MultiQuote





|