Yeah, it works with date = tempstring and your last fix. Again, thanks for
your time and your help.
Panos
>From: Jianjun Zhang <[EMAIL PROTECTED]>
>Reply-To: "JDJList" <[EMAIL PROTECTED]>
>To: "JDJList" <[EMAIL PROTECTED]>
>Subject: [jdjlist] Re: For Charles or anyone that can help
>Date: Thu, 16 May 2002 17:10:00 -0700
>
>After reading all of your code, I don't think that my first suggestion was
>correct. It should still be
>working if you use date=tempstring,..... (witht he Vector add change) Sorry
>about the confusion.
>
>-JJ
>
>Panagiotis Plevrakis wrote:
>
> > This works fine and makes sense. Now I'm getting all the children
>elements
> > (which follow the same sequential order.)
> > Thanks a lot,
> > Panos
> >
> > >From: Jianjun Zhang <[EMAIL PROTECTED]>
> > >Reply-To: "JDJList" <[EMAIL PROTECTED]>
> > >To: "JDJList" <[EMAIL PROTECTED]>
> > >Subject: [jdjlist] Re: For Charles or anyone that can help
> > >Date: Thu, 16 May 2002 16:40:58 -0700
> > >
> > >Panos,
> > > Yeah, my change won't work if you have multi-sets of the children
> > >elements,
> > >because you will only add
> > > the last set to your vector. Does your children elements always
>follow
> > >the same
> > >sequential order such as
> > ><date/><value/><change/><date/><value/><change/>.... ? If this is the
>case,
> > >then
> > >use the following code in
> > >endElement():
> > > > if (qName.compareToIgnoreCase("date") == 0) {
> > > > date = new String(tempstring);
> > > > } else if (qName.compareToIgnoreCase("value") == 0) {
> > > > value = new String(tempstring);
> > > > } else if (qName.compareToIgnoreCase("change") == 0) {
> > > > change = new String(tempstring);
> > > vector.add(new Data(date, value, change));
> > > > } else if (qName.compareToIgnoreCase("data") == 0) {
> > > > // vector.add(new Data(date, value, change));
> > > //do nothing, since this is the end of the root.
> > > > }
> > > >
> > >Let me know if this works. If the assumption of sequential orders isn't
> > >right,
> > >let me know also, I will give you
> > >another example.
> > >
> > >-JJ
> > >
> > >Panagiotis Plevrakis wrote:
> > >
> > > > I am still getting only the last set of <date><value><change>. I
>forgot
> > >to
> > > > mention that <data> is the root element and within this element
>there is
> > >a
> > > > sequence of <date><value><change> elements.
> > > >
> > > > Panos
> > > >
> > > > >From: Jianjun Zhang <[EMAIL PROTECTED]>
> > > > >Reply-To: "JDJList" <[EMAIL PROTECTED]>
> > > > >To: "JDJList" <[EMAIL PROTECTED]>
> > > > >Subject: [jdjlist] Re: For Charles or anyone that can help
> > > > >Date: Thu, 16 May 2002 15:11:12 -0700
> > > > >
> > > > >You should reuse class variables (date, value, change, ...) in your
> > > > >endElement(...) callback routines. Everytime this is
> > > > >called, you are assigning new values to date, value and change
>(make
> > >them
> > > > >pointing to new values). Essentially, at the
> > > > >end, your vector contains a list of objects with the same internal
> > >variable
> > > > >values (the last one). Use the following:
> > > > >
> > > > > if (qName.compareToIgnoreCase("date") == 0) {
> > > > > date = new String(tempstring);
> > > > > } else if (qName.compareToIgnoreCase("value") == 0) {
> > > > > value = new String(tempstring);
> > > > > } else if (qName.compareToIgnoreCase("change") == 0) {
> > > > > change = new String(tempstring);
> > > > > } else if (qName.compareToIgnoreCase("data") == 0) {
> > > > > vector.add(new Data(date, value, change));
> > > > > }
> > > > >
> > > > >This may not be the most efficient way of doing this (a string copy
>is
> > > > >performed), but requires least change, a better probably
> > > > >is to keep a stack of the values so that no copies are needed.
> > > > >
> > > > >This is assuming that there is only one set of date, value, change
> > >elements
> > > > >per data element parent.
> > > > >I haven't tested this, but hope it works.
> > > > >
> > > > >-JJ
> > > > >
> > > > >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?
> > > > > > Thanks for your help.
> > > > > > ***********************************************************
> > > > > > import java.util.*;
> > > > > > import java.io.*;
> > > > > > import java.net.*;
> > > > > > import javax.xml.parsers.*;
> > > > > > import org.xml.sax.*;
> > > > > > import org.xml.sax.helpers.*;
> > > > > >
> > > > > > public class Parser extends DefaultHandler {
> > > > > > Vector vector = new Vector();
> > > > > > String date, value, change, tempstring;
> > > > > > Data data = null;
> > > > > >
> > > > > > public static void main(String[]args) {
> > > > > > Parser p = new Parser();
> > > > > > p.parse("database.xml");
> > > > > > p.display();
> > > > > > }
> > > > > >
> > > > > > private void display() {
> > > > > > for (int i = 0; i < vector.size(); i++) {
> > > > > > System.out.println(vector.size()); //debug
> > > > > > data = (Data) vector.elementAt(i);
> > > > > >
> > > > > > System.out.println(data.getDate() + "\n" +
> > > > > > data.getValue() + "\n" +
> > > > > > data.getChange() + "\n");
> > > > > > System.out.println();
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > private void parse(String file) {
> > > > > > try {
> > > > > > SAXParserFactory spf =
>SAXParserFactory.newInstance();
> > > > > >
> > > > > > spf.setValidating(true);
> > > > > > SAXParser saxParser = spf.newSAXParser();
> > > > > > XMLReader xmlReader = saxParser.getXMLReader();
> > > > > >
> > > > > > xmlReader.setContentHandler(this);
> > > > > > xmlReader.setErrorHandler(this);
> > > > > > String urlstring = "file:" +
> > > > > > new File
>(file).getAbsolutePath();
> > > > > > URL url = new URL(urlstring);
> > > > > > URLConnection connection = url.openConnection();
> > > > > > InputStream inputstream =
>connection.getInputStream();
> > > > > > InputSource inputsource = new
>InputSource(inputstream);
> > > > > >
> > > > > > xmlReader.parse(inputsource);
> > > > > > } catch (Throwable err) {
> > > > > > System.out.println(err.getMessage());
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > public void startElement(String namespaceURI, String
>localName,
> > > > > > String qName, Attributes atts) throws SAXException {
> > > > > > tempstring = "";
> > > > > > if (qName.compareToIgnoreCase("data") == 0) {
> > > > > > date = "";
> > > > > > value = "";
> > > > > > change = "";
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > public void endElement(String uri, String localName,
> > > > > > String qName) throws SAXException {
> > > > > > if (qName.compareToIgnoreCase("date") == 0) {
> > > > > > date = tempstring;
> > > > > > } else if (qName.compareToIgnoreCase("value") == 0) {
> > > > > > value = tempstring;
> > > > > > } else if (qName.compareToIgnoreCase("change") == 0) {
> > > > > > change = tempstring;
> > > > > > } else if (qName.compareToIgnoreCase("data") == 0) {
> > > > > > vector.add(new Data(date, value, change));
> > > > > > }
> > > > > > }
> > > > > >
> > > > > > public void characters(char[] ch, int start,
> > > > > > int length) throws SAXException {
> > > > > > String charactersfound = new String(ch, start, length);
> > > > > > tempstring = tempstring + charactersfound;
> > > > > > }
> > > > > >
> > > > > > class Data {
> > > > > >
> > > > > > private String date = "";
> > > > > > private String value = "";
> > > > > > private String change = "";
> > > > > >
> > > > > > public Data() {}
> > > > > >
> > > > > > public Data(String date, String value, String change) {
> > > > > > this.date = date;
> > > > > > this.value = value;
> > > > > > this.change = change;
> > > > > > }
> > > > > >
> > > > > > public String getDate() {
> > > > > > return date;
> > > > > > }
> > > > > >
> > > > > > public String getValue() {
> > > > > > return value;
> > > > > > }
> > > > > >
> > > > > > public String getChange() {
> > > > > > return change;
> > > > > > }
> > > > > >
> > > > > > public void setDate(String date) {
> > > > > > this.date = date;
> > > > > > }
> > > > > >
> > > > > > public void setValue(String value) {
> > > > > > this.value = value;
> > > > > > }
> > > > > >
> > > > > > public void setChange(String change) {
> > > > > > this.change = change;
> > > > > > }
> > > > > > } //end Data
> > > > > > } //end Parser
> > > > > > *********************************************************
> > > > > >
> > > > > >
>_________________________________________________________________
> > > > > > MSN Photos is the easiest way to share and print your photos:
> > > > > > http://photos.msn.com/support/worldwide.aspx
> > > > > >
> > > > > > To change your membership options, refer to:
> > > > > > http://www.sys-con.com/java/list.cfm
> > > > >
> > > > >
> > > > >To change your membership options, refer to:
> > > > >http://www.sys-con.com/java/list.cfm
> > > >
> > > > _________________________________________________________________
> > > > Chat with friends online, try MSN Messenger:
>http://messenger.msn.com
> > > >
> > > > To change your membership options, refer to:
> > > > http://www.sys-con.com/java/list.cfm
> > >
> > >
> > >To change your membership options, refer to:
> > >http://www.sys-con.com/java/list.cfm
> >
> > _________________________________________________________________
> > Join the world�s largest e-mail service with MSN Hotmail.
> > http://www.hotmail.com
> >
> > To change your membership options, refer to:
> > http://www.sys-con.com/java/list.cfm
>
>
>To change your membership options, refer to:
>http://www.sys-con.com/java/list.cfm
_________________________________________________________________
Send and receive Hotmail on your mobile device: http://mobile.msn.com
To change your membership options, refer to:
http://www.sys-con.com/java/list.cfm