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.Associations;
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(String 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(String subject) {
this.subject = subject;
}
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append(" { Book Details --");
sb.append("Title:" + getTitle());
sb.append(", ");
sb.append("Author:" + getAuthor());
sb.append(", ");
sb.append("Subject:" + getSubject());
sb.append(". } \n");
return sb.toString();
}
}
2>
package java.Associations;
import java.awt.print.Book;
import java.io.*;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
//For jdk1.5 with built in xerces parser
//import com.sun.org.apache.xml.internal.serialize.OutputFo rmat;
//import com.sun.org.apache.xml.internal.serialize.XMLSeria lizer;
//For JDK 1.3 or JDK 1.4 with xerces 2.7.1
import com.sun.org.apache.xml.internal.serialize.OutputFo rmat;
import com.sun.org.apache.xml.internal.serialize.XMLSeria lizer;
public class XMLCreatorExample {
//No generics
List myData;
Document dom;
public XMLCreatorExample() {
//create a list to hold the data
myData = new ArrayList();
//initialize the list
loadData();
//Get a DOM object
createDocument();
}
public void runExample(){
System.out.println("Started .. ");
createDOMTree();
printToFile();
System.out.println("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 InputStreamReader(in));
String strLine;
//Read File Line By Line
while ((strLine = br.readLine()) != null) {
// Print the content on the console
System.out.println (strLine);
myData.add(new Book(strLine));
}
//Close the input stream
in.close();
}catch (Exception e){//Catch exception if any
System.err.println("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
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
//get an instance of builder
DocumentBuilder db = dbf.newDocumentBuilder();
//create an instance of DOM
dom = db.newDocument();
}catch(ParserConfigurationException pce) {
//dump it
System.out.println("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.createElement("Books");
dom.appendChild(rootEle);
//No enhanced for
Iterator it = myData.iterator();
while(it.hasNext()) {
Book b = (Book)it.next();
//For each Book object create <Book> element and attach it to root
Element bookEle = createBookElement(b);
rootEle.appendChild(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 createBookElement(Book b){
Element bookEle = dom.createElement("Book");
bookEle.setAttribute("Subject", b.getSubject());
//create author element and author text node and attach it to bookElement
Element authEle = dom.createElement("Author");
Text authText = dom.createTextNode(b.getAuthor());
authEle.appendChild(authText);
bookEle.appendChild(authEle);
//create title element and title text node and attach it to bookElement
Element titleEle = dom.createElement("Title");
Text titleText = dom.createTextNode(b.getTitle());
titleEle.appendChild(titleText);
bookEle.appendChild(titleEle);
return bookEle;
}
/**
* This method uses Xerces specific classes
* prints the XML document to file.
*/
private void printToFile(){
try
{
//print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);
//to generate output to console use this serializer
//XMLSerializer serializer = new XMLSerializer(System.out, format);
//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStream(new File("book.xml")), format);
serializer.serialize(dom);
} catch(IOException ie) {
ie.printStackTrace();
}
}
public static void main(String args[]) {
//create an instance
XMLCreatorExample xce = new XMLCreatorExample();
//run the example
xce.runExample();
}
}