This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes Strange error from xerces 2_7_1 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Strange error from xerces 2_7_1" Watch "Strange error from xerces 2_7_1" New topic

Strange error from xerces 2_7_1

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1114

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(
at com.wrox.jws.stockstore.StockCoreDOMGenerator.main(


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;

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);


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);

Element symbolEl = doc.createElement(Markup.SYMBOL);

Element whenEl = doc.createElement(Markup.WHEN);
Element dateEl = doc.createElement(Markup.DATE);
Element timeEl = doc.createElement(Markup.TIME);

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]);

Element changeEl = doc.createElement(Markup.CHANGE);
Element volumeEl = doc.createElement(Markup.VOLUME);


private void saveDocument() throws Exception
PrintWriter writer = new PrintWriter(new FileWriter(STOCK_FILE));
// ****************************************************
// ****************************************************
// ERROR listed above HAPPENS ON println below
// ****************************************************
// ****************************************************
// ****************************************************



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

//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);

generator.addStock(res.getString(1), new String[]{res.getString(2),
if(res != null) res.close();
if(stmt != null) stmt.close();
if(con != null) con.close();


System.out.println("Stock quotes saved successfully");

Paul Clapham

Joined: Oct 14, 2005
Posts: 19728

You need to use the importNode() method of Document to "import" a node from one document to another.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1114
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

Joined: Nov 26, 2004
Posts: 183
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


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

Joined: Jul 12, 2002
Posts: 1114
Yup, that worked!!!


-- Mike
Have you checked out Aspose?
subject: Strange error from xerces 2_7_1
jQuery in Action, 3rd edition