Class XMLExamples

java.lang.Object
  extended by java.awt.Component
      extended by java.awt.Container
          extended by java.awt.Window
              extended by java.awt.Frame
                  extended by javax.swing.JFrame
                      extended by XMLExamples
All Implemented Interfaces:
java.awt.event.ActionListener, java.awt.image.ImageObserver, java.awt.MenuContainer, java.io.Serializable, java.util.EventListener, javax.accessibility.Accessible, javax.swing.RootPaneContainer, javax.swing.WindowConstants

public class XMLExamples
extends javax.swing.JFrame
implements java.awt.event.ActionListener

Example of StAX and JaXB use. Uses the following schema:

<xsi:schema xmlns:xsi="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.geog.leeds.ac.uk" xmlns="http://www.geog.leeds.ac.uk" elementFormDefault="qualified">
<xsi:element name="map">
<xsi:complexType>
<xsi:sequence>
<xsi:element name="polygon" minOccurs="0" maxOccurs="unbounded">
<xsi:complexType>
<xsi:sequence>
<xsi:element name="points" type="xsi:string"/>
</xsi:sequence>
<xsi:attribute name="id" type="xsi:ID"/>
</xsi:complexType>
</xsi:element>
</xsi:sequence>
</xsi:complexType>
</xsi:element>
</xsi:schema>

This should be saved in an xsd file in the same directory as this file and the following command run against it at the command line:

xjc -p xmlclasses filename.xsd

This should generate the following classes:

Map.java - The class representing the root element of the XML
ObjectFactory.java - Class for generating new copies of the XML root and sub-elements
package-info.java

Compile these (note they are in the xmlclasses package). This should also generate classes representing the sub-elements that are classes within Map.java. Provided you have the XSD file in the same directory as this XMLExamples.java file, the new files should be in a xmlclasses directory within the directory this java file and the XSD file is in. (NB: this can be any package name - we've just pick this here and used it throughout).

You should then be able to compile and run this XMLExamples.java file, and use it to read in XML files like this:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="map2.xsl" ?>
<map xmlns="http://www.geog.leeds.ac.uk" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="www.geog.leeds.ac.uk map2.xsd">
<polygon id="p1">
<points>100,100 200,100 200,200 100,0 100,100 </points>
</polygon>
</map>

See Also:
Serialized Form

Nested Class Summary
 
Nested classes/interfaces inherited from class javax.swing.JFrame
javax.swing.JFrame.AccessibleJFrame
 
Nested classes/interfaces inherited from class java.awt.Frame
java.awt.Frame.AccessibleAWTFrame
 
Nested classes/interfaces inherited from class java.awt.Window
java.awt.Window.AccessibleAWTWindow
 
Nested classes/interfaces inherited from class java.awt.Container
java.awt.Container.AccessibleAWTContainer
 
Nested classes/interfaces inherited from class java.awt.Component
java.awt.Component.AccessibleAWTComponent, java.awt.Component.BaselineResizeBehavior, java.awt.Component.BltBufferStrategy, java.awt.Component.FlipBufferStrategy
 
Field Summary
private  java.lang.String id
           
private  java.lang.String jaxbMenuText
           
private  javax.swing.JLabel jlabel
           
private  javax.swing.JPanel jpanel
           
private  boolean loaded
           
private  java.lang.String loadJaxbMenuItemText
           
private  java.lang.String loadStaxMenuItemText
           
private  java.lang.String newJaxbMenuItemText
           
private  java.awt.Polygon polygon
           
private  java.lang.String saveJaxbMenuItemText
           
private  java.lang.String saveStaxMenuItemText
           
private  java.lang.String staxMenuText
           
 
Fields inherited from class javax.swing.JFrame
accessibleContext, EXIT_ON_CLOSE, rootPane, rootPaneCheckingEnabled
 
Fields inherited from class java.awt.Frame
CROSSHAIR_CURSOR, DEFAULT_CURSOR, E_RESIZE_CURSOR, HAND_CURSOR, ICONIFIED, MAXIMIZED_BOTH, MAXIMIZED_HORIZ, MAXIMIZED_VERT, MOVE_CURSOR, N_RESIZE_CURSOR, NE_RESIZE_CURSOR, NORMAL, NW_RESIZE_CURSOR, S_RESIZE_CURSOR, SE_RESIZE_CURSOR, SW_RESIZE_CURSOR, TEXT_CURSOR, W_RESIZE_CURSOR, WAIT_CURSOR
 
Fields inherited from class java.awt.Component
BOTTOM_ALIGNMENT, CENTER_ALIGNMENT, LEFT_ALIGNMENT, RIGHT_ALIGNMENT, TOP_ALIGNMENT
 
Fields inherited from interface javax.swing.WindowConstants
DISPOSE_ON_CLOSE, DO_NOTHING_ON_CLOSE, HIDE_ON_CLOSE
 
Fields inherited from interface java.awt.image.ImageObserver
ABORT, ALLBITS, ERROR, FRAMEBITS, HEIGHT, PROPERTIES, SOMEBITS, WIDTH
 
Constructor Summary
XMLExamples()
          Sets up the GUI.
 
Method Summary
 void actionPerformed(java.awt.event.ActionEvent e)
          Allows XMLExamples to be used as an ActionListener.
 java.awt.Polygon buildPolygon(java.lang.String points)
          Convenience method to take in XML points and convert them to a java.awt.Polygon.
private  java.io.File getFileFromUser(java.lang.String title)
          Convenience method to get a file from a user.
private  java.lang.String getPoints(java.awt.Polygon polygon)
          Convenience method to return a String of points from a java.awt.Polygon.
static void main(java.lang.String[] args)
          Just starts everything going.
private  void newJaxbXML()
          Generates a completely new object from the XML Schema, rather than reading one from XML file elements.
 void paint(java.awt.Graphics g)
          If a polygon has been loaded into the system, paint it.
private  void readJaxbXML()
          Reads XML using JaxB.
private  void readStaxXML()
          Reads XML using Stax.
private  void writeJaxbXML()
          Writes XML using JaxB.
private  void writeStaxXML()
          Write XML files using Stax.
 
Methods inherited from class javax.swing.JFrame
addImpl, createRootPane, frameInit, getAccessibleContext, getContentPane, getDefaultCloseOperation, getGlassPane, getGraphics, getJMenuBar, getLayeredPane, getRootPane, getTransferHandler, isDefaultLookAndFeelDecorated, isRootPaneCheckingEnabled, paramString, processWindowEvent, remove, repaint, setContentPane, setDefaultCloseOperation, setDefaultLookAndFeelDecorated, setGlassPane, setIconImage, setJMenuBar, setLayeredPane, setLayout, setRootPane, setRootPaneCheckingEnabled, setTransferHandler, update
 
Methods inherited from class java.awt.Frame
addNotify, getCursorType, getExtendedState, getFrames, getIconImage, getMaximizedBounds, getMenuBar, getState, getTitle, isResizable, isUndecorated, remove, removeNotify, setCursor, setExtendedState, setMaximizedBounds, setMenuBar, setResizable, setState, setTitle, setUndecorated
 
Methods inherited from class java.awt.Window
addPropertyChangeListener, addPropertyChangeListener, addWindowFocusListener, addWindowListener, addWindowStateListener, applyResourceBundle, applyResourceBundle, createBufferStrategy, createBufferStrategy, dispose, getBufferStrategy, getFocusableWindowState, getFocusCycleRootAncestor, getFocusOwner, getFocusTraversalKeys, getGraphicsConfiguration, getIconImages, getInputContext, getListeners, getLocale, getModalExclusionType, getMostRecentFocusOwner, getOwnedWindows, getOwner, getOwnerlessWindows, getToolkit, getWarningString, getWindowFocusListeners, getWindowListeners, getWindows, getWindowStateListeners, hide, isActive, isAlwaysOnTop, isAlwaysOnTopSupported, isFocusableWindow, isFocusCycleRoot, isFocused, isLocationByPlatform, isShowing, pack, postEvent, processEvent, processWindowFocusEvent, processWindowStateEvent, removeWindowFocusListener, removeWindowListener, removeWindowStateListener, reshape, setAlwaysOnTop, setBounds, setBounds, setCursor, setFocusableWindowState, setFocusCycleRoot, setIconImages, setLocationByPlatform, setLocationRelativeTo, setMinimumSize, setModalExclusionType, setSize, setSize, setVisible, show, toBack, toFront
 
Methods inherited from class java.awt.Container
add, add, add, add, add, addContainerListener, applyComponentOrientation, areFocusTraversalKeysSet, countComponents, deliverEvent, doLayout, findComponentAt, findComponentAt, getAlignmentX, getAlignmentY, getComponent, getComponentAt, getComponentAt, getComponentCount, getComponents, getComponentZOrder, getContainerListeners, getFocusTraversalPolicy, getInsets, getLayout, getMaximumSize, getMinimumSize, getMousePosition, getPreferredSize, insets, invalidate, isAncestorOf, isFocusCycleRoot, isFocusTraversalPolicyProvider, isFocusTraversalPolicySet, layout, list, list, locate, minimumSize, paintComponents, preferredSize, print, printComponents, processContainerEvent, remove, removeAll, removeContainerListener, setComponentZOrder, setFocusTraversalKeys, setFocusTraversalPolicy, setFocusTraversalPolicyProvider, setFont, transferFocusBackward, transferFocusDownCycle, validate, validateTree
 
Methods inherited from class java.awt.Component
action, add, addComponentListener, addFocusListener, addHierarchyBoundsListener, addHierarchyListener, addInputMethodListener, addKeyListener, addMouseListener, addMouseMotionListener, addMouseWheelListener, bounds, checkImage, checkImage, coalesceEvents, contains, contains, createImage, createImage, createVolatileImage, createVolatileImage, disable, disableEvents, dispatchEvent, enable, enable, enableEvents, enableInputMethods, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getBackground, getBaseline, getBaselineResizeBehavior, getBounds, getBounds, getColorModel, getComponentListeners, getComponentOrientation, getCursor, getDropTarget, getFocusListeners, getFocusTraversalKeysEnabled, getFont, getFontMetrics, getForeground, getHeight, getHierarchyBoundsListeners, getHierarchyListeners, getIgnoreRepaint, getInputMethodListeners, getInputMethodRequests, getKeyListeners, getLocation, getLocation, getLocationOnScreen, getMouseListeners, getMouseMotionListeners, getMousePosition, getMouseWheelListeners, getName, getParent, getPeer, getPropertyChangeListeners, getPropertyChangeListeners, getSize, getSize, getTreeLock, getWidth, getX, getY, gotFocus, handleEvent, hasFocus, imageUpdate, inside, isBackgroundSet, isCursorSet, isDisplayable, isDoubleBuffered, isEnabled, isFocusable, isFocusOwner, isFocusTraversable, isFontSet, isForegroundSet, isLightweight, isMaximumSizeSet, isMinimumSizeSet, isOpaque, isPreferredSizeSet, isValid, isVisible, keyDown, keyUp, list, list, list, location, lostFocus, mouseDown, mouseDrag, mouseEnter, mouseExit, mouseMove, mouseUp, move, nextFocus, paintAll, prepareImage, prepareImage, printAll, processComponentEvent, processFocusEvent, processHierarchyBoundsEvent, processHierarchyEvent, processInputMethodEvent, processKeyEvent, processMouseEvent, processMouseMotionEvent, processMouseWheelEvent, removeComponentListener, removeFocusListener, removeHierarchyBoundsListener, removeHierarchyListener, removeInputMethodListener, removeKeyListener, removeMouseListener, removeMouseMotionListener, removeMouseWheelListener, removePropertyChangeListener, removePropertyChangeListener, repaint, repaint, repaint, requestFocus, requestFocus, requestFocusInWindow, requestFocusInWindow, resize, resize, setBackground, setComponentOrientation, setDropTarget, setEnabled, setFocusable, setFocusTraversalKeysEnabled, setForeground, setIgnoreRepaint, setLocale, setLocation, setLocation, setMaximumSize, setName, setPreferredSize, show, size, toString, transferFocus, transferFocusUpCycle
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface java.awt.MenuContainer
getFont, postEvent
 

Field Detail

polygon

private java.awt.Polygon polygon

id

private java.lang.String id

saveStaxMenuItemText

private java.lang.String saveStaxMenuItemText

loadStaxMenuItemText

private java.lang.String loadStaxMenuItemText

saveJaxbMenuItemText

private java.lang.String saveJaxbMenuItemText

loadJaxbMenuItemText

private java.lang.String loadJaxbMenuItemText

newJaxbMenuItemText

private java.lang.String newJaxbMenuItemText

staxMenuText

private java.lang.String staxMenuText

jaxbMenuText

private java.lang.String jaxbMenuText

loaded

private boolean loaded

jpanel

private javax.swing.JPanel jpanel

jlabel

private javax.swing.JLabel jlabel
Constructor Detail

XMLExamples

public XMLExamples()
Sets up the GUI.

Method Detail

actionPerformed

public void actionPerformed(java.awt.event.ActionEvent e)
Allows XMLExamples to be used as an ActionListener. Rather poorly, uses text items passed into constructor to determine method to call. Would be better using Actions.

Specified by:
actionPerformed in interface java.awt.event.ActionListener
Parameters:
e -

readStaxXML

private void readStaxXML()
Reads XML using Stax.


writeStaxXML

private void writeStaxXML()
Write XML files using Stax.


readJaxbXML

private void readJaxbXML()
Reads XML using JaxB.


writeJaxbXML

private void writeJaxbXML()
Writes XML using JaxB.


newJaxbXML

private void newJaxbXML()
Generates a completely new object from the XML Schema, rather than reading one from XML file elements.


getFileFromUser

private java.io.File getFileFromUser(java.lang.String title)
Convenience method to get a file from a user.

Parameters:
title - - for dialog.
Returns:
File picked or null if cancelled.

buildPolygon

public java.awt.Polygon buildPolygon(java.lang.String points)
Convenience method to take in XML points and convert them to a java.awt.Polygon. Will parse points with space or comma delimiters.

Parameters:
points - should be in the form "x,y x,y x,y x,y".

getPoints

private java.lang.String getPoints(java.awt.Polygon polygon)
Convenience method to return a String of points from a java.awt.Polygon.

Returns:
points will be in the form "x,y x,y x,y x,y".

paint

public void paint(java.awt.Graphics g)
If a polygon has been loaded into the system, paint it.

Overrides:
paint in class java.awt.Container
Parameters:
g -

main

public static void main(java.lang.String[] args)
Just starts everything going.

Parameters:
args -