|
||||||||||
| 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"));
// 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 |
|---|
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 XmlNode read(InputStream pXmlSource)
throws SAXException,
IOException,
ParserConfigurationException
XmlNode structure.
pXmlSource - XML input source
SAXException - if the file has parse errors
IOException - if any IO error occurs
ParserConfigurationException - if the SAX parser is not well configured
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)
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||