|
||||||||||
| 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 LexicalHandlerSAXException
public void startCDATA()
throws SAXException
startCDATA in interface LexicalHandlerSAXException
public void endCDATA()
throws SAXException
endCDATA in interface LexicalHandlerSAXException
public void startDTD(String name,
String publicId,
String systemId)
throws SAXException
startDTD in interface LexicalHandlerSAXException
public void endDTD()
throws SAXException
endDTD in interface LexicalHandlerSAXException
public void startEntity(String name)
throws SAXException
startEntity in interface LexicalHandlerSAXException
public void endEntity(String name)
throws SAXException
endEntity in interface LexicalHandlerSAXExceptionpublic void setDocumentLocator(Locator pLocator)
setDocumentLocator in interface ContentHandlersetDocumentLocator in class DefaultHandlerpLocator - the locator instance
public void warning(SAXParseException pError)
throws SAXException
warning in interface ErrorHandlerwarning in class DefaultHandlerpError -
SAXExceptionsetValidationEnabled(boolean)
public void error(SAXParseException pError)
throws SAXException
error in interface ErrorHandlererror in class DefaultHandlerpError -
SAXExceptionsetValidationEnabled(boolean)
public void fatalError(SAXParseException pError)
throws SAXException
fatalError in interface ErrorHandlerfatalError in class DefaultHandlerpError -
SAXExceptionsetValidationEnabled(boolean)public void startDocument()
startDocument in interface ContentHandlerstartDocument in class DefaultHandlerpublic void endDocument()
endDocument in interface ContentHandlerendDocument in class DefaultHandler
public void startElement(String pNameSpaceURI,
String pLocalName,
String pName,
Attributes pAttr)
throws SAXException
startElement in interface ContentHandlerstartElement in class DefaultHandlerSAXException
public void characters(char[] pChar,
int pStart,
int pLength)
characters in interface ContentHandlercharacters in class DefaultHandler
public void endElement(String pURI,
String pLocalName,
String pName)
throws SAXException
endElement in interface ContentHandlerendElement in class DefaultHandlerSAXException
public void startPrefixMapping(String pPrefix,
String pUri)
startPrefixMapping in interface ContentHandlerstartPrefixMapping 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 configured
public 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 configured
public 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 created
public 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 | |||||||||