Package com.nfbsoftware.xml
Class XmlDocument
java.lang.Object
com.nfbsoftware.xml.XmlDocument
- All Implemented Interfaces:
IXmlDocument,IXmlElement,Serializable
- Author:
- Brendan Clemenzi
- See Also:
-
Field Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionXmlDocument(IXmlDocument xmlDocument) XmlDocument(File file) XmlDocument(File file, String enc) XmlDocument(InputStream in, String enc) XmlDocument(Reader reader) XmlDocument(String xml) XmlDocument(Node node, Document doc) -
Method Summary
Modifier and TypeMethodDescriptionvoidaddChild(IXmlElement child) This appends an IXmlElement object to the this element.voidaddToChild(IXmlElement child, String parent) This method appends the input IXmlElement object to the element identified by the path.voidappendChildByName(String parent, XmlDocument child) clone(boolean deep) This method creates a copy of the existing IXmlElement object, including a clone of every child if and only if the deep parameter is set to be true.static NodecloneChildren(Document doc, Node child) Clone a child node using the input document nodecreateChild(String childName) This method creates a new child under this element with an element name equal to the childName parameter.createChild(String childName, String value) This method creates a new child under this element with an element name equal to the childName parameter and a value equal to the value parameter.createChildByName(String child) This method creates a standing alone XmlData object with sName and sValue.createChildren(String fullChildPath) This method creates aall of the child elements referenced by the child path.protected NodeexamineChild(Document rootDoc, Node child) Description of the MethodgetAttribute(String name) This method returns the attribute's value according to input attribute name.final StringgetAttributeByName(String sName) final intfinal StringgetAttributeNameByIndex(int index) This method returns all of the attribute names of the current nodeThis method returns all of the attribute values of the current node.getCDATASection(String childName) This method returns the descendent's value.protected final StringgetCDATATextValue(Node node) final StringgetCDATAValue(String sChild) getChild(int index) .This method returns the first child element identified by the path.getChildAttribute(String child, String attr) This method returns the attribute value of a child node.intThis method returns the number of children.This method returns all of this element's children.getChildrenByName(String name) This method returns all of this element's children with the given name.final intgetChildValue(String childName) This method returns the descendent's value.This method returns the document node of the internal XML document.getName()This method returns the name of this elementThis method returns the namespace of the current node.protected final StringgetNodeTextValue(Node node) final StringgetNodeValue(String sChild) This method returns the document node of the internal XML document.This method returns the root node of the internal XML document.Method getRootElement returns the root element for the xml document.This method returns the root node of the internal XML document.getValue()This method returns the element's valuevoidisValid()Determine if the xml document in questionvoidThis method replaces the content of the current root node with that of the input file.voidThis method replaces the content of the current root node with that of the input file.voidload(InputStream in) This method replaces the content of the current root node with that of the InputStream.voidload(InputStream in, String enc) This method replaces the content of the current root node with that of the InputStream.voidThis method replaces the content of the current root node with that of the Reader.voidThis method replaces the content of the current root node with that of the input string.protected NodeprvGetChildByName(String sChildName) Description of the Methodprotected NodeprvGetChildByName(String sChildName, Node rootNode) Description of the MethodvoidremoveAttribute(String attrName) This method removes an attribute from this element..removeChild(int index) This method removes the index-th child element.removeChild(String childName) This method removes a child element.voidremoveChildAttribute(String childName, String attrName) This method removes an attribute from a child elementremoveChildrenWithXpath(String xpathQuery) This method removes all child elements with the given xpath query.removeChildWithXpath(String xpathQuery) This method removes a single child element with the given xpath query.voidreplaceAllChildren(IXmlElement parent) Removes all of the element's children and adds all of the children of the provided parent element.replaceChild(int index, IXmlElement newChild) This method replaces the index-th element with the new element.replaceChild(String childName, IXmlElement newChild) This method replaces the child element with the new element.booleanreplaceFromReader(Reader reader) This method replaces the content of the current root node with that of the reader.booleanThis method replaces the content of the current root node with that of the InputStream.booleanreplaceFromStream(InputStream in, String encoding) This method replaces the content of the current root node with that of the InputStream.final booleanreplaceFromString(String xml) This method replaces the content of the current root node with that of the input string.selectChild(String xpathQuery) selectChildren(String xpathQuery) protected NodeListselectNodesViaXPath(String childName, Node root) setAttribute(String attrName, String value) Sets the value of an attribute of this element.voidsetCDATASection(String value) Sets this element's CDATA valueprotected final voidsetCDATASection(Node node, String sValue) protected final voidsetTextValue(Node node, String sValue) voidSets this element's valuetoString()voidThis method saves the stored XML document as a file.voidwrite(OutputStream output) This methods writes the content of the current XML document to the output stream.
-
Field Details
-
mDocument
mDocument stores the Document Node of the XML document. In W3C's DOM model, each XML document must have a Document node and a root Element node. -
mRootNode
mRootNode stores the root Element node of the XML document. -
SEPARATOR
sSeparator contains the separator symbol used in child path.- See Also:
-
-
Constructor Details
-
XmlDocument
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
XmlDocument
- Parameters:
file- The XML file.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
XmlDocument
- Parameters:
file- The file containing the XML documentenc- The encoding- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
XmlDocument
- Parameters:
in- The input stream with the xml document.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
XmlDocument
- Parameters:
in- The input stream with the xml document.enc- the encoding- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
XmlDocument
- Parameters:
reader- The reader with the xml document.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
XmlDocument
- Parameters:
node- Node to create element from.doc- Document to create element from.
-
XmlDocument
- Parameters:
xmlDocument- An xml document- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
XmlDocument
- Parameters:
xml- A string with hopefully valid XML.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
-
Method Details
-
addChild
Description copied from interface:IXmlElementThis appends an IXmlElement object to the this element.- Specified by:
addChildin interfaceIXmlElement- Parameters:
child- Child to be added to current root node of this object.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
addToChild
Description copied from interface:IXmlElementThis method appends the input IXmlElement object to the element identified by the path.- Specified by:
addToChildin interfaceIXmlElement- Parameters:
parent- Parent XML that will have the child added to it.child- XML Document- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
appendChildByName
- Parameters:
parent- Parent XML that will have the child added to it.child- XML Document- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
clone
Description copied from interface:IXmlElementThis method creates a copy of the existing IXmlElement object, including a clone of every child if and only if the deep parameter is set to be true.- Specified by:
clonein interfaceIXmlElement- Parameters:
deep- Iftrue, recursively clone the subtree under the specified node; iffalse, clone only the node itself (and its attributes, if it is anElement).- Returns:
- A new XMLDocument that should be a copy of the current one.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
createChild
Description copied from interface:IXmlElementThis method creates a new child under this element with an element name equal to the childName parameter. The returned element is referenced directly by this element's document after the operation.- Specified by:
createChildin interfaceIXmlElement- Parameters:
childName- the child name- Returns:
- XmlDocument Contains the newly created child node
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
createChild
Description copied from interface:IXmlElementThis method creates a new child under this element with an element name equal to the childName parameter and a value equal to the value parameter. The returned element is referenced directly by this element's document after the operation.- Specified by:
createChildin interfaceIXmlElement- Parameters:
childName- The element's desired name.value- The element's value.- Returns:
- A new XMLElement with the name and value assigned to it.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
createChildren
Description copied from interface:IXmlElementThis method creates aall of the child elements referenced by the child path. The returned element is referenced directly by this element's document after the operation succeeds.- Specified by:
createChildrenin interfaceIXmlElement- Parameters:
fullChildPath- The full child path for the element to be created from.- Returns:
- A new XmlElement from the child.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
createChildByName
This method creates a standing alone XmlData object with sName and sValue. Note: NameSpace is not supported for all the implementations.- Parameters:
child- Child name in XPath format, like REQUEST/HEADER/INFO- Returns:
- A new XML Document from the child.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getRootNode
This method returns the root node of the internal XML document.- Specified by:
getRootNodein interfaceIXmlElement- Returns:
- root node
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getDocument
This method returns the document node of the internal XML document.- Specified by:
getDocumentin interfaceIXmlElement- Returns:
- document node
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getPrivateElementNode
This method returns the root node of the internal XML document.- Returns:
- root node
-
getPrivateDocument
This method returns the document node of the internal XML document.- Returns:
- document node
-
getAttribute
Description copied from interface:IXmlElementThis method returns the attribute's value according to input attribute name.- Specified by:
getAttributein interfaceIXmlElement- Parameters:
name- The attribute name.- Returns:
- The attrivute's value. If there is no value it will return an empty string.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getAttributeByName
- Parameters:
sName- The attribute's name.- Returns:
- The attribute's value; if there is no value it will return an empty string.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getAttributeNames
Description copied from interface:IXmlElementThis method returns all of the attribute names of the current node- Specified by:
getAttributeNamesin interfaceIXmlElement- Returns:
- A collection of attribute names associated with this document.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getAttributeValues
Description copied from interface:IXmlElementThis method returns all of the attribute values of the current node.- Specified by:
getAttributeValuesin interfaceIXmlElement- Returns:
- A collection of attribute values associated with this document.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChild
.- Specified by:
getChildin interfaceIXmlElement- Parameters:
index- The desired position- Returns:
- The child
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
selectChildren
- Specified by:
selectChildrenin interfaceIXmlElement- Parameters:
xpathQuery- The XPath string designated the desired children.- Returns:
- A list of the matching children. If there are no matching children it will return an empty list.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
selectChild
- Specified by:
selectChildin interfaceIXmlElement- Parameters:
xpathQuery- The XPath string designated the desired child.- Returns:
- The element with the matching child. Returns null if there is no match.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChild
Description copied from interface:IXmlElementThis method returns the first child element identified by the path. If multiple children with the given name exist, the first child found is returned. This method throws an exception if no child element matches the specified name.- Specified by:
getChildin interfaceIXmlElement- Parameters:
childName- The child's name.- Returns:
- The corresponding element.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChildAttribute
Description copied from interface:IXmlElementThis method returns the attribute value of a child node.- Specified by:
getChildAttributein interfaceIXmlElement- Parameters:
child- The child.attr- The attribute of the child.- Returns:
- The matching value; if there is no matching value it will return an empty string.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChildrenCount
- Returns:
- The number of children in this document.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChildCount
Description copied from interface:IXmlElementThis method returns the number of children.- Specified by:
getChildCountin interfaceIXmlElement- Returns:
- The number of children.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChildren
Description copied from interface:IXmlElementThis method returns all of this element's children.- Specified by:
getChildrenin interfaceIXmlElement- Returns:
- A list of children in the current document.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChildrenByName
Description copied from interface:IXmlElementThis method returns all of this element's children with the given name.- Specified by:
getChildrenByNamein interfaceIXmlElement- Parameters:
name- The children's name.- Returns:
- A list of children matching the name; it will return an empty collection if there are no matches.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getChildValue
Description copied from interface:IXmlElementThis method returns the descendent's value.- Specified by:
getChildValuein interfaceIXmlElement- Parameters:
childName- The name of the child.- Returns:
- The value of the corresponding child.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getCDATASection
Description copied from interface:IXmlElementThis method returns the descendent's value.- Specified by:
getCDATASectionin interfaceIXmlElement- Parameters:
childName- The name of the child.- Returns:
- The corresponding CDATA of the child.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getNodeValue
- Parameters:
sChild- The name of the child.- Returns:
- The corresponding name value.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
selectNodesViaXPath
protected NodeList selectNodesViaXPath(String childName, Node root) throws XmlDocumentCheckedException - Parameters:
childName- The child name.root- The root node.- Returns:
- The corresponding nodes.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getNodeTextValue
- Parameters:
node- The node.- Returns:
- The value of the corresponding node or an empty string if there is no value.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getCDATAValue
- Parameters:
sChild- The child.- Returns:
- The corresponding CDATA value or an empty String if there is none.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getCDATATextValue
- Parameters:
node- The desired node.- Returns:
- The corresponding CDATA value or an empty String if there is none.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getName
Description copied from interface:IXmlElementThis method returns the name of this element- Specified by:
getNamein interfaceIXmlElement- Returns:
- The name of the root node.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getNameSpace
This method returns the namespace of the current node.- Specified by:
getNameSpacein interfaceIXmlDocument- Returns:
- The namespace of the current node.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getValue
This method returns the element's value- Specified by:
getValuein interfaceIXmlElement- Returns:
- the value of the element
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
load
This method replaces the content of the current root node with that of the input file.- Specified by:
loadin interfaceIXmlDocument- Parameters:
file- points to the XML file- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
load
This method replaces the content of the current root node with that of the input file.- Specified by:
loadin interfaceIXmlDocument- Parameters:
file- points to the XML fileenc- The name of the supported character encoding- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
load
This method replaces the content of the current root node with that of the InputStream.- Specified by:
loadin interfaceIXmlDocument- Parameters:
in- A stream containing an XML document.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
load
This method replaces the content of the current root node with that of the InputStream.- Specified by:
loadin interfaceIXmlDocument- Parameters:
in- A stream containing an XML document.enc- The name of the supported character encoding- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
load
This method replaces the content of the current root node with that of the Reader.- Specified by:
loadin interfaceIXmlDocument- Parameters:
reader- A reader containing an XML document.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
load
This method replaces the content of the current root node with that of the input string.- Specified by:
loadin interfaceIXmlDocument- Parameters:
xml- A string contains an XML document.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
removeAttribute
This method removes an attribute from this element..- Specified by:
removeAttributein interfaceIXmlElement- Parameters:
attrName- the name of the attribute- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
removeChild
This method removes the index-th child element.- Specified by:
removeChildin interfaceIXmlElement- Parameters:
index- the index of the child to remove- Returns:
- the removed child
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
removeChild
This method removes a child element.- Specified by:
removeChildin interfaceIXmlElement- Parameters:
childName- the name of the element to remove- Returns:
- the removed child
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
removeChildrenWithXpath
public List<IXmlElement> removeChildrenWithXpath(String xpathQuery) throws XmlDocumentCheckedException This method removes all child elements with the given xpath query.- Specified by:
removeChildrenWithXpathin interfaceIXmlElement- Parameters:
xpathQuery- query to identify elements- Returns:
- the removed child
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
removeChildWithXpath
This method removes a single child element with the given xpath query.- Specified by:
removeChildWithXpathin interfaceIXmlElement- Parameters:
xpathQuery- query to identify element- Returns:
- the removed child
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
removeChildAttribute
public void removeChildAttribute(String childName, String attrName) throws XmlDocumentCheckedException This method removes an attribute from a child element- Specified by:
removeChildAttributein interfaceIXmlElement- Parameters:
childName- the name of the child elementattrName- the name of the attribute to remove- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
replaceAllChildren
Removes all of the element's children and adds all of the children of the provided parent element.- Specified by:
replaceAllChildrenin interfaceIXmlElement- Parameters:
parent- the element whose children are added to this element- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
replaceChild
This method replaces the index-th element with the new element.- Specified by:
replaceChildin interfaceIXmlElement- Parameters:
index- the index of the child to replacenewChild- the child to add to this element- Returns:
- the replaced element
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
replaceChild
public IXmlElement replaceChild(String childName, IXmlElement newChild) throws XmlDocumentCheckedException This method replaces the child element with the new element.- Specified by:
replaceChildin interfaceIXmlElement- Parameters:
childName- the name of the child to replacenewChild- the child to add to this element- Returns:
- the replaced element
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
setAttribute
Sets the value of an attribute of this element. If the attribute doesn't exist, it is created.- Specified by:
setAttributein interfaceIXmlElement- Parameters:
attrName- the attribute namevalue- the attribute value- Returns:
- The element.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
setTextValue
- Parameters:
node- The specific node that will have its value set.sValue- The value.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
setCDATASection
- Parameters:
node- The specific node that will have its value set.sValue- The value.- Throws:
Exception- If something goes horribly wrong.
-
setCDATASection
Description copied from interface:IXmlElementSets this element's CDATA value- Specified by:
setCDATASectionin interfaceIXmlElement- Parameters:
value- The desired String that will become the target's value.- Throws:
Exception- If something goes horribly wrong.
-
setValue
Description copied from interface:IXmlElementSets this element's value- Specified by:
setValuein interfaceIXmlElement- Parameters:
value- The desired String that will become the node's value.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
write
Description copied from interface:IXmlElementThis method saves the stored XML document as a file.- Specified by:
writein interfaceIXmlElement- Parameters:
file- The file the XML will be saved to.- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
write
This methods writes the content of the current XML document to the output stream.- Specified by:
writein interfaceIXmlElement- Parameters:
output- Contains the output stream to write to- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getRootElement
Method getRootElement returns the root element for the xml document.- Specified by:
getRootElementin interfaceIXmlDocument- Returns:
- IXmlElement root xml element
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
toString
-
replaceFromStream
This method replaces the content of the current root node with that of the InputStream.- Parameters:
in- A stream containing an XML document.- Returns:
- boolean Indicates success or failure
-
replaceFromStream
public boolean replaceFromStream(InputStream in, String encoding) throws UnsupportedEncodingException This method replaces the content of the current root node with that of the InputStream.- Parameters:
in- A stream containing an XML document.encoding- The name of the supported character encoding- Returns:
- boolean Indicates success or failure
- Throws:
UnsupportedEncodingException- If the encoding is not supported.
-
replaceFromReader
This method replaces the content of the current root node with that of the reader.- Parameters:
reader- A reader stream with an XML document.- Returns:
- boolean Indicates success or failure
-
replaceFromString
This method replaces the content of the current root node with that of the input string.- Parameters:
xml- A string contains an XML document.- Returns:
- boolean Indicates success or failure
-
prvGetChildByName
Description of the Method- Parameters:
sChildName- The child element's name.- Returns:
- The node with the child element's name.
-
prvGetChildByName
Description of the Method- Parameters:
sChildName- Description of ParameterrootNode- Description of Parameter- Returns:
- Description of the Returned Value
-
examineChild
Description of the Method- Parameters:
rootDoc- Description of Parameterchild- Description of Parameter- Returns:
- Description of the Returned Value
- Throws:
XmlDocumentCheckedException- Description of Exception
-
cloneChildren
Clone a child node using the input document node- Parameters:
doc- Input document nodechild- Child node to clone- Returns:
- Cloned child node
- Throws:
XmlDocumentCheckedException- When fails to clone
-
getAttributeCount
- Returns:
- The number of attributes.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
isValid
Description copied from interface:IXmlDocumentDetermine if the xml document in question- Specified by:
isValidin interfaceIXmlDocument- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-
getAttributeNameByIndex
- Parameters:
index- Desired position.- Returns:
- Attribute name.
- Throws:
XmlDocumentCheckedException- If the XML is invalid.
-