|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.yajul.xml.DOMUtil
public class DOMUtil
Provides commonly used DOM operations in convenient methods. Good for replacing many lines of DOM code with a nice one-liner. Works with any JAXP 1.1 implementation (e.g. XERCES/XALAN).
Constructor Summary | |
---|---|
DOMUtil()
|
Method Summary | |
---|---|
static org.w3c.dom.Element |
addChild(org.w3c.dom.Document document,
org.w3c.dom.Element parent,
java.lang.String childTag)
Adds a child element to the specified parent element. |
static org.w3c.dom.Element |
addChildWithText(org.w3c.dom.Document document,
org.w3c.dom.Element parent,
java.lang.String childTag,
java.lang.String text)
Adds a child element to the specified parent element containing some text. |
static org.w3c.dom.Document |
createDocument()
Creates a new DOM document with no root element. |
static org.w3c.dom.Document |
createDocument(java.lang.String rootElementTag)
Creates a new DOM document with the specified root element as the 'document element'. |
static java.util.Map<java.lang.String,java.lang.String> |
getAttributeMap(org.w3c.dom.Element elem)
Returns a map of all the attributes in the element. |
static java.util.List<java.lang.String> |
getAttributeNames(org.w3c.dom.Element elem)
Returns the names of all the attributes in the element as an array of strings. |
static boolean |
getBooleanAttribute(org.w3c.dom.Element elem,
java.lang.String attributeName,
boolean defaultValue)
Returns the value of the specified attribute as a boolean primitive. |
static java.util.List<org.w3c.dom.Element> |
getChildElements(org.w3c.dom.Document parent)
Returns a list of child elements. |
static java.util.List<org.w3c.dom.Element> |
getChildElements(org.w3c.dom.Document parent,
java.lang.String tag)
Returns an array of child elements with the specified tag name. |
static java.util.List<org.w3c.dom.Element> |
getChildElements(org.w3c.dom.Element parent)
Returns the list of child elements in a parent element |
static java.util.List<org.w3c.dom.Element> |
getChildElements(org.w3c.dom.Element parent,
java.lang.String tag)
Returns a list of ALL child elements in this element (either directly or inside a descendant) with the specified tag name. |
static java.lang.String |
getChildText(org.w3c.dom.Element element)
Returns the text inside an element as a string. |
static javax.xml.parsers.DocumentBuilder |
getDocumentBuilder()
Returns a new document builder. |
static int |
getIntAttribute(org.w3c.dom.Element elem,
java.lang.String attributeName,
int defaultValue)
Returns the value of the specified attribute as an int primitive. |
static org.w3c.dom.Document |
parse(java.io.InputStream input)
Parses the input stream and returns a DOM document. |
static org.w3c.dom.Document |
parse(org.xml.sax.XMLReader reader,
org.xml.sax.InputSource input)
Parse the input with the specified reader, producing a DOM Document. |
static org.w3c.dom.Document |
parseFile(java.io.File file)
Parse an XML file. |
static org.w3c.dom.Document |
parseFile(java.lang.String fileName)
Parse a file using it's name. |
static org.w3c.dom.Document |
parseResource(java.lang.String resourceName)
Parse an XML resource. |
static java.util.List<org.w3c.dom.Element> |
toElementList(org.w3c.dom.NodeList nodeList)
Returns an array of elements, given a node list. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
public DOMUtil()
Method Detail |
---|
public static org.w3c.dom.Document createDocument(java.lang.String rootElementTag) throws javax.xml.parsers.ParserConfigurationException
rootElementTag
- The tag name for the root element.
javax.xml.parsers.ParserConfigurationException
- - If the document could not
be created.public static org.w3c.dom.Document createDocument() throws javax.xml.parsers.ParserConfigurationException
javax.xml.parsers.ParserConfigurationException
- - If the document could not
be created.public static org.w3c.dom.Element addChild(org.w3c.dom.Document document, org.w3c.dom.Element parent, java.lang.String childTag)
document
- The document that contains the parent.parent
- The parent element.childTag
- The tag name for the new child.
public static org.w3c.dom.Element addChildWithText(org.w3c.dom.Document document, org.w3c.dom.Element parent, java.lang.String childTag, java.lang.String text)
document
- The document that contains the parent.parent
- The parent element.childTag
- The tag name for the new child.text
- The text that will be inside the new child. Note: If 'text' is null, a
zero length string will be used in order to avoid NPE's (in XALAN2) later on.
public static javax.xml.parsers.DocumentBuilder getDocumentBuilder() throws javax.xml.parsers.ParserConfigurationException
javax.xml.parsers.ParserConfigurationException
- - If the JAXP
implementation is configured incorrectlypublic static java.util.List<org.w3c.dom.Element> toElementList(org.w3c.dom.NodeList nodeList)
nodeList
- The node list.
public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element parent)
parent
- The parent element.
public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Document parent)
parent
- The parent document.
public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Element parent, java.lang.String tag)
parent
- The parent element.tag
- The child element tag name.
public static java.util.List<org.w3c.dom.Element> getChildElements(org.w3c.dom.Document parent, java.lang.String tag)
parent
- The parent element.tag
- - The child element tag name.
public static java.lang.String getChildText(org.w3c.dom.Element element)
element
- The element containing the text.
public static java.util.List<java.lang.String> getAttributeNames(org.w3c.dom.Element elem)
elem
- The element.
public static java.util.Map<java.lang.String,java.lang.String> getAttributeMap(org.w3c.dom.Element elem)
elem
- The element.
public static boolean getBooleanAttribute(org.w3c.dom.Element elem, java.lang.String attributeName, boolean defaultValue)
elem
- The element.attributeName
- The attribute name in the element.defaultValue
- The default value.
public static int getIntAttribute(org.w3c.dom.Element elem, java.lang.String attributeName, int defaultValue)
elem
- The element.attributeName
- The attribute name in the element.defaultValue
- The default value.
public static org.w3c.dom.Document parse(java.io.InputStream input) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
input
- - The input stream to parse.
javax.xml.parsers.ParserConfigurationException
- - If the JAXP
implementation is configured incorrectly
org.xml.sax.SAXException
- - If the document could not be parsed
java.io.IOException
- - If there was something wrong with the
input stream.public static org.w3c.dom.Document parseFile(java.lang.String fileName) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
fileName
- The name of the file to parse.
javax.xml.parsers.ParserConfigurationException
- - If the JAXP
implementation is configured incorrectly
org.xml.sax.SAXException
- - If the document could not be parsed
java.io.IOException
- - If there was something wrong with the
input stream.public static org.w3c.dom.Document parseFile(java.io.File file) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
file
- The file to parse.
javax.xml.parsers.ParserConfigurationException
- - If the JAXP
implementation is configured incorrectly
org.xml.sax.SAXException
- - If the document could not be parsed
java.io.IOException
- - If there was something wrong with the
input stream.public static org.w3c.dom.Document parseResource(java.lang.String resourceName) throws javax.xml.parsers.ParserConfigurationException, org.xml.sax.SAXException, java.io.IOException
resourceName
- The resource to parse.
javax.xml.parsers.ParserConfigurationException
- If the JAXP
implementation is configured incorrectly
org.xml.sax.SAXException
- If the document could not be parsed
java.io.IOException
- If there was something wrong with the
input stream.public static org.w3c.dom.Document parse(org.xml.sax.XMLReader reader, org.xml.sax.InputSource input) throws javax.xml.transform.TransformerConfigurationException, java.io.IOException, org.xml.sax.SAXException
reader
- The reader, which will produce SAX2 events.input
- The input source
javax.xml.transform.TransformerConfigurationException
- if the transformer doesn't support this operation.
java.io.IOException
- if the input cannot be read
org.xml.sax.SAXException
- if the input cannot be parsed
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |