wood burning stoves 2.0*
The moose likes Web Services and the fly likes Example 2, page 47 from Beginning Web Services doesn't work Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "Example 2, page 47 from Beginning Web Services doesn Watch "Example 2, page 47 from Beginning Web Services doesn New topic
Author

Example 2, page 47 from Beginning Web Services doesn't work

Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I'm having a lot of trouble with the examples in this book. For me, they're not really working. I'm copying and pasting the code, but while the example 1 creates an xml file (after I passed null to fix another problem), example 2 can't read it.

Here's the detail:

Example 1 that creates the xml still does not work. Well, it says it created the xml successfully, but you can't view it. Opening the file with a text editor shows some problems (see below).

Here's the code that "runs" to create the xml file below....

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=user&password=password";
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));
// passing null will serialize the whole document.
// book passed "doc", but that caused errors.

writer.println(((DocumentImpl)doc).saveXML(null));
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");

}
}

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

The xml file created from example 1 is:

<?xml version="1.0" encoding="UTF-16"?>
<stock_quotes><stock_quote><symbol>EDS</symbol><when><date>2006-0-9</date><time>10:59</time></when><price type="ask" value="32.32"/><price type="open" value="32.8"/><price type="dayhigh" value="33.1"/><price type="daylow" value="32.16"/><change>+0.239</change><volume>67552600</volume></stock_quote><stock_quote><symbol>SUNW</symbol><when><date>2006-0-9</date><time>10:59</time></when><price type="ask" value="5.93"/><price type="open" value="5.67"/><price type="dayhigh" value="6.01"/><price type="daylow" value="5.56"/><change>+0.239</change><volume>67552600</volume></stock_quote><stock_quote><symbol>IBM</symbol><when><date>2006-0-9</date><time>10:59</time></when><price type="ask" value="69.01"/><price type="open" value="69.51"/><price type="dayhigh" value="71.39"/><price type="daylow" value="71.39"/><change>+0.239</change><volume>67552600</volume></stock_quote><stock_quote><symbol>MSFT</symbol><when><date>2006-0-9</date><time>10:59</time></when><price type="ask" value="51.25"/><price type="open" value="51.31"/><price type="dayhigh" value="52.79"/><price type="daylow" value="50.64"/><change>+0.239</change><volume>67552600</volume></stock_quote><stock_quote><symbol>^DJI</symbol><when><date>2006-0-9</date><time>10:59</time></when><price type="ask" value="8448.19"/><price type="open" value="8540.47"/><price type="dayhigh" value="8621.95"/><price type="daylow" value="8448.19"/><change>+0.239</change><volume>67552600</volume></stock_quote><stock_quote><symbol>^IXIC</symbol><when><date>2006-0-9</date><time>10:59</time></when><price type="ask" value="1360.62"/><price type="open" value="1390.41"/><price type="dayhigh" value="1395.29"/><price type="daylow" value="1360.22"/><change>+0.239</change><volume>67552600</volume></stock_quote><stock_quote><symbol>^GSPC</symbol><when><date>2006-0-9</date><time>10:59</time></when><price type="ask" value="884.58"/><price type="open" value="905.36"/><price type="dayhigh" value="907.84"/><price type="daylow" value="884.46"/><change>+0.239</change><volume>67552600</volume></stock_quote></stock_quotes>

-------------------

I checked the code using a comparison tool and the only difference was that I passed a "null" instead of "doc" when creating the xml.

Does anyone see what's wrong here?

Thanks!

- Mike
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
On the succeeding exercises, I just used the xml file supplied, not the xml generated from the previous exercise.

There is a stock_quote.xml file in \Beginning_JWS_Examples\Chp02\

I used it instead of the one generated in the first exercise.
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
Yup, that's why it worked I guess.

Did you switch to JDOM? I was "hoping" to have code that worked to create the XML file itself. I'm now thinking about switching to JDOM since the author's code doesn't produce real XML (readable by anything I've tried).

There may be a simple explanation why their code doesn't work, but I don't see it and I don't want to spend another 2 days trying to debug somebody else's code.

Is your goal to work though the book? If so, perhaps we can compare notes here?

Thanks again for your reply!!

-- Mike
Mike London
Ranch Hand

Joined: Jul 12, 2002
Posts: 1075
I re-wrote the example 1 in BWS using JDOM 1.0.

Now, it's (much) simpler to understand (I added some comments and code formatting where the orignial code from the book wasn't clear on first read).

Also, this code actually creates an xml file that a browser will display!

Hurray!!!

Hope this is useful.

-- Mike

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

package com.wrox.jws.stockcore;

import org.jdom.Attribute;
import org.jdom.Element;
import org.jdom.Document;
import org.jdom.output.XMLOutputter;

import java.sql.*;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.io.FileWriter;

public class StockCoreDOMGenerator
{
private static Element stockQuoteElement = new Element (Markup.STOCK_QUOTES);
private static Document myDocument = new Document(stockQuoteElement);
private static String TYPES[] = {"ask", "open", "dayhigh", "daylow"};

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=user&password=password";
public static final String SQL = "SELECT * FROM Quotes";
public static final String DRIVER = "com.mysql.jdbc.Driver";
}

public StockCoreDOMGenerator()
{
}

private void addStock(String symbol, String quote[], String change, String volume)
{
GregorianCalendar cal = new GregorianCalendar();

// get date and time values to add
String date = cal.get(Calendar.YEAR) + "-" + cal.get(Calendar.MONTH) + "-" + cal.get(Calendar.DATE) ;
String time = cal.get(Calendar.HOUR_OF_DAY) + ":" + cal.get(Calendar.MINUTE);

// every time this method is called, create a new element to add to the
// stockQuoteElement we created at the top of the program.

Element stockQuote = new Element(Markup.STOCK_QUOTE);

// in this element, put all the child elements.

// symbol
Element symbol1 = new Element("symbol");
symbol1.addContent(symbol);
stockQuote.addContent(symbol1);

// add when...date...time elements

Element when = new Element (Markup.WHEN);

Element eltDate = new Element (Markup.DATE);
eltDate.addContent(date);
when.addContent(eltDate);

// get time from internal variables above
Element eltTime = new Element (Markup.TIME);
eltTime.addContent(time);
when.addContent(eltTime);

// then add these to the stockquote
stockQuote.addContent(when);

// now process the String array wiht 5 elements passed in
for (int x = 0; x < 4; x++)
{
Element price = new Element(Markup.PRICE);
price.setAttribute(new Attribute(Markup.TYPE ,TYPES[x] ) );
price.setAttribute(new Attribute(Markup.VALUE, quote[x]) ) ;

stockQuote.addContent(price);
}
// finally, add the change and volumne elements

Element chg = new Element(Markup.CHANGE);
chg.addContent(change);

Element vol = new Element(Markup.VOLUME);
vol.addContent(volume);

// now add to the parent stockQuoteElement
stockQuoteElement.addContent(stockQuote);
}

public static void main(String args[]) throws Exception
{
StockCoreDOMGenerator generator = new StockCoreDOMGenerator();
Class.forName(Database.DRIVER);

// connect to database...
Connection con = DriverManager.getConnection(Database.URL);
Statement stmt = con.createStatement();
ResultSet res = stmt.executeQuery(Database.SQL);

try
{
res.beforeFirst();
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));

} // end loop

XMLOutputter outputter = new XMLOutputter();
FileWriter writer = new FileWriter("stock_quotes.xml");
outputter.output(myDocument, writer);
writer.close();

}
catch (Exception e)
{
e.printStackTrace();
}
finally
{
if(res != null) res.close();
if(stmt != null) stmt.close();
if(con != null) con.close();
}
System.out.println("Stock quotes saved successfully");

} // end main

} // cnd class
Jesus Angeles
Ranch Hand

Joined: Feb 26, 2005
Posts: 2057
Currently, I am preparing for scdjws exam and I am focusing on items required for it. I will tackle jdom maybe after the exam.

I believe I ran all codes in that book. I just did this chapter 2 last.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Example 2, page 47 from Beginning Web Services doesn't work