www.webdeveloper.com
Results 1 to 6 of 6

Thread: Parsing dates in XML

  1. #1
    Join Date
    Jun 2006
    Location
    Cracow, Poland
    Posts
    28

    Question Parsing dates in XML

    Hi. Can anyone please help me with this issue I have? I'm trying to parse pieces of text stored in xml documents, that are supposed to represent dates. The trouble is, I tried the DateFormat.parse(...) method, but it only throws exceptions, no matter what I pass it as arugment. I even tried the examples taken from java api docs, and it would still throw exceptions (like ParseException or something). So the question is: does anybody know a library, or a single class even, that has the ability to parse a String into a Date object? I'd be more than happy to have it parse date in the following formats:

    • ISO8601: 1994-11-05T08:15:30-05:00 or 1994-11-05T13:15:30Z
    • RFC1123 (RFC822): Sun, 06 Nov 1994 08:49:37 GMT
    • RFC1036 (RFC850): Sunday, 06-Nov-94 08:49:37 GMT
    • ANSI C asctime(): Sun Nov 6 08:49:37 1994


    Please, help.
    IDIOT, n. A member of a large and powerful tribe whose influence in human affairs has always been dominant and controlling.

    Ambrose Bierce Devil's Dictionary

  2. #2
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    use SimpleDateFormat...I have always used it and I Know it workes(its a child class of dateformat class).
    My guess is your problem is in the text you are passing.
    Post the actual code u are using and the string that you are trying to parse
    Last edited by Khalid Ali; 06-21-2006 at 12:21 PM.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  3. #3
    Join Date
    Jun 2006
    Location
    Cracow, Poland
    Posts
    28
    Ok, here's the complete method code:
    Code:
    public static DAVProperty parse(Node node) {    
        DAVProperty ret = null;
        if (node.getNodeType() == Node.ELEMENT_NODE) { 
          String nodeName = node.getNodeName();
          String nodeValue = node.getTextContent();      
          DAVNamedProperty propName = DAVNamedProperty.UNKNOWN_PROPERTY; 
          for (DAVNamedProperty d : DAVNamedProperty.values()) {        
            if (nodeName.equals(d.xmlName())) {
              propName = d;
              break;
            }
          }
          if (propName == DAVNamedProperty.CREATION_DATE || 
              propName == DAVNamedProperty.GET_LAST_MODIFIED) {
            ret = new DAVDate(propName);
            //here should also be parsing the nodeValue for date
            //and setting it as the value of the property, but not now.
          } else {
            ret = new DAVStringProperty(propName,nodeName,nodeValue);        
          } 
        }
        l.debug("parse(): returning: " + ret);
        return ret;
      }
    There are comments in the place where I want the parsing of the date to take place. I don't have the code I used earlier any more, cause it didn't work, so I simply deleted it. The thing is, the method enters the dates region correctly. When it does so, the nodeValue contains the date string, and it is written in one of the formats I mentioned in the previous post. There is a method
    Code:
    public void DAVProperty.setValue(Object o)
    that could be used there to set the value of the property to either a Date or a Calendar object (makes no difference to me, could be either one), but I'd first like it to be read and parsed from the xml Node. Any ideas? Many thanks.
    IDIOT, n. A member of a large and powerful tribe whose influence in human affairs has always been dominant and controlling.

    Ambrose Bierce Devil's Dictionary

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    you need to set the format correctly before you can do what u are trying...I took one format from the examples above and wrote the following code
    PHP Code:
    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z");
            
    String strDate "Sun, 06 Nov 1994 08:49:37 GMT";
            try {
                
    Date parsedDate format.parse(strDate);
                
    System.out.println(format.format(new Date()));
                
    System.out.println("-- "+format.parse(strDate));
            } catch (
    ParseException e) {
                
    e.printStackTrace();
            } 
    It does what u are trying to do....
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Jun 2006
    Location
    Cracow, Poland
    Posts
    28
    Great, many many thanks One more thing though - should anyone ever find this thread useful, remember about your locale. The SimpleDateFormat class provides a constructor that takes two arguments - one is the format string, the other is the locale in which the strings parsed are supposed to be. My example shows it's a matter of vital importance. I'm trying to parse dates that are written in English, but I live in Poland, so my computer has Polish localization. Thus, trying out the code Khalid posted yielded an exception. The answer to that problem is to construct the format with the following code:
    Code:
    SimpleDateFormat format = new SimpleDateFormat("EEE, dd MMM yyyy hh:mm:ss z", Locale.UK);
    Should be obvious, but it wasn't for me, so I figure there might be others, for whom it won't be so obvious as well. Thanks again, Khalid
    IDIOT, n. A member of a large and powerful tribe whose influence in human affairs has always been dominant and controlling.

    Ambrose Bierce Devil's Dictionary

  6. #6
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,123
    glad to be of any help...:-)
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles