• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Strange error from xerces 2_7_1

 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm trying to run an XML code sample directly from source code I downloaded from a book's website. The code simply reads a few records from a database and then constucts a DOM tree, then outputs the xml to a file.

If anyone can help me figure out what's going on with xerces, I would really appreciate the reply!!! I have asterisks around the actual line of code in the code listing below that's causing the error you see below.

Here's the error in the main method when trying to save the xml:

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.stockstore.StockCoreDOMGenerator.saveDocument(StockCoreDOMGenerator.java:112)
at com.wrox.jws.stockstore.StockCoreDOMGenerator.main(StockCoreDOMGenerator.java:146)

==============================

The xml processing code is:

package com.wrox.jws.stockstore;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
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; // xml docment to hold stock quotes
private static String TYPES[] = {"ask", "open", "dayhigh", "daylow"};
private static final String STOCK_FILE = "stock_quote.xml";

// interface below has the names of the elements and attributes used to build the stock
// quotes data

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";
}


// interface below has the various properties to conect to the database.
private static interface Database
{
public static final String URL = "jdbc:mysql://localhost/JWS_XML_Examples?user=itsme&password=1234";
public static final String SQL = "SELECT * FROM Quotes";
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[i]);
priceEl.setAttribute(Markup.VALUE, quote[i]);
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));
// ****************************************************
// ****************************************************
// ERROR listed above HAPPENS ON println below
writer.println(((DocumentImpl)doc).saveXML(doc));
// ****************************************************
// ****************************************************
// ****************************************************

writer.close();

}

public static void main(String args[]) throws Exception
{
StockCoreDOMGenerator generator = new StockCoreDOMGenerator();

Class.forName(Database.DRIVER);
//Properties props = new Properties();
//props.put("user", Database.USER );
//props.put("password", Database.PASSWD );

Connection con = DriverManager.getConnection(Database.URL); //, 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");

}
}
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use the importNode() method of Document to "import" a node from one document to another.
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Paul,

Could you be a little more specific relative to the code I posted? Where would I add that logic?

The code I posted came directly from the Author's site, so I'm puzzled why it doesn't work.

Thanks very much for your next reply.

-- M
 
Rajagopal Manohar
Ranch Hand
Posts: 183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Mike London:
Hi Paul,

Could you be a little more specific relative to the code I posted? Where would I add that logic?

The code I posted came directly from the Author's site, so I'm puzzled why it doesn't work.

Thanks very much for your next reply.

-- M

Mike,

In case you want to serialise the whole of your document it is enough to pass null to your saveXML document
 
Mike London
Ranch Hand
Posts: 1192
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup, that worked!!!

THANK YOU!!!

-- Mike
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic