org.w3c.dom.DOMException:WRONG_DOCUMENT_ERR
Hi there,
I am going over the Begining Java Services book from Wrox. While running the folowing code I am getting an error shown below the code, let me know how to fix this as I am new to this.
package com.wrox.jws.stockcore;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Attr;
import org.apache.xerces.dom.DocumentImpl;
import java.sql.*;
import java.util.GregorianCalendar;
import java.io.PrintWriter;
import java.io.FileWriter;
public class StockCoreDOMGenerator
{
private Document doc;
private static String TYPES[] = {"ask", "open", "dayhigh", "daylow"};
private static final String STOCK_FILE = "stock_quote.xml";
private static interface Markup
{
public static final String STOCK_QUOTES = "stock_quotes";
public static final String STOCK_QUOTE = "stock_quote";
public static final String SYMBOL = "symbol";
public static final String WHEN = "when";
public static final String DATE = "date";
public static final String TIME = "time";
public static final String PRICE = "price";
public static final String TYPE = "type";
public static final String VALUE = "value";
public static final String CHANGE = "change";
public static final String VOLUME = "volume";
}
private static interface Database
{
//public static final String URL = "jdbc:cloudscape:C:/Wrox/BegJWS/src/com/wrox/jws/stockquote/Stocks";//"jdbc:mysql:/localhost:3306/Quotes";
public static final String URL = "jdbc:mysql://localhost:3306/bookcode";
public static final String USER = "root";
public static final String PASSWD = "hello123";
public static final String SQL = "SELECT * FROM quotes";
//public static final String DRIVER = "COM.cloudscape.core.JDBCDriver";
//public static final String DRIVER = "org.gjt.mm.mysql.Driver";
public static final String DRIVER = "com.mysql.jdbc.Driver";
}
public StockCoreDOMGenerator()
{
doc = new DocumentImpl();
Element root = doc.createElement(Markup.STOCK_QUOTES);
doc.appendChild(root);
}
private void addStock(String symbol, String quote[], String change,
String volume)
{
GregorianCalendar cal = new GregorianCalendar();
String date = cal.get(cal.YEAR) + "-" + cal.get(cal.MONTH) +
"-" + cal.get(cal.DATE) ;
String time = cal.get(cal.HOUR_OF_DAY) + ":" + cal.get(cal.MINUTE);
Element root = doc.getDocumentElement();
Element stockQuoteEl = doc.createElement(Markup.STOCK_QUOTE);
root.appendChild(stockQuoteEl);
Element symbolEl = doc.createElement(Markup.SYMBOL);
symbolEl.appendChild(doc.createTextNode(symbol));
stockQuoteEl.appendChild(symbolEl);
Element whenEl = doc.createElement(Markup.WHEN);
Element dateEl = doc.createElement(Markup.DATE);
dateEl.appendChild(doc.createTextNode(date));
whenEl.appendChild(dateEl);
Element timeEl = doc.createElement(Markup.TIME);
timeEl.appendChild(doc.createTextNode(time));
whenEl.appendChild(timeEl);
stockQuoteEl.appendChild(whenEl);
for(int i = 0;i < 4;i++)
{
Element priceEl = doc.createElement(Markup.PRICE);
priceEl.setAttribute(Markup.TYPE, TYPES);
priceEl.setAttribute(Markup.VALUE, quote);
stockQuoteEl.appendChild(priceEl);
}
Element changeEl = doc.createElement(Markup.CHANGE);
changeEl.appendChild(doc.createTextNode("+0.239"));
stockQuoteEl.appendChild(changeEl);
Element volumeEl = doc.createElement(Markup.VOLUME);
volumeEl.appendChild(doc.createTextNode("67552600"));
stockQuoteEl.appendChild(volumeEl);
}
private void saveDocument() throws Exception
{
PrintWriter writer = new PrintWriter(new FileWriter(STOCK_FILE));
writer.println(((DocumentImpl)doc).saveXML(doc));
writer.close();
}
public static void main(String args[]) throws Exception
{
StockCoreDOMGenerator generator = new StockCoreDOMGenerator();
Class.forName(Database.DRIVER).newInstance();
//Properties props = new Properties();
//props.put("user", Database.USER );
//props.put("password", Database.PASSWD );
Connection con = DriverManager.getConnection(Database.URL, "root","hello123"); //, props;
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(Database.SQL);
try
{
while(res.next())
{
generator.addStock(res.getString(1), new String[]{res.getString(2),
res.getString(3),res.getString(4),res.getString(5)},
res.getString(6),res.getString(7));
}
}
finally
{
if(res != null) res.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
}
generator.saveDocument();
System.out.println("Stock quotes saved successfully");
}
}
ERROR
D:\JWSExample\Chp02\classes>java com.wrox.jws.stockcore.StockCoreDOMGenerator
Exception in thread "main" org.w3c.dom.DOMException: WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it.
at org.apache.xerces.dom.CoreDocumentImpl.saveXML(Unknown Source) at com.wrox.jws.stockcore.StockCoreDOMGenerator.saveDocument(StockCoreDOMGenerator.java:108)
at com.wrox.jws.stockcore.StockCoreDOMGenerator.main(StockCoreDOMGenerator.java:142)

