aspose file tools*
The moose likes Web Services and the fly likes Beginning Java Web Services Code Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Beginning Java Web Services Code Problem" Watch "Beginning Java Web Services Code Problem" New topic
Author

Beginning Java Web Services Code Problem

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
The very first example in the "Beginning Java Web Services" book (XML) has a problem.

Perhaps it's due to the current version of xerces and that the book is a couple years old, not sure.

When I cut and pasted the code into Eclipse after importing the xerces libraries and tried to run it, I got the error below (no visible errors in Eclipse)...

Can anyone help me figure out what might be going on?

Thanks much in advance!!!

-- Mike

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

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

The code from their download site (freely available to all) that created this error is:

package com.wrox.jws.stockstore;

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; // 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=sa12";
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));
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);
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");

}
}
Evgeniy Bulanov
Greenhorn

Joined: Dec 19, 2005
Posts: 23
I suppose you have got a problem with xml. Try to create it with another parser. I prefer jdom. And one note also, use any logger for find out what's wrong with xml file.


Computers are like air conditioners - They stop working properly when you open Windows
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Yeah, I really like JDOM too.

But, wouldn't that mean I need to re-write the XML from the book to use JDOM?

-- Mike
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I commented out most of the code in the first example and *still* got the DOM error below.

This is strange.

I also tried to only use a couple of the xerces 2_7_1 libraries to see if I had a conflict.

Any additional ideas would be welcome...

-- Mike

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:147)


/*
* Created on Dec 19, 2005, 8:07:09 AM
*
* JWS_XML_Example
*
* Jim
*
*/
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=sa123";
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));
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);
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");

}
}
JavaMocha Latte
Greenhorn

Joined: Dec 30, 2005
Posts: 1
I think I have found a solution...

Modify the saveDocument() function as follows...

public void saveDocument() throws Exception
{
PrintWriter writer = new PrintWriter(new FileWriter(STOCK_FILE));
// line in book below (commented & replaced by following line
// writer.println(((DocumentImpl)doc).saveXML(doc));
writer.println(((DocumentImpl)doc).saveXML(doc.getDocumentElement()));
writer.close();
}

Using this code I WAS able to save an XML file...
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42612
    
  65
Hello "JavaMocha Latte"-

Welcome to JavaRanch.

On your way in you may have missed that there is a policy on screen names here, and your does not conform to it. You can adjust it right here. Thanks for your prompt attention to this matter.

Enjoy your time here.


Ping & DNS - my free Android networking tools app
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Beginning Java Web Services Code Problem