com.sibvisions.util.xml
Class XmlWorker

java.lang.Object
  extended by org.xml.sax.helpers.DefaultHandler
      extended by com.sibvisions.util.xml.XmlWorker
All Implemented Interfaces:
ContentHandler, DTDHandler, EntityResolver, ErrorHandler, LexicalHandler

public class XmlWorker
extends DefaultHandler
implements LexicalHandler

Simple parser and writer for xml files. The parser extends the DefaultHandler to overwrite the necessary functions.
The newly created xml files will be encoded in UTF-8.

Example to work with a xml file:

 XmlWorker xmw = new XmlWorker();
 
 XmlNode xmnRead;
 XmlNode xmnChange;
 
 ArrayList<XmlNode> alElements;
 
 
 // Parse a xml file
 xmnRead = xmw.read(new FileInputStream("example.xml"));
     
 // Access a node list
 alElements = xmnRead.getNodes("/archive/element");
 
 // Count node list elements
 System.out.println(xmnRead.size("/archive/element"));
 
 // Access a node from a node list
 System.out.println(xmnRead.getNode("/archive/element(0)"));
 
 // Access a single node
 System.out.println(xmnRead.getNode("/archive/name"));
 
 // Change the value of a node
 xmnRead.setNode("/archive/element(1)/user", "xml");
 
 // Remove all sub elements and add one element (as attribute)
 xmnChange = new XmlNode(XmlNode.TYPE_ATTRIBUTE, "new");
 xmnChange.setValue("changed");
 
 xmnRead.setNode("/archive/element(1)/type", xmnChange);
 
 // Add a new node (the required elements (newelement, type) 
 // will be created automaticly)
 xmnRead.setNode("/archive/newelement/type", "new");
 
 // Remove a node
 xmnRead.removeNode("/archive/element(2)");
 
 // Insert a list-node with a value, between first and second element
 xmnRead.insertNode("/archive/element(1)", "between");
 
 // Insert a new-node without a value, between first and second element
 xmnRead.insertNode("/archive/element(1)", new XmlNode("break"));
 
 // Save the new xml structure
 xmw.write(new FileOutputStream("example_v2.xml"), 2);
 


Constructor Summary
XmlWorker()
           
 
Method Summary
 void characters(char[] pChar, int pStart, int pLength)
          
 void comment(char[] pChar, int pStart, int pLength)
          
 void endCDATA()
          
 void endDocument()
          
 void endDTD()
          
 void endElement(String pURI, String pLocalName, String pName)
          
 void endEntity(String name)
          
 void error(SAXParseException pError)
          Throws the parse exception if validation is enabled.
 void fatalError(SAXParseException pError)
          Throws the parse exception if validation is enabled.
 int getIndentation()
          Gets the xml output indentation.
 String getSchema()
          Gets the resource name of the schema file for validation of the xml.
 boolean isValidationEnabled()
          Gets the validation state.
 XmlNode read(InputStream pXmlSource)
          Parses a xml file and create a XmlNode structure.
 void setDocumentLocator(Locator pLocator)
          Caches the document locator for line numbering.
 void setIndentation(int pIndent)
          Sets the xml output indentation.
 void setSchema(String pSchema)
          Sets the resource name of the schema file for validation of the xml.
 void setValidationEnabled(boolean pValidation)
          Set the general validation of xml en- or disabled.
 void startCDATA()
          
 void startDocument()
          
 void startDTD(String name, String publicId, String systemId)
          
 void startElement(String pNameSpaceURI, String pLocalName, String pName, Attributes pAttr)
          
 void startEntity(String name)
          
 void warning(SAXParseException pError)
          Throws the parse exception if validation is enabled.
 void write(OutputStream pXmlTarget, XmlNode pNode)
          Writes the structure of a XmlNode as xml stream to the specified target stream.
 
Methods inherited from class org.xml.sax.helpers.DefaultHandler
endPrefixMapping, ignorableWhitespace, notationDecl, processingInstruction, resolveEntity, skippedEntity, startPrefixMapping, unparsedEntityDecl
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

XmlWorker

public XmlWorker()
Method Detail

comment

public void comment(char[] pChar,
                    int pStart,
                    int pLength)
             throws SAXException

Specified by:
comment in interface LexicalHandler
Throws:
SAXException

startCDATA

public void startCDATA()
                throws SAXException

Specified by:
startCDATA in interface LexicalHandler
Throws:
SAXException

endCDATA

public void endCDATA()
              throws SAXException

Specified by:
endCDATA in interface LexicalHandler
Throws:
SAXException

startDTD

public void startDTD(String name,
                     String publicId,
                     String systemId)
              throws SAXException

Specified by:
startDTD in interface LexicalHandler
Throws:
SAXException

endDTD

public void endDTD()
            throws SAXException

Specified by:
endDTD in interface LexicalHandler
Throws:
SAXException

startEntity

public void startEntity(String name)
                 throws SAXException

Specified by:
startEntity in interface LexicalHandler
Throws:
SAXException

endEntity

public void endEntity(String name)
               throws SAXException

Specified by:
endEntity in interface LexicalHandler
Throws:
SAXException

setDocumentLocator

public void setDocumentLocator(Locator pLocator)
Caches the document locator for line numbering.

Specified by:
setDocumentLocator in interface ContentHandler
Overrides:
setDocumentLocator in class DefaultHandler
Parameters:
pLocator - the locator instance

warning

public void warning(SAXParseException pError)
             throws SAXException
Throws the parse exception if validation is enabled.

Specified by:
warning in interface ErrorHandler
Overrides:
warning in class DefaultHandler
Parameters:
pError -
Throws:
SAXException
See Also:
setValidationEnabled(boolean)

error

public void error(SAXParseException pError)
           throws SAXException
Throws the parse exception if validation is enabled.

Specified by:
error in interface ErrorHandler
Overrides:
error in class DefaultHandler
Parameters:
pError -
Throws:
SAXException
See Also:
setValidationEnabled(boolean)

fatalError

public void fatalError(SAXParseException pError)
                throws SAXException
Throws the parse exception if validation is enabled.

Specified by:
fatalError in interface ErrorHandler
Overrides:
fatalError in class DefaultHandler
Parameters:
pError -
Throws:
SAXException
See Also:
setValidationEnabled(boolean)

startDocument

public void startDocument()

Specified by:
startDocument in interface ContentHandler
Overrides:
startDocument in class DefaultHandler

endDocument

public void endDocument()

Specified by:
endDocument in interface ContentHandler
Overrides:
endDocument in class DefaultHandler

startElement

public void startElement(String pNameSpaceURI,
                         String pLocalName,
                         String pName,
                         Attributes pAttr)
                  throws SAXException

Specified by:
startElement in interface ContentHandler
Overrides:
startElement in class DefaultHandler
Throws:
SAXException

characters

public void characters(char[] pChar,
                       int pStart,
                       int pLength)

Specified by:
characters in interface ContentHandler
Overrides:
characters in class DefaultHandler

endElement

public void endElement(String pURI,
                       String pLocalName,
                       String pName)
                throws SAXException

Specified by:
endElement in interface ContentHandler
Overrides:
endElement in class DefaultHandler
Throws:
SAXException

read

public XmlNode read(InputStream pXmlSource)
             throws SAXException,
                    IOException,
                    ParserConfigurationException
Parses a xml file and create a XmlNode structure.

Parameters:
pXmlSource - XML input source
Returns:
xml file in node structure
Throws:
SAXException - if the file has parse errors
IOException - if any IO error occurs
ParserConfigurationException - if the SAX parser is not well configured

write

public void write(OutputStream pXmlTarget,
                  XmlNode pNode)
           throws IOException
Writes the structure of a XmlNode as xml stream to the specified target stream.

Parameters:
pXmlTarget - target stream for the xml content
pNode - node for xml creation
Throws:
IOException - if the xml can not be created

setIndentation

public void setIndentation(int pIndent)
Sets the xml output indentation.

Parameters:
pIndent - space character count

getIndentation

public int getIndentation()
Gets the xml output indentation. This methods doesn't support the intentation of read xml files.

Returns:
space character count

setSchema

public void setSchema(String pSchema)
Sets the resource name of the schema file for validation of the xml.

Parameters:
pSchema - the resource name of the schema file /package/schema.xsd

getSchema

public String getSchema()
Gets the resource name of the schema file for validation of the xml.

Returns:
the resource name or null if schema validation is disabled

setValidationEnabled

public void setValidationEnabled(boolean pValidation)
Set the general validation of xml en- or disabled. The validation is disabled by default.

Parameters:
pValidation - true to enable the general validation or false to disable it
See Also:
isValidationEnabled()

isValidationEnabled

public boolean isValidationEnabled()
Gets the validation state.

Returns:
true if validation of xml is enabled, false otherwise
See Also:
setValidationEnabled(boolean)


Copyright © 2009 SIB Visions GmbH. All Rights Reserved.