JAXP 1.3 Datatype API : XML Data « XML « Java

Home
Java
1.2D Graphics GUI
2.3D
3.Advanced Graphics
4.Ant
5.Apache Common
6.Chart
7.Class
8.Collections Data Structure
9.Data Type
10.Database SQL JDBC
11.Design Pattern
12.Development Class
13.EJB3
14.Email
15.Event
16.File Input Output
17.Game
18.Generics
19.GWT
20.Hibernate
21.I18N
22.J2EE
23.J2ME
24.JavaFX
25.JDK 6
26.JDK 7
27.JNDI LDAP
28.JPA
29.JSP
30.JSTL
31.Language Basics
32.Network Protocol
33.PDF RTF
34.Reflection
35.Regular Expressions
36.Scripting
37.Security
38.Servlets
39.Spring
40.Swing Components
41.Swing JFC
42.SWT JFace Eclipse
43.Threads
44.Tiny Application
45.Velocity
46.Web Services SOA
47.XML
Java » XML » XML Data 




JAXP 1.3 Datatype API
 
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 
 *      http://www.apache.org/licenses/LICENSE-2.0
 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeConstants;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * <p>
 * A sample which demonstrates usage of the JAXP 1.3 Datatype API.
 * </p>
 
 @version $Id: DatatypeAPIUsage.java 447685 2006-09-19 02:37:49Z mrglavas $
 */
public class DatatypeAPIUsage {

  public static void main(String[] args) {
    try {
      DatatypeFactory df = DatatypeFactory.newInstance();
      // my work number in milliseconds:
      Duration myPhone = df.newDuration(9054133519l);
      Duration myLife = df.newDuration(true, 2921513450);
      int compareVal = myPhone.compare(myLife);
      switch (compareVal) {
      case DatatypeConstants.LESSER:
        System.out.println("There are fewer milliseconds in my phone number than my lifespan.");
        break;
      case DatatypeConstants.EQUAL:
        System.out
            .println("The same number of milliseconds are in my phone number and my lifespan.");
        break;
      case DatatypeConstants.GREATER:
        System.out.println("There are more milliseconds in my phone number than my lifespan.");
        break;
      case DatatypeConstants.INDETERMINATE:
        System.out.println("The comparison could not be carried out.");
      }

      // create a yearMonthDuration
      Duration ymDuration = df.newDurationYearMonth("P12Y10M");
      System.out.println("P12Y10M is of type: " + ymDuration.getXMLSchemaType());

      // create a dayTimeDuration (really this time)
      Duration dtDuration = df.newDurationDayTime("P10DT10H12M0S");
      System.out.println("P10DT10H12M0S is of type: " + dtDuration.getXMLSchemaType());

      // try to fool the factory!
      try {
        ymDuration = df.newDurationYearMonth("P12Y10M1D");
      catch (IllegalArgumentException e) {
        System.out.println("'duration': P12Y10M1D is not 'yearMonthDuration'!!!");
      }

      XMLGregorianCalendar xgc = df.newXMLGregorianCalendar();
      xgc.setYear(1975);
      xgc.setMonth(DatatypeConstants.AUGUST);
      xgc.setDay(11);
      xgc.setHour(6);
      xgc.setMinute(44);
      xgc.setSecond(0);
      xgc.setMillisecond(0);
      xgc.setTimezone(5);
      xgc.add(myPhone);
      System.out
          .println("The approximate end of the number of milliseconds in my phone number was "
              + xgc);

      // adding a duration to XMLGregorianCalendar
      xgc.add(myLife);
      System.out.println("Adding the duration myLife to the above calendar:" + xgc);

      // create a new XMLGregorianCalendar using the string format of xgc.
      XMLGregorianCalendar xgcCopy = df.newXMLGregorianCalendar(xgc.toXMLFormat());

      // should be equal-if not what happened!!
      if (xgcCopy.compare(xgc!= DatatypeConstants.EQUAL) {
        System.out.println("oooops!");
      else {
        System.out.println("Very good: " + xgc + " is equal to " + xgcCopy);
      }
    catch (DatatypeConfigurationException dce) {
      System.err.println("error: Datatype error occurred - " + dce.getMessage());
      dce.printStackTrace(System.err);
    }
  // main(String[])

// DatatypeAPIUsage

   
  














Related examples in the same category
1.Streaming XML
2.extends DefaultHandler to create a XML binding
3.Your won XML binding
4.Parse and format xs:dateTime values
5.Replaces all XML character entities with the character they represent.
6.Sniffed Xml InputStream to find out the declaration and file encoding
7.Whether the given character is a valid XML space.
8.Common XML constants.
9.Sniffed Xml Reader
10.Source To InputSource
11.Utility class for working with xml data
12.whether the given 32 bit character is a valid XML 1.1 character.
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.