12 Replies - 11282 Views - Last Post: 08 February 2012 - 03:26 AM Rate Topic: -----

#1 Mister_Mac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-April 11

Scanning and extracting from an XML file

Posted 07 February 2012 - 08:44 AM

Hello everybody,

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>,  


Is This A Good Question/Topic? 0
  • +

Replies To: Scanning and extracting from an XML file

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 09:02 AM

I personally wouldnt take your approach. If your allowed to use libraries, you should use SAX It is quite a well known library and used to handle xml file. If you cant use a library, I would work with Nodes and Document builder instead. There are quite a number of different ways to achieve what ur after.
Was This Post Helpful? 1
  • +
  • -

#3 Mister_Mac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-April 11

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 09:05 AM

View Postnick2price, on 07 February 2012 - 11:02 AM, said:

I personally wouldnt take your approach. If your allowed to use libraries, you should use SAX It is quite a well known library and used to handle xml file. If you cant use a library, I would work with Nodes and Document builder instead. There are quite a number of different ways to achieve what ur after.


Thank you for the recommendation! While it is too late for the current incarnation of the file (which will be submitted in 30-45 minutes), I will try to do it afterwards for future reference.
Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 09:22 AM

I havnt used the findWithinHorizon method before, but it looks like you shouldnt have this in your while loop. So try deleted that whole while loop, and change it to

String res = inputFile.findWithinHorizon("<TITLE>", 0);
while (res != null)
{
if (inputFile.hasNext()) 
    System.out.print(inputFile.next() + ", ");
else
    System.out.println("String not found");
}


Havnt tested it, see what that does for you.

This post has been edited by nick2price: 07 February 2012 - 09:23 AM

Was This Post Helpful? 1
  • +
  • -

#5 Mister_Mac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-April 11

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 09:37 AM

View Postnick2price, on 07 February 2012 - 11:22 AM, said:

I havnt used the findWithinHorizon method before, but it looks like you shouldnt have this in your while loop. So try deleted that whole while loop, and change it to

String res = inputFile.findWithinHorizon("<TITLE>", 0);
while (res != null)
{
if (inputFile.hasNext()) 
    System.out.print(inputFile.next() + ", ");
else
    System.out.println("String not found");
}


Havnt tested it, see what that does for you.



Thanks for the tip however unless I made a mistake in typing your suggestion, I get an endless loop of "String not found"

Here is the code with your suggestion added:
 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 (M_M)
	 * 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
		
		//@param Ask the user to name the destination file
		System.out.print("Name the destination file: ");
		String writefile = keyboard.nextLine();
		
		//This will create a file to be printed to
		PrintWriter outputFile = new PrintWriter(writefile);
		//What about if file is close, open it, while it has next read and print then close. Next line reopen.
			
		
		String res = inputFile.findWithinHorizon("<TITLE>",0);
		while (res !=null)
		{
			if (inputFile.hasNext())
				outputFile.print(inputFile.next() + ", ");
			else
				System.out.println("String not found");
		}
//		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("</TITLE>"); //M_M - Tells the program what is the end of the entry. 
//		if (inputFile.hasNext()) //M_M this works but is being temp. disabled for a new approach
//			outputFile.print(inputFile.next() + ", ");
//		else
//			outputFile.println("String not found");
//		}	
		
			 //TG "Close the file
		
		inputFile.close();//M_M - closes the input file
		outputFile.close();//M_M - closes the output file.

		
		
	} //M_M "End main method"

} //M_M "End Class ToFile"



I do appreciate the suggestion as well your help. :-)
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 565
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 09:57 AM

If this dont work, I will have to wait until I get home to test it out, unless someone else comes a long and helps you. Change what I just suggested to
String res = inputFile.findWithinHorizon("<TITLE>", 0);  
while (res != null)
{
    MatchResult lastMatch = inputFile.match();
    System.out.println(lastMatch);


    res = inputFile.findWithinHorizon("<TITLE>", 0);
}



You will need to import regex.
import java.util.regex.*;


Not sure if it will work, but test it out anyways and let us know.
Was This Post Helpful? 1
  • +
  • -

#7 jon.kiparsky  Icon User is offline

  • Chinga la migra
  • member icon


Reputation: 10720
  • View blog
  • Posts: 18,353
  • Joined: 19-March 11

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 10:13 AM

String res = inputFile.findWithinHorizon("<TITLE>", 0);
while (res != null)
{
if (inputFile.hasNext()) 
    System.out.print(inputFile.next() + ", ");
else
    System.out.println("String not found");
}




This loops endlessly because res is never changed. Once it gets to the end of file, it'll just keep repeating "String not found".


If you want to read XML, I agree that existing libraries are the way to go. There are a couple, try them out and see how they work for you.

If you want to parse XML - that is, if you want to try writing an XML parser, in order to learn how it's done - you want to think in terms of stacks. If you're familiar with stacks, it'll be obvious what's needed, if you're not, you should get yourself a copy of Sedgewick's Algorithms and work through it.
Was This Post Helpful? 1
  • +
  • -

#8 Mister_Mac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-April 11

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 10:18 AM

Hi everybody, I have spoken with my professor about the data dump and she informs me that I may be going about this the wrong way. She sat down and explained that she wanted the output to look like the following:

Title, Artist, Country,..... Year
T1, A1, C1, .... Y1
T2, A2, C2, ....Y2
T3, A3, C3, .... Y3
.
.
.
.
Tn, An, C, .... Yn


She has granted me an extension to the end of the day so as of right now I will be taking a short lunch break, then resuming work before class begins.

Thank you for all your help, I'll make an update later to ensure I'm on track.
Was This Post Helpful? 0
  • +
  • -

#9 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 11:41 AM

Quote

I am using the Java Scanner class to read through the file,


You can't parse xml like that. There is no constraint in xml over the positions at which things appear. Xml doesn't know or care about lines.

Usually the most painless way to locate stuff in xml is with XPath

http://technojeeves....63-xpath-tester
Was This Post Helpful? 1
  • +
  • -

#10 Mister_Mac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-April 11

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 06:21 PM

View Postg00se, on 07 February 2012 - 01:41 PM, said:

Quote

I am using the Java Scanner class to read through the file,


You can't parse xml like that. There is no constraint in xml over the positions at which things appear. Xml doesn't know or care about lines.

Usually the most painless way to locate stuff in xml is with XPath

http://technojeeves....63-xpath-tester


Thanks for the suggestion g00se, but that is too much for me to take in (mentally absorb) right now.

What I am currently doing is searching the net for something that I could build off of, exactly what I did with Mr. Tony Gaddis code above.
I feel that way of doing this is best appropriate because 1) it actually forces me to do some work and 2) I can truthfully claim that I made a lot of effort
and that somebody else's code was only a foundation which mine was built off of.

However I thank you nonetheless for the recommendation and once I'm finished with this assignment I will read through it in depthly and try it.
Was This Post Helpful? 0
  • +
  • -

#11 Mister_Mac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-April 11

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 09:23 PM

Hello again, I found a website that would help and I have created this code based upon it.


 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;

/*
 * This is code found at the following website.
 * http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/
 * @author Kiran Pai
 * @additional modifications made by Mister Mac
 * @version 2012.Feb.07
 */
public class ReadAndPrintXMLFile {

	public static void main (String argv []) {
		try {
			//read the following right to left, newInstance of DocumentBuilderFactory with the varible docBUilderFactory
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File("CSIT430_a1_SP2012.txt"));//M_M changed books.xml to CSIT430_a1_SP2012.txt
			
			//normalize text representation
			doc.getDocumentElement().normalize();
			System.out.println ("Root element of the doc is " + 
					doc.getDocumentElement().getNodeName());
			
			NodeList listofCDs = doc.getElementsByTagName("CD"); //locate the number of cd's
			int totalCDs = listofCDs.getLength(); //M_M find how many CD's are listed assign it to variable totalCDs
			System.out.println("Total number of CD's : " + totalCDs);
			
			for (int s=0; s<listofCDs.getLength() ; s++) {
				
				Node firstCDnode = listofCDs.item(s);
				if(firstCDnode.getNodeType() == Node.ELEMENT_NODE) {
					
					Element firstCDelement = (Element)firstCDnode;
					
					//-------
					NodeList trackTitleList = firstCDelement.getElementsByTagName("TITLE"); //M_M - this will get the first element by XML tag "TITLE"
					Element trackTitleElement = (Element)trackTitleList.item(0);
					
					NodeList textTTLEList = trackTitleElement.getChildNodes();
					System.out.println("Track Title: " + ((Node)textTTLEList.item(0)).getNodeValue().trim());
					
					
					//-----
					
					NodeList trackArtistList = firstCDelement.getElementsByTagName("ARTIST");
					Element trackArtistElement = (Element)trackArtistList.item(0);
					
					NodeList textARTSTList = trackArtistElement.getChildNodes();
					System.out.println("Track Artist :" + ((Node)textARTSTList.item(0)).getNodeValue().trim());
					
					//------
					
					NodeList trackCountryList = firstCDelement.getElementsByTagName("COUNTRY");
					Element trackCountryElement = (Element)trackCountryList.item(0);
					
					NodeList textCRTYList = trackCountryElement.getChildNodes();
					System.out.println("Track Country: " + ((Node)textCRTYList.item(0)).getNodeValue().trim());
					
					//------------
					
					NodeList trackCompanyList = firstCDelement.getElementsByTagName("COMPANY");
					Element trackCompanyElement = (Element)trackCompanyList.item(0);
					
					NodeList textCMPNYList = trackCompanyElement.getChildNodes();
					System.out.println("Track Company: " + ((Node)textCMPNYList.item(0)).getNodeValue().trim());
					
					//-------------
					
					NodeList priceList = firstCDelement.getElementsByTagName("PRICE");
					Element priceElement = (Element)priceList.item(0);
					
					NodeList textPriceList = priceElement.getChildNodes();
					System.out.println("Price: " + ((Node)textPriceList.item(0)).getNodeValue().trim());
					
					//---------------
					
					NodeList yearList = firstCDelement.getElementsByTagName("YEAR");
					Element yearElement = (Element)yearList.item(0);
					
					NodeList textYearList = yearElement.getChildNodes();
					System.out.println("Year: " + ((Node)textYearList.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
	} //end of class





My new question is how would I format it that when I have it print to file (or even the console in this case) to format it like this


Title Artist Country Company Price Year
Hide your heart Bonnie Taylor UK CBS Records 9.90 1988

for each record?
Was This Post Helpful? 0
  • +
  • -

#12 Mister_Mac  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 26-April 11

Re: Scanning and extracting from an XML file

Posted 07 February 2012 - 11:20 PM

Hi there I think I almost has this completed but I can't figure out why I keep having an exception thrown and what can I do to fix it.
Here is the exception result

Root element of the doc is CATALOG
Total number of CD's : 26
java.util.UnknownFormatConversionException: Conversion = '1'
	at java.util.Formatter.checkText(Formatter.java:2519)
	at java.util.Formatter.parse(Formatter.java:2483)
	at java.util.Formatter.format(Formatter.java:2430)
	at java.util.Formatter.format(Formatter.java:2383)
	at ReadAndPrintXMLFile.main(ReadAndPrintXMLFile.java:106)



Line 106 points to the "newstringformat.format( ".....
section or at least according to eclipse.


import java.io.*; //needed for the PrintWriter class and IO Exception
import java.util.Formatter; //needed for string formatting

import org.w3c.dom.*;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

/*
 * This is code found at the following website.
 * http://www.developerfusion.com/code/2064/a-simple-way-to-read-an-xml-file-in-java/
 * @author Kiran Pai
 * @additional modifications made by Mister Mac
 * @version 2002.Feb.26
 */
public class ReadAndPrintXMLFile {

	public static void main (String argv [])throws IOException  { //Throws exception if file is not found.
		//M_M - This will create a file to be printed to
		FileWriter fwriter = new FileWriter("Mac_M_a1A.txt", true);
		PrintWriter outputFile = new PrintWriter(fwriter);
		
		//The following idea received from http://stackoverflow.com/questions/8154366/how-to-center-a-string-using-string-format
		outputFile.printf("|%1$-25s | %2$-20s | %3$-15s | %4$-25s | %5$-15s | %6$-5s| %n", "Title", "Artist", "Country", "Company", "Price", "Year");
		
			System.out.format("|%1$-25s | %2$-20s | %3$-15s | %4$-25s | %5$-15s | %6$-5s| %n", "Title", "Artist", "Country", "Company", "Price", "Year");
		//outputFile.printf( " |%1-30s    |%2-30s    |%3$-5s     |%4-50s   |%5-50s    |%6-50s    |%n", "Title", "Artist", "Country", "Company", "Price", "Year");
		
		
		try {
			//read the following right to left, newInstance of DocumentBuilderFactory with the varible docBUilderFactory
			DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
			DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
			Document doc = docBuilder.parse(new File("CSIT430_a1_SP2012.txt"));//M_M changed books.xml to input.xml
			
			//normalize text representation
			doc.getDocumentElement().normalize();
			System.out.println ("Root element of the doc is " + 
					doc.getDocumentElement().getNodeName());
			
			NodeList listofCDs = doc.getElementsByTagName("CD"); //locate the number of cd's
			int totalCDs = listofCDs.getLength(); //M_M find how many CD's are listed assign it to variable totalCDs
			System.out.println("Total number of CD's : " + totalCDs);
			
			for (int s=0; s<listofCDs.getLength() ; s++) {
				
				Node firstCDnode = listofCDs.item(s);
				if(firstCDnode.getNodeType() == Node.ELEMENT_NODE) {
					
					Element firstCDelement = (Element)firstCDnode;
					
					//-------
					NodeList trackTitleList = firstCDelement.getElementsByTagName("TITLE"); //M_M - this will get the first element by XML tag "TITLE"
					Element trackTitleElement = (Element)trackTitleList.item(0);
					
					NodeList textTTLEList = trackTitleElement.getChildNodes();
//					System.out.println("Track Title: " + ((Node)textTTLEList.item(0)).getNodeValue().trim());
					
					
					//-----
					
					NodeList trackArtistList = firstCDelement.getElementsByTagName("ARTIST");
					Element trackArtistElement = (Element)trackArtistList.item(0);
					
					NodeList textARTSTList = trackArtistElement.getChildNodes();
//					System.out.println("Track Artist :" + ((Node)textARTSTList.item(0)).getNodeValue().trim());
					
					//------
					
					NodeList trackCountryList = firstCDelement.getElementsByTagName("COUNTRY");
					Element trackCountryElement = (Element)trackCountryList.item(0);
					
					NodeList textCRTYList = trackCountryElement.getChildNodes();
//					System.out.println("Track Country: " + ((Node)textCRTYList.item(0)).getNodeValue().trim());
					
					//------------
					
					NodeList trackCompanyList = firstCDelement.getElementsByTagName("COMPANY");
					Element trackCompanyElement = (Element)trackCompanyList.item(0);
					
					NodeList textCMPNYList = trackCompanyElement.getChildNodes();
//					System.out.println("Track Company: " + ((Node)textCMPNYList.item(0)).getNodeValue().trim());
					
					//-------------
					
					NodeList priceList = firstCDelement.getElementsByTagName("PRICE");
					Element priceElement = (Element)priceList.item(0);
					
					NodeList textPriceList = priceElement.getChildNodes();
//					System.out.println("Price: " + ((Node)textPriceList.item(0)).getNodeValue().trim());
					
					//---------------
					
					NodeList yearList = firstCDelement.getElementsByTagName("YEAR");
					Element yearElement = (Element)yearList.item(0);
					
					NodeList textYearList = yearElement.getChildNodes();
//					System.out.println("Year: " + ((Node)textYearList.item(0)).getNodeValue().trim());
					
					//------------------
					
					Formatter newstringformat = new Formatter();
					newstringformat.format( "|%1-50s|%2-50s|%3$-5s|%4-50s|%5-50s|%6-50s|%n", ((Node) textTTLEList.item(0)).getNodeValue(), 
							((Node) textARTSTList.item(0)).getNodeValue(),((Node) textCRTYList.item(0)).getNodeValue(), 
							((Node) textCRTYList.item(0)).getNodeValue(), ((Node) textCMPNYList.item(0)).getNodeValue(),
							((Node) textPriceList.item(0)).getNodeValue(),((Node) textYearList.item(0)).getNodeValue());
					
					outputFile.print(newstringformat);
					
				} //end of if clause
			} //end of for loop with s var
			outputFile.close();
			
		}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
	} //end of class


Was This Post Helpful? 0
  • +
  • -

#13 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3464
  • View blog
  • Posts: 15,676
  • Joined: 20-September 08

Re: Scanning and extracting from an XML file

Posted 08 February 2012 - 03:26 AM

The format string appearing now at line 105 above is wrong. Try something like the following. If it's not pretty enough, at least it won't throw an exception:

newstringformat.format("|%-50s|%-50s|%-50s|%-50s|%-50s|%-50s|\n", <REST FOLLOWS HERE>

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1