Panagiotis Plevrakis wrote:
> In the following code I'm parsing an xml file that contains
> the elements <date>,<value>,<change> more than once. The parent
> element of these elements is <data> . I load their values in a Vector
> wrapped in a Data object. While I expect more than one set of data to
> be displayed, I get only the last one. What am I doing wrong?
I don't know. I loaded the source you included and ran it against the
following database.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<database>
<notdata>
<date>01/01/2002</date>
<change>There was no change on this date</change>
<value>Nor was there any value</value>
</notdata>
<data>
<date>01/02/2002</date>
<change>There was minor change</change>
<value>There was some value</value>
</data>
<Data>
<date>01/03/2002</date>
<change>There was a chill wind blowing</change>
<value>A light jacket would have been of some value</value>
</Data>
<data>
<date>01/04/2002</date>
<change>The wind has died, but it is raining</change>
<value>An umbrella</value>
</data>
<data>
<date>01/05/2002</date>
<change>The sun is beating down</change>
<value>Fully charged A/C in the car</value>
</data>
</database>
It worked as it seemed like it should. Except that it printed a "4" at
the beginning of each display. That threw me until I noticed your debug
print statement. I would have done the operations a little differently
(aamof, I have attached my version, if you are interested). The main
critique I have is that you do case insensitive comparisons on the
elements and XML parsing should be case sensitive. Notice I threw in a
<Data></Data> element as a test case.
If the format of the xml file is such that your code doesn't work with
it but worked with my simplistic version, send me the xml file and I'll
look at it.
hth,
Tomm
/*
* ParseData.java
*
* Created on May 16, 2002, 4:20 PM
*/
/**
*
* @author Tomm Carr
*/
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import java.net.*;
public class ParseData extends DefaultHandler {
private Vector dataVector = new Vector();
private Data currentData = null;
private String change;
private String date;
private String tempstring;
private String value;
private boolean inDataElement = false;
public ParseData( String file ) {
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
spf.setValidating( true );
XMLReader xmlReader = spf.newSAXParser().getXMLReader();
xmlReader.setContentHandler( this );
xmlReader.setErrorHandler( this );
xmlReader.parse( new InputSource( new FileReader( file ) ) );
}
catch ( Exception e ) {
e.printStackTrace ();
}
}
public void characters( char[] ch,
int start,
int length )
throws SAXException {
if ( inDataElement )
tempstring = new String( ch, start, length ).trim();
}
public void startElement( String namespaceURI,
String localName,
String qName,
Attributes atts )
throws SAXException {
if ( !inDataElement && qName.equals( "data" ) ) {
// We have just entered the <data> element. Mark this event.
inDataElement = true;
}
}
public void endElement( String uri,
String localName,
String qName )
throws SAXException {
if ( inDataElement ) { // The only element of interest
if ( qName.equals( "date" ) ) {
date = tempstring;
}
else if ( qName.equals( "value" ) ) {
value = tempstring;
}
else if ( qName.equals( "change" ) ) {
change = tempstring;
}
else if ( qName.equals( "data" ) ) {
// Finishes parsing all subelements of <data>. Save
the values.
dataVector.add( new Data( date, value, change ) );
inDataElement = false;
}
}
}
public void endDocument()
throws org.xml.sax.SAXException {
for ( int i = 0; i < dataVector.size(); i++ ) {
currentData = ( Data ) dataVector.elementAt( i );
System.out.println( currentData.getDate() + "\n" +
currentData.getValue() + "\n"
+
currentData.getChange() + "\n" );
}
}
/**
* @param args the command line arguments
*/
public static void main( String[] args ) {
new ParseData( "database.xml" );
}
class Data {
private String change;
private String date;
private String value;
public Data( String date,
String value,
String change ) {
this.date = date;
this.value = value;
this.change = change;
}
public String getChange() {
return change;
}
public String getDate() {
return date;
}
public String getValue() {
return value;
}
} //end Data
} //end Parser
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm