tig.xml
Class DomDoc
- java.lang.Object
-
- tig.xml.DomDoc
-
- All Implemented Interfaces:
- GeneralConstants
public class DomDoc extends java.lang.Object implements GeneralConstants
Wrapps aorg.w3c.dom.Document, providing easy access to the document's contents.
Usage of this class is based on the notion of current path of an element, which describes its path to root node, using "/" as separator and the names of the node's ancestors.
For example, with the XML structure :<rootNode> <elt1> <elt2> <elt3/> </elt2> </elt1> </rootNode>the current path of elt3 is "rootNode/elt1/elt2".
So you specify which part of the document you want to access withsetCurrentPath(), and then use the other methods.
If the path is not set, it is considered as "", which is the path to root node. So the results are obtained for the whole document.
-
-
Field Summary
Fields Modifier and Type Field and Description static intREGEX_NORMALConstant indicating that regular expressions (ingetAttributesValues(String,String)) are interpreted the standard way.static intREGEX_WILDCARD_ONLYConstant indicating that regular expressions (ingetAttributesValues(String,String)) are modified before being interpreted : wildcards ('*') are changed to '.*'.
-
Constructor Summary
Constructors Constructor and Description DomDoc(org.w3c.dom.Document doc)Unique constructor.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description static booleanbelongsToPath(org.w3c.dom.Node node, java.lang.String path)Indicates if a node belongs to a given path.java.lang.String[][]getAttributesValues(java.lang.String tagName, java.lang.String attributeNames)Equivalent togetAttributesValues(String,String[])when 'attributeNames' contains only element, or when a regular expression is used to designate the attribute names.java.lang.String[][]getAttributesValues(java.lang.String tagName, java.lang.String[] attributeNames)Method to get the values of tags attributes.java.lang.StringgetAttributeValue(java.lang.String tagName, java.lang.String attributeName)Method to get the value of an attribute of a tag of current path.java.lang.StringgetCurrentPath()Returns the "current path".org.w3c.dom.DocumentgetDocument()Returns the Document that thisDomDocis wrapping.static java.lang.StringgetPath(org.w3c.dom.Node node)Returns a String representing the path of a node.static voidmain(java.lang.String[] args)voidsetCurrentPath(java.lang.String path)Sets the "current path".voidsetDocument(org.w3c.dom.Document doc)Sets the Document that thisDomDocis wrapping.voidsetRegExMode(int regExMode)Sets the "regular expression mode" ; seeREGEX_XXXconstants.
-
-
-
Field Detail
-
REGEX_NORMAL
public static final int REGEX_NORMAL
Constant indicating that regular expressions (ingetAttributesValues(String,String)) are interpreted the standard way.
Used withsetRegExMode(int).- See Also:
- Constant Field Values
-
REGEX_WILDCARD_ONLY
public static final int REGEX_WILDCARD_ONLY
Constant indicating that regular expressions (ingetAttributesValues(String,String)) are modified before being interpreted : wildcards ('*') are changed to '.*'. This mode permits to consider that the '*' is equivalent to any sequence of characters. Used withsetRegExMode(int).
This is the defalut mode.- See Also:
- Constant Field Values
-
-
Constructor Detail
-
DomDoc
public DomDoc(org.w3c.dom.Document doc)
Unique constructor.- Parameters:
doc- the DOM Document used to handle internally the data.
-
-
Method Detail
-
setCurrentPath
public void setCurrentPath(java.lang.String path)
Sets the "current path". See class documentation for more information.
-
getCurrentPath
public java.lang.String getCurrentPath()
Returns the "current path". See class documentation for more information.
-
setDocument
public void setDocument(org.w3c.dom.Document doc)
Sets the Document that thisDomDocis wrapping. The current path is set to empty String (root node).
-
getDocument
public org.w3c.dom.Document getDocument()
Returns the Document that thisDomDocis wrapping.
-
setRegExMode
public void setRegExMode(int regExMode)
Sets the "regular expression mode" ; seeREGEX_XXXconstants.
-
getAttributeValue
public java.lang.String getAttributeValue(java.lang.String tagName, java.lang.String attributeName)Method to get the value of an attribute of a tag of current path.
Convenient if the document contains one and only one tag 'tagName' ; this tag must have an attribute 'attributeName'. Written to directly get a String.
Example :<myXmlDoc> ... <theTag theAttribute = "theValue"> ... </myXmlDoc>The callgetAttributeValue(theTag, theAttribute)will return "theValue".
This method is sensitive to last call tosetCurrentPath(String)
To retrieve attribute values of several tags, or several attribute values of a tag, usegetAttributesValues(String,String[]).- Returns:
- The value of the specified attribute if one is found - Also works if the doc contains
several pairs {tag, attribute}. In this case, the first tag encountered is returned.
Returns null in the following cases :- if 'tagName' or 'attributeName' is null ;
- if the document contains more than one tag 'tagName' ;
if there is no such pair {tag, attribute} (tag or attribute not found).
-
getAttributesValues
public java.lang.String[][] getAttributesValues(java.lang.String tagName, java.lang.String[] attributeNames) throws java.lang.ExceptionMethod to get the values of tags attributes. Return results for tags of the current path.
This method can be used to retrieve the attribute values of tags named 'tagName'.
Example :<myXmlDoc> ... <theTag attr1 = "val1.1" attr2 = "val1.2" attr3="val1.3"/> <theTag attr1 = "val2.1" attr3="val2.3"/> <theTag attr1 = "val3.1" attr2 = "val3.2" attr3="val3.3"/> ... </myXmlDoc>The code :String[] attributeNames = new String[]{"attr1", "attr2"}; getAttributesValues("theTag", attributeNames);will return a String[] containing :{ {"val1.1", "val1.2"}, {"val2.1", null}, {"val3.1", "val3.2"} }- Returns:
- the list as specified above or null if the document doesn't contain 'tagName' tags.
If attributes are missing, null is put to the corresponding place of the the resulting array.
Returns null if parameter 'tagName' is null, if 'attributeNames' is null or empty - Throws:
java.lang.Exception
-
getAttributesValues
public java.lang.String[][] getAttributesValues(java.lang.String tagName, java.lang.String attributeNames)Equivalent togetAttributesValues(String,String[])when 'attributeNames' contains only element, or when a regular expression is used to designate the attribute names.
The most common use of regular expressions is to use a wildcard ('*') to replace any sequence of character. For more details, see documentation of classjava.util.regex.Pattern.
WARNING : the returned array is not filled with 'null' if the match is not found, like ingetAttributesValues(String,String[]).
Example :<myXmlDoc> ... <theTag attr1 = "val1.1" attr2 = "val1.2" attr3="val1.3"/> <theTag attr1 = "val2.1" attr3="val2.3"/> <theTag attr1 = "val3.1" attr2 = "val3.2" attr3="val3.3"/> ... </myXmlDoc>The code :getAttributesValues("theTag", "attr*");will return a String[] containing :{ {"val1.1", "val1.2", "val1.3"}, {"val2.1", "val2.3"}, {"val3.1", "val3.2", "val3.3"} }
-
getPath
public static java.lang.String getPath(org.w3c.dom.Node node)
Returns a String representing the path of a node.
The String is built with the names of the parent nodes separated by "/".
-
belongsToPath
public static boolean belongsToPath(org.w3c.dom.Node node, java.lang.String path)Indicates if a node belongs to a given path.
-
main
public static void main(java.lang.String[] args)
-
-
DMelt 3.0 © DataMelt by jWork.ORG