|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectorg.xml.sax.helpers.DefaultHandler
com.sibvisions.util.xml.XmlWorker
public class XmlWorker
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"));
// Access an attribute from a node
System.out.println(xmnRead.getNode("/archive/element/attribute0"));
// Change the value of a node
xmnRead.setNode("/archive/element(1)/user", "xml");
// Replace one node with another node (changes the node type to attribute and uses the new node name)
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");
// Set a new attribute with the name real_attrib_name
xmnRead.insertNode("/archive/element/attrib_name", new XmlNode(XmlNode.TYPE_ATTRIBUTE, "real_attrib_name", "value"));
// 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"));
// Insert a new-node without a value, as last node in the "element" list
xmnRead.insertNode("/archive/element", new XmlNode("last"));
// Save the new xml structure
xmw.write(new FileOutputStream("example_v2.xml"), xmnRead);
| 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(java.lang.String pURI,
java.lang.String pLocalName,
java.lang.String pName)
|
void |
endEntity(java.lang.String name)
|
void |
error(org.xml.sax.SAXParseException pError)
Throws the parse exception if validation is enabled. |
void |
fatalError(org.xml.sax.SAXParseException pError)
Throws the parse exception if validation is enabled. |
int |
getIndentation()
Gets the xml output indentation. |
java.lang.String |
getSchema()
Gets the resource name of the schema file for validation of the xml. |
boolean |
isAutomaticDecrypt()
Gets whether automatic decryption is enabled. |
boolean |
isEncrypted(java.lang.String pNodeName)
Gets whether the node with the given name should be encrypted. |
boolean |
isFormatWhitespaces()
Gets whether to format whitespaces in tag values. |
boolean |
isInsertNewLines()
Gets whether new lines will be inserted for better readable. |
boolean |
isValidationEnabled()
Gets the validation state. |
XmlNode |
read(java.io.File pSource)
Parses a xml file and create a XmlNode structure. |
XmlNode |
read(java.io.InputStream pXmlSource)
Parses a xml file and create a XmlNode structure. |
XmlNode |
readAndClose(java.io.InputStream pXmlSource)
Parses a xml file and create a XmlNode structure. |
static XmlNode |
readNode(java.io.File pFile)
Reads a file and returns the node. |
static XmlNode |
readNode(java.io.InputStream pStream)
Reads a stream and returns the node. |
void |
setAutomaticDecrypt(boolean pAutoDecrypt)
Sets whether automatic decryption should be used. |
void |
setDocumentLocator(org.xml.sax.Locator pLocator)
Caches the document locator for line numbering. |
void |
setEncrypted(java.lang.String pNodeName,
boolean pEncrypt)
Sets the encryption mode of a node. |
void |
setFormatWhitespaces(boolean pFormat)
Sets whether to format whitespaces in tag values. |
void |
setIndentation(int pIndent)
Sets the xml output indentation. |
void |
setInsertNewLines(boolean pInsertNewLines)
Sets whether new lines should be inserted for better readable xml files. |
void |
setSchema(java.lang.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(java.lang.String name,
java.lang.String publicId,
java.lang.String systemId)
|
void |
startElement(java.lang.String pNameSpaceURI,
java.lang.String pLocalName,
java.lang.String pName,
org.xml.sax.Attributes pAttr)
|
void |
startEntity(java.lang.String name)
|
void |
startPrefixMapping(java.lang.String pPrefix,
java.lang.String pUri)
|
void |
warning(org.xml.sax.SAXParseException pError)
Throws the parse exception if validation is enabled. |
void |
write(java.io.File pTarget,
XmlNode pNode)
Writes the structure of a XmlNode as xml stream to the specified
target file. |
void |
write(java.io.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, unparsedEntityDecl |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Constructor Detail |
|---|
public XmlWorker()
| Method Detail |
|---|
public void comment(char[] pChar,
int pStart,
int pLength)
throws org.xml.sax.SAXException
comment in interface org.xml.sax.ext.LexicalHandlerorg.xml.sax.SAXException
public void startCDATA()
throws org.xml.sax.SAXException
startCDATA in interface org.xml.sax.ext.LexicalHandlerorg.xml.sax.SAXException
public void endCDATA()
throws org.xml.sax.SAXException
endCDATA in interface org.xml.sax.ext.LexicalHandlerorg.xml.sax.SAXException
public void startDTD(java.lang.String name,
java.lang.String publicId,
java.lang.String systemId)
throws org.xml.sax.SAXException
startDTD in interface org.xml.sax.ext.LexicalHandlerorg.xml.sax.SAXException
public void endDTD()
throws org.xml.sax.SAXException
endDTD in interface org.xml.sax.ext.LexicalHandlerorg.xml.sax.SAXException
public void startEntity(java.lang.String name)
throws org.xml.sax.SAXException
startEntity in interface org.xml.sax.ext.LexicalHandlerorg.xml.sax.SAXException
public void endEntity(java.lang.String name)
throws org.xml.sax.SAXException
endEntity in interface org.xml.sax.ext.LexicalHandlerorg.xml.sax.SAXExceptionpublic void setDocumentLocator(org.xml.sax.Locator pLocator)
setDocumentLocator in interface org.xml.sax.ContentHandlersetDocumentLocator in class org.xml.sax.helpers.DefaultHandlerpLocator - the locator instance
public void warning(org.xml.sax.SAXParseException pError)
throws org.xml.sax.SAXException
warning in interface org.xml.sax.ErrorHandlerwarning in class org.xml.sax.helpers.DefaultHandlerpError -
org.xml.sax.SAXExceptionsetValidationEnabled(boolean)
public void error(org.xml.sax.SAXParseException pError)
throws org.xml.sax.SAXException
error in interface org.xml.sax.ErrorHandlererror in class org.xml.sax.helpers.DefaultHandlerpError -
org.xml.sax.SAXExceptionsetValidationEnabled(boolean)
public void fatalError(org.xml.sax.SAXParseException pError)
throws org.xml.sax.SAXException
fatalError in interface org.xml.sax.ErrorHandlerfatalError in class org.xml.sax.helpers.DefaultHandlerpError -
org.xml.sax.SAXExceptionsetValidationEnabled(boolean)public void startDocument()
startDocument in interface org.xml.sax.ContentHandlerstartDocument in class org.xml.sax.helpers.DefaultHandlerpublic void endDocument()
endDocument in interface org.xml.sax.ContentHandlerendDocument in class org.xml.sax.helpers.DefaultHandler
public void startElement(java.lang.String pNameSpaceURI,
java.lang.String pLocalName,
java.lang.String pName,
org.xml.sax.Attributes pAttr)
throws org.xml.sax.SAXException
startElement in interface org.xml.sax.ContentHandlerstartElement in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXException
public void characters(char[] pChar,
int pStart,
int pLength)
characters in interface org.xml.sax.ContentHandlercharacters in class org.xml.sax.helpers.DefaultHandler
public void endElement(java.lang.String pURI,
java.lang.String pLocalName,
java.lang.String pName)
throws org.xml.sax.SAXException
endElement in interface org.xml.sax.ContentHandlerendElement in class org.xml.sax.helpers.DefaultHandlerorg.xml.sax.SAXException
public void startPrefixMapping(java.lang.String pPrefix,
java.lang.String pUri)
startPrefixMapping in interface org.xml.sax.ContentHandlerstartPrefixMapping in class org.xml.sax.helpers.DefaultHandler
public XmlNode read(java.io.File pSource)
throws org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException
XmlNode structure.
pSource - XML input file
org.xml.sax.SAXException - if the file has parse errors
java.io.IOException - if any IO error occurs
javax.xml.parsers.ParserConfigurationException - if the SAX parser is not well configured
public XmlNode readAndClose(java.io.InputStream pXmlSource)
throws org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException
XmlNode structure.
This closes the stream after reading.
pXmlSource - XML input source
org.xml.sax.SAXException - if the xml has parse errors
java.io.IOException - if any IO error occurs
javax.xml.parsers.ParserConfigurationException - if the SAX parser is not well configured
public XmlNode read(java.io.InputStream pXmlSource)
throws org.xml.sax.SAXException,
java.io.IOException,
javax.xml.parsers.ParserConfigurationException
XmlNode structure.
pXmlSource - XML input source
org.xml.sax.SAXException - if the xml has parse errors
java.io.IOException - if any IO error occurs
javax.xml.parsers.ParserConfigurationException - if the SAX parser is not well configured
public void write(java.io.File pTarget,
XmlNode pNode)
throws java.io.IOException
XmlNode as xml stream to the specified
target file.
pTarget - target file for the xml contentpNode - node for xml creation
java.io.IOException - if the xml can not be created
public void write(java.io.OutputStream pXmlTarget,
XmlNode pNode)
throws java.io.IOException
XmlNode as xml stream to the specified
target stream.
pXmlTarget - target stream for the xml contentpNode - node for xml creation
java.io.IOException - if the xml can not be createdpublic void setIndentation(int pIndent)
pIndent - space character countpublic int getIndentation()
public void setSchema(java.lang.String pSchema)
pSchema - the resource name of the schema file /package/schema.xsdpublic java.lang.String getSchema()
null if schema validation is disabledpublic void setValidationEnabled(boolean pValidation)
pValidation - true to enable the general validation or false
to disable itisValidationEnabled()public boolean isValidationEnabled()
true if validation of xml is enabled, false otherwisesetValidationEnabled(boolean)public void setInsertNewLines(boolean pInsertNewLines)
OutputStream.
The default setting is false.
pInsertNewLines - true to insert new lineswrite(OutputStream, XmlNode)public boolean isInsertNewLines()
true if new lines will be inserted, false otherwisesetInsertNewLines(boolean)
public void setEncrypted(java.lang.String pNodeName,
boolean pEncrypt)
pNodeName - the node name without index. It is not possible to encrypt single list elements.pEncrypt - true to encrypt the node, false otherwisepublic boolean isEncrypted(java.lang.String pNodeName)
pNodeName - the node name without index
true if the node name is encryptedpublic void setAutomaticDecrypt(boolean pAutoDecrypt)
pAutoDecrypt - true to decrypt all tags with the attribute encoded="true",
false to ignore automatic decryptionpublic boolean isAutomaticDecrypt()
true if automatic node decryption is enabled, false otherwise
public static XmlNode readNode(java.io.File pFile)
throws java.lang.Exception
pFile - the file
java.lang.Exception - if reading file failed
public static XmlNode readNode(java.io.InputStream pStream)
throws java.lang.Exception
pStream - the stream
java.lang.Exception - if reading file failedpublic void setFormatWhitespaces(boolean pFormat)
pFormat - true to format, false to keep the value as it ispublic boolean isFormatWhitespaces()
true if whitespaces in tag values will be formatted, false otherwise
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||