com.nonesole.persistence.tools
Class XMLHandler

java.lang.Object
  extended by com.nonesole.persistence.tools.XMLHandler

public class XMLHandler
extends java.lang.Object

XML Tools

Version:
1.0 - build in 2009-07-21
Author:
JACK LEE

Method Summary
 org.w3c.dom.Document createDocument()
          Create new Document
static XMLHandler getInstance()
          Get XMLHandler Object
 java.util.List<org.w3c.dom.Node> getNodes(org.w3c.dom.Node node, java.lang.String nodeName)
          Get list of node whose name is given
 org.w3c.dom.Document loadFile(java.lang.String filePath)
          Read XML File and return Document object
 org.w3c.dom.Document parse(byte[] xml)
          byte[] to Document
 org.w3c.dom.Document parse(java.lang.StringBuilder xmlString)
          Transform StringBuilder to Document
 byte[] parseToStream(org.w3c.dom.Document doc, java.lang.String character)
          Transform Document to byte[]
 java.lang.String parseToString(org.w3c.dom.Document doc, java.lang.String charset)
          Transform Document to String
 void saveFile(org.w3c.dom.Document doc, java.lang.String filePath, java.util.Properties property)
          Save Document Object to XML File
 org.w3c.dom.Element transformToAtrribute(java.lang.Object obj, org.w3c.dom.Element ele, org.w3c.dom.Document doc)
          Transform Object to Element
Field of Object is attribute of element.
 java.lang.String xmlDocumentToString(org.w3c.dom.Document doc, java.lang.String charactor)
          Transform Document to String
 java.lang.StringBuilder xmlDocumentToStringBuilder(org.w3c.dom.Document doc, java.lang.String character)
          Transform Document to StringBuilder
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

getInstance

public static XMLHandler getInstance()
Get XMLHandler Object

Returns:
XMLHandler Object

loadFile

public final org.w3c.dom.Document loadFile(java.lang.String filePath)
                                    throws javax.xml.parsers.ParserConfigurationException,
                                           org.xml.sax.SAXException,
                                           java.io.IOException
Read XML File and return Document object

Parameters:
filePath - String
Returns:
document Document
Throws:
javax.xml.parsers.ParserConfigurationException
java.io.IOException
org.xml.sax.SAXException

saveFile

public final void saveFile(org.w3c.dom.Document doc,
                           java.lang.String filePath,
                           java.util.Properties property)
                    throws javax.xml.transform.TransformerException
Save Document Object to XML File
 Example��
   Properties property = new Properties();
   property.put(OutputKeys.ENCODING, "GB2312");
   property.put(OutputKeys.INDENT, "yes");
   XMLHandler.getInstance()
        .saveFile(new Document(),"D:\\test.xml",property);
 

Parameters:
doc - Document
filePath -
property - Properties configuration set
Throws:
javax.xml.transform.TransformerException
java.lang.Exception

parse

public final org.w3c.dom.Document parse(java.lang.StringBuilder xmlString)
                                 throws javax.xml.parsers.ParserConfigurationException,
                                        org.xml.sax.SAXException,
                                        java.io.IOException
Transform StringBuilder to Document

Parameters:
xmlString - - StringBuilder
Returns:
Document
Throws:
javax.xml.parsers.ParserConfigurationException
java.io.IOException
org.xml.sax.SAXException

parse

public final org.w3c.dom.Document parse(byte[] xml)
                                 throws javax.xml.parsers.ParserConfigurationException,
                                        org.xml.sax.SAXException,
                                        java.io.IOException
byte[] to Document

Parameters:
xml - - the byte[] array object;
Returns:
Document Object;
Throws:
javax.xml.parsers.ParserConfigurationException
java.io.IOException
org.xml.sax.SAXException

createDocument

public final org.w3c.dom.Document createDocument()
                                          throws javax.xml.parsers.ParserConfigurationException
Create new Document

Returns:
DOM Document Object;
Throws:
javax.xml.parsers.ParserConfigurationException

transformToAtrribute

public final org.w3c.dom.Element transformToAtrribute(java.lang.Object obj,
                                                      org.w3c.dom.Element ele,
                                                      org.w3c.dom.Document doc)
                                               throws org.w3c.dom.DOMException,
                                                      OperationsException
Transform Object to Element
Field of Object is attribute of element. If it exists some objects implements java.util.Collection and java.util.Map, the sub element will be treated as child node of current element.
 Example��
        Document doc = XMLHandler.createDocument();
        CatalogEntry ce = new CatalogEntry();
        Element e = doc.createElement("entry");
        doc.appendChild(XMLHandler.getInstance().transformToAtrribute(ce, e, doc));
 XML File��
        
                
                        
                        
                
        
 

Parameters:
obj - Object needs transform
ele - Empty element
doc - Document object for creating new element
Returns:
result element
Throws:
org.w3c.dom.DOMException
OperationsException

parseToString

public final java.lang.String parseToString(org.w3c.dom.Document doc,
                                            java.lang.String charset)
Transform Document to String

Parameters:
doc - Document
charset - CharSet
Returns:
result

xmlDocumentToString

public final java.lang.String xmlDocumentToString(org.w3c.dom.Document doc,
                                                  java.lang.String charactor)
Transform Document to String

Parameters:
doc - Document
Returns:
result

xmlDocumentToStringBuilder

public final java.lang.StringBuilder xmlDocumentToStringBuilder(org.w3c.dom.Document doc,
                                                                java.lang.String character)
Transform Document to StringBuilder

Parameters:
doc - Document
Returns:
StringBuilder

parseToStream

public final byte[] parseToStream(org.w3c.dom.Document doc,
                                  java.lang.String character)
Transform Document to byte[]

Parameters:
doc - Document
Returns:
byte[]

getNodes

public final java.util.List<org.w3c.dom.Node> getNodes(org.w3c.dom.Node node,
                                                       java.lang.String nodeName)
Get list of node whose name is given

Parameters:
node - father node
nodeName - child node name
Returns:
list of child nodes