|
||||||||||
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(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 |
isAutomaticDecrypt()
Gets whether automatic decryption is enabled. |
boolean |
isEncrypted(String pNodeName)
Gets whether the node with the given name should be encrypted. |
boolean |
isInsertNewLines()
Gets whether new lines will be inserted for better readable. |
boolean |
isValidationEnabled()
Gets the validation state. |
XmlNode |
read(File pSource)
Parses a xml file and create a XmlNode structure. |
XmlNode |
read(InputStream pXmlSource)
Parses a xml file and create a XmlNode structure. |
void |
setAutomaticDecrypt(boolean pAutoDecrypt)
Sets whether automatic decryption should be used. |
void |
setDocumentLocator(Locator pLocator)
Caches the document locator for line numbering. |
void |
setEncrypted(String pNodeName,
boolean pEncrypt)
Sets the encryption mode of a node. |
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(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 |
startPrefixMapping(String pPrefix,
String pUri)
|
void |
warning(SAXParseException pError)
Throws the parse exception if validation is enabled. |
void |
write(File pTarget,
XmlNode pNode)
Writes the structure of a XmlNode as xml stream to the specified
target file. |
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, 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 SAXException
comment
in interface LexicalHandler
SAXException
public void startCDATA() throws SAXException
startCDATA
in interface LexicalHandler
SAXException
public void endCDATA() throws SAXException
endCDATA
in interface LexicalHandler
SAXException
public void startDTD(String name, String publicId, String systemId) throws SAXException
startDTD
in interface LexicalHandler
SAXException
public void endDTD() throws SAXException
endDTD
in interface LexicalHandler
SAXException
public void startEntity(String name) throws SAXException
startEntity
in interface LexicalHandler
SAXException
public void endEntity(String name) throws SAXException
endEntity
in interface LexicalHandler
SAXException
public void setDocumentLocator(Locator pLocator)
setDocumentLocator
in interface ContentHandler
setDocumentLocator
in class DefaultHandler
pLocator
- the locator instancepublic void warning(SAXParseException pError) throws SAXException
warning
in interface ErrorHandler
warning
in class DefaultHandler
pError
-
SAXException
setValidationEnabled(boolean)
public void error(SAXParseException pError) throws SAXException
error
in interface ErrorHandler
error
in class DefaultHandler
pError
-
SAXException
setValidationEnabled(boolean)
public void fatalError(SAXParseException pError) throws SAXException
fatalError
in interface ErrorHandler
fatalError
in class DefaultHandler
pError
-
SAXException
setValidationEnabled(boolean)
public void startDocument()
startDocument
in interface ContentHandler
startDocument
in class DefaultHandler
public void endDocument()
endDocument
in interface ContentHandler
endDocument
in class DefaultHandler
public void startElement(String pNameSpaceURI, String pLocalName, String pName, Attributes pAttr) throws SAXException
startElement
in interface ContentHandler
startElement
in class DefaultHandler
SAXException
public void characters(char[] pChar, int pStart, int pLength)
characters
in interface ContentHandler
characters
in class DefaultHandler
public void endElement(String pURI, String pLocalName, String pName) throws SAXException
endElement
in interface ContentHandler
endElement
in class DefaultHandler
SAXException
public void startPrefixMapping(String pPrefix, String pUri)
startPrefixMapping
in interface ContentHandler
startPrefixMapping
in class DefaultHandler
public XmlNode read(File pSource) throws SAXException, IOException, ParserConfigurationException
XmlNode
structure.
pSource
- XML input file
SAXException
- if the file has parse errors
IOException
- if any IO error occurs
ParserConfigurationException
- if the SAX parser is not well configuredpublic XmlNode read(InputStream pXmlSource) throws SAXException, IOException, ParserConfigurationException
XmlNode
structure.
pXmlSource
- XML input source
SAXException
- if the xml has parse errors
IOException
- if any IO error occurs
ParserConfigurationException
- if the SAX parser is not well configuredpublic void write(File pTarget, XmlNode pNode) throws IOException
XmlNode
as xml stream to the specified
target file.
pTarget
- target file for the xml contentpNode
- node for xml creation
IOException
- if the xml can not be createdpublic void write(OutputStream pXmlTarget, XmlNode pNode) throws IOException
XmlNode
as xml stream to the specified
target stream.
pXmlTarget
- target stream for the xml contentpNode
- node for xml creation
IOException
- if the xml can not be createdpublic void setIndentation(int pIndent)
pIndent
- space character countpublic int getIndentation()
public void setSchema(String pSchema)
pSchema
- the resource name of the schema file /package/schema.xsdpublic 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(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(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
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |