Dear Buddies,
I have two Java Prog:
My task is to read the Content of the file and write to an XML file.
so i started doing it perfectly...
What i have problem is when i tried to execute the main class, i am not able to reach the book class yet. it says like this
My error comes under loadData() method.
cannot find symbol
symbol: Constructor book(java,lang, String)
location: class.java.awt. print.Book
because i used line by line command to read from file and there is no way to read that way in Book class???( parameter is not passed int hat way)
can you help me to solve this problem.
I realy got stuck with this
Thanks
My progs are
1>
package java.Associatio ns;
public class Book {
private String title;
private String author;
private String subject;
public Book(String subject) {
this.subject = subject;
}
public Book() {
}
public Book(String title, String author, String subject) {
this.title = title;
this.author = author;
this.subject = subject;
}
public String getAuthor() {
return author;
}
public void setAuthor(Strin g author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubject() {
return subject;
}
public void setSubject(Stri ng subject) {
this.subject = subject;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(" { Book Details --");
sb.append("Titl e:" + getTitle());
sb.append(", ");
sb.append("Auth or:" + getAuthor());
sb.append(", ");
sb.append("Subj ect:" + getSubject());
sb.append(". } \n");
return sb.toString();
}
}
2>
package java.Associatio ns;
import java.awt.print. Book;
import java.io.*;
import java.util.Array List;
import java.util.Itera tor;
import java.util.List;
import javax.xml.parse rs.DocumentBuil der;
import javax.xml.parse rs.DocumentBuil derFactory;
import javax.xml.parse rs.ParserConfig urationExceptio n;
import org.w3c.dom.Doc ument;
import org.w3c.dom.Ele ment;
import org.w3c.dom.Tex t;
//For jdk1.5 with built in xerces parser
//import com.sun.org.apa che.xml.interna l.serialize.Out putFormat;
//import com.sun.org.apa che.xml.interna l.serialize.XML Serializer;
//For JDK 1.3 or JDK 1.4 with xerces 2.7.1
import com.sun.org.apa che.xml.interna l.serialize.Out putFormat;
import com.sun.org.apa che.xml.interna l.serialize.XML Serializer;
public class XMLCreatorExamp le {
//No generics
List myData;
Document dom;
public XMLCreatorExamp le() {
//create a list to hold the data
myData = new ArrayList();
//initialize the list
loadData();
//Get a DOM object
createDocument( );
}
public void runExample(){
System.out.prin tln("Started .. ");
createDOMTree() ;
printToFile();
System.out.prin tln("Generated file successfully.") ;
}
/**
* Add a list of books to the list
* In a production system you might populate the list from a DB
*/
private void loadData(){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream ("C:\\coding\\1 .txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream (fstream);
BufferedReader br = new BufferedReader( new InputStreamRead er(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.prin tln (strLine);
myData.add(new Book(strLine));
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.prin tln("Error: " + e.getMessage()) ;
}
}
/**
* Using JAXP in implementation independent manner create a document object
* using which we create a xml tree in memory
*/
private void createDocument( ) {
//get an instance of factory
DocumentBuilder Factory dbf = DocumentBuilder Factory.newInst ance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocument Builder();
//create an instance of DOM
dom = db.newDocument( );
}catch(ParserCo nfigurationExce ption pce) {
//dump it
System.out.prin tln("Error while trying to instantiate DocumentBuilder " + pce);
System.exit(1);
}
}
/**
* The real workhorse which creates the XML structure
*/
private void createDOMTree() {
//create the root element <Books>
Element rootEle = dom.createEleme nt("Books");
dom.appendChild (rootEle);
//No enhanced for
Iterator it = myData.iterator ();
while(it.hasNex t()) {
Book b = (Book)it.next() ;
//For each Book object create <Book> element and attach it to root
Element bookEle = createBookEleme nt(b);
rootEle.appendC hild(bookEle);
}
}
/**
* Helper method which creates a XML element <Book>
* @param b The book for which we need to create an xml representation
* @return XML element snippet representing a book
*/
private Element createBookEleme nt(Book b){
Element bookEle = dom.createEleme nt("Book");
bookEle.setAttr ibute("Subject" , b.getSubject()) ;
//create author element and author text node and attach it to bookElement
Element authEle = dom.createEleme nt("Author");
Text authText = dom.createTextN ode(b.getAuthor ());
authEle.appendC hild(authText);
bookEle.appendC hild(authEle);
//create title element and title text node and attach it to bookElement
Element titleEle = dom.createEleme nt("Title");
Text titleText = dom.createTextN ode(b.getTitle( ));
titleEle.append Child(titleText );
bookEle.appendC hild(titleEle);
return bookEle;
}
/**
* This method uses Xerces specific classes
* prints the XML document to file.
*/
private void printToFile(){
try
{
//print
OutputFormat format = new OutputFormat(do m);
format.setInden ting(true);
//to generate output to console use this serializer
//XMLSerializer serializer = new XMLSerializer(S ystem.out, format);
//to generate a file output use fileoutputstrea m instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStrea m(new File("book.xml" )), format);
serializer.seri alize(dom);
} catch(IOExcepti on ie) {
ie.printStackTr ace();
}
}
public static void main(String args[]) {
//create an instance
XMLCreatorExamp le xce = new XMLCreatorExamp le();
//run the example
xce.runExample( );
}
}
I have two Java Prog:
My task is to read the Content of the file and write to an XML file.
so i started doing it perfectly...
What i have problem is when i tried to execute the main class, i am not able to reach the book class yet. it says like this
My error comes under loadData() method.
cannot find symbol
symbol: Constructor book(java,lang, String)
location: class.java.awt. print.Book
because i used line by line command to read from file and there is no way to read that way in Book class???( parameter is not passed int hat way)
can you help me to solve this problem.
I realy got stuck with this
Thanks
My progs are
1>
package java.Associatio ns;
public class Book {
private String title;
private String author;
private String subject;
public Book(String subject) {
this.subject = subject;
}
public Book() {
}
public Book(String title, String author, String subject) {
this.title = title;
this.author = author;
this.subject = subject;
}
public String getAuthor() {
return author;
}
public void setAuthor(Strin g author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getSubject() {
return subject;
}
public void setSubject(Stri ng subject) {
this.subject = subject;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(" { Book Details --");
sb.append("Titl e:" + getTitle());
sb.append(", ");
sb.append("Auth or:" + getAuthor());
sb.append(", ");
sb.append("Subj ect:" + getSubject());
sb.append(". } \n");
return sb.toString();
}
}
2>
package java.Associatio ns;
import java.awt.print. Book;
import java.io.*;
import java.util.Array List;
import java.util.Itera tor;
import java.util.List;
import javax.xml.parse rs.DocumentBuil der;
import javax.xml.parse rs.DocumentBuil derFactory;
import javax.xml.parse rs.ParserConfig urationExceptio n;
import org.w3c.dom.Doc ument;
import org.w3c.dom.Ele ment;
import org.w3c.dom.Tex t;
//For jdk1.5 with built in xerces parser
//import com.sun.org.apa che.xml.interna l.serialize.Out putFormat;
//import com.sun.org.apa che.xml.interna l.serialize.XML Serializer;
//For JDK 1.3 or JDK 1.4 with xerces 2.7.1
import com.sun.org.apa che.xml.interna l.serialize.Out putFormat;
import com.sun.org.apa che.xml.interna l.serialize.XML Serializer;
public class XMLCreatorExamp le {
//No generics
List myData;
Document dom;
public XMLCreatorExamp le() {
//create a list to hold the data
myData = new ArrayList();
//initialize the list
loadData();
//Get a DOM object
createDocument( );
}
public void runExample(){
System.out.prin tln("Started .. ");
createDOMTree() ;
printToFile();
System.out.prin tln("Generated file successfully.") ;
}
/**
* Add a list of books to the list
* In a production system you might populate the list from a DB
*/
private void loadData(){
try{
// Open the file that is the first
// command line parameter
FileInputStream fstream = new FileInputStream ("C:\\coding\\1 .txt");
// Get the object of DataInputStream
DataInputStream in = new DataInputStream (fstream);
BufferedReader br = new BufferedReader( new InputStreamRead er(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.prin tln (strLine);
myData.add(new Book(strLine));
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.prin tln("Error: " + e.getMessage()) ;
}
}
/**
* Using JAXP in implementation independent manner create a document object
* using which we create a xml tree in memory
*/
private void createDocument( ) {
//get an instance of factory
DocumentBuilder Factory dbf = DocumentBuilder Factory.newInst ance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocument Builder();
//create an instance of DOM
dom = db.newDocument( );
}catch(ParserCo nfigurationExce ption pce) {
//dump it
System.out.prin tln("Error while trying to instantiate DocumentBuilder " + pce);
System.exit(1);
}
}
/**
* The real workhorse which creates the XML structure
*/
private void createDOMTree() {
//create the root element <Books>
Element rootEle = dom.createEleme nt("Books");
dom.appendChild (rootEle);
//No enhanced for
Iterator it = myData.iterator ();
while(it.hasNex t()) {
Book b = (Book)it.next() ;
//For each Book object create <Book> element and attach it to root
Element bookEle = createBookEleme nt(b);
rootEle.appendC hild(bookEle);
}
}
/**
* Helper method which creates a XML element <Book>
* @param b The book for which we need to create an xml representation
* @return XML element snippet representing a book
*/
private Element createBookEleme nt(Book b){
Element bookEle = dom.createEleme nt("Book");
bookEle.setAttr ibute("Subject" , b.getSubject()) ;
//create author element and author text node and attach it to bookElement
Element authEle = dom.createEleme nt("Author");
Text authText = dom.createTextN ode(b.getAuthor ());
authEle.appendC hild(authText);
bookEle.appendC hild(authEle);
//create title element and title text node and attach it to bookElement
Element titleEle = dom.createEleme nt("Title");
Text titleText = dom.createTextN ode(b.getTitle( ));
titleEle.append Child(titleText );
bookEle.appendC hild(titleEle);
return bookEle;
}
/**
* This method uses Xerces specific classes
* prints the XML document to file.
*/
private void printToFile(){
try
{
OutputFormat format = new OutputFormat(do m);
format.setInden ting(true);
//to generate output to console use this serializer
//XMLSerializer serializer = new XMLSerializer(S ystem.out, format);
//to generate a file output use fileoutputstrea m instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStrea m(new File("book.xml" )), format);
serializer.seri alize(dom);
} catch(IOExcepti on ie) {
ie.printStackTr ace();
}
}
public static void main(String args[]) {
//create an instance
XMLCreatorExamp le xce = new XMLCreatorExamp le();
//run the example
xce.runExample( );
}
}
Comment