public class XMLUtils extends java.lang.Object
Constructor and Description |
---|
XMLUtils() |
Modifier and Type | Method and Description |
---|---|
static org.w3c.dom.Element |
appendChild(org.w3c.dom.Element element,
java.lang.String tagName)
Appends a child element with the given tag name to the given element and
returns the new element.
|
static org.w3c.dom.Element |
appendChild(org.w3c.dom.Element parent,
java.lang.String tagName,
java.lang.String textContent)
Appends a child element with the given tag name and the given text content to
the given element and returns the new element.
|
static org.w3c.dom.Document |
createEmptyDocument()
Creates an empty XML document.
|
static javax.xml.stream.XMLEventReader |
createStAXParser(org.xml.sax.InputSource input)
Creates a StAX parser.
|
static <ElementsEnum extends java.lang.Enum<ElementsEnum>,AttributesEnum extends java.lang.Enum<AttributesEnum>> |
createUtf8Writer(java.io.File outputFile,
java.lang.Class<AttributesEnum> attributesClass)
Creates a new
XMLWriter that writes to the given output file. |
static java.util.List<org.w3c.dom.Element> |
elementNodes(org.w3c.dom.NodeList nodeList)
Extracts all ElementNodes from a NodeList and returns the result as a list.
|
static java.lang.String |
fixIllegalXmlChars(java.lang.String content)
Fixes chars which are not allowed in XML content.
|
static org.w3c.dom.Element |
getAncestor(org.w3c.dom.Element element,
int distance)
Returns the ancestor of the given element with the given distance.
|
static org.w3c.dom.Element |
getElementByChildContent(java.util.List<org.w3c.dom.Element> elements,
java.lang.String childName,
java.lang.String childContent)
Returns the first element whose child with the given name has the given text
content.
|
static int |
getElementPosition(org.w3c.dom.Element element)
Determines the index (starting at 0) of the given element relative to other
element nodes for the same parent.
|
static org.w3c.dom.Element |
getLastNamedChild(org.w3c.dom.Element element,
java.lang.String name)
Returns the last child of the given element which is an element named as
specified.
|
static org.w3c.dom.Element |
getNamedChild(org.w3c.dom.Element element,
java.lang.String name)
Returns the first child of the given element which is an element named as
specified.
|
static java.lang.String |
getNamedChildContent(org.w3c.dom.Element parent,
java.lang.String name)
Get the text content of the given element's first child that is an element
named as specified.
|
static java.util.List<org.w3c.dom.Element> |
getNamedChildren(org.w3c.dom.Element element,
java.lang.String elementNames)
Returns all children of the given element which are element named as
specified.
|
static java.util.List<org.w3c.dom.Element> |
leafElementNodes(org.w3c.dom.Element root)
Get all leaf elements of an XML tree rooted at an element
|
static org.w3c.dom.Document |
parse(java.io.File file)
Parse a file without validation.
|
static org.w3c.dom.Document |
parse(java.io.File file,
java.io.File schema)
Same as
parse(File, URL) but with schema file. |
static org.w3c.dom.Document |
parse(java.io.File file,
java.net.URL schemaURL)
Parse and validate file using schema.
|
static org.w3c.dom.Document |
parse(org.xml.sax.InputSource input)
Parse an input source without validation.
|
static org.w3c.dom.Document |
parse(org.xml.sax.InputSource input,
java.net.URL schemaURL)
Parse and validate file using schema.
|
static void |
parseSAX(java.io.File file,
org.xml.sax.helpers.DefaultHandler handler)
Parse an input source using SAX without validation.
|
static void |
parseSAX(java.io.File file,
java.net.URL schemaURL,
org.xml.sax.helpers.DefaultHandler handler)
Parse and validate file using SAX and schema.
|
static void |
parseSAX(org.xml.sax.InputSource input,
org.xml.sax.helpers.DefaultHandler handler)
Parse an input source using SAX without validation.
|
static void |
parseSAX(org.xml.sax.InputSource input,
java.net.URL schemaURL,
org.xml.sax.helpers.DefaultHandler handler)
Parse and validate file using SAX and schema.
|
static void |
parseSAX(java.lang.String content,
org.xml.sax.helpers.DefaultHandler handler)
Parse a string that contains XML without validation.
|
static java.lang.String |
prettyPrint(org.w3c.dom.Document doc)
Returns a string representation of the given XML document, which is "pretty
printed", i.e.
|
static java.lang.String |
print(org.w3c.dom.Document document)
Transforms the document to an xml string (flat, with no line breaks).
|
static void |
removeElement(org.w3c.dom.Element element)
Removes the given element from its parent.
|
static org.xml.sax.InputSource |
toInputSource(java.lang.String string)
Converts the given
String to a SAX InputSource . |
public XMLUtils()
public static <ElementsEnum extends java.lang.Enum<ElementsEnum>,AttributesEnum extends java.lang.Enum<AttributesEnum>> XMLWriter<ElementsEnum,AttributesEnum> createUtf8Writer(java.io.File outputFile, java.lang.Class<AttributesEnum> attributesClass) throws java.io.FileNotFoundException, java.io.UnsupportedEncodingException
XMLWriter
that writes to the given output file.java.io.FileNotFoundException
java.io.UnsupportedEncodingException
public static org.w3c.dom.Document parse(java.io.File file) throws org.xml.sax.SAXException, java.io.IOException
file
- the file to parse.org.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed.java.io.IOException
- if an IO exception occurs.public static org.w3c.dom.Document parse(org.xml.sax.InputSource input) throws org.xml.sax.SAXException, java.io.IOException
input
- the input source to parseorg.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed.java.io.IOException
- if an IO exception occurs.public static void parseSAX(java.io.File file, org.xml.sax.helpers.DefaultHandler handler) throws org.xml.sax.SAXException, java.io.IOException
org.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed.java.io.IOException
- if an IO exception occurs.public static void parseSAX(org.xml.sax.InputSource input, org.xml.sax.helpers.DefaultHandler handler) throws org.xml.sax.SAXException, java.io.IOException
org.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed.java.io.IOException
- if an IO exception occurs.public static void parseSAX(java.lang.String content, org.xml.sax.helpers.DefaultHandler handler) throws org.xml.sax.SAXException, java.io.IOException
org.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed.java.io.IOException
- if an IO exception occurs.public static org.w3c.dom.Document parse(java.io.File file, java.net.URL schemaURL) throws org.xml.sax.SAXException, java.io.IOException
file
- the file to parse.schemaURL
- URL point to schema, may not be nullorg.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed or not validjava.io.IOException
- if an IO exception occurs.public static org.w3c.dom.Document parse(org.xml.sax.InputSource input, java.net.URL schemaURL) throws org.xml.sax.SAXException, java.io.IOException
input
- the input to parse.schemaURL
- URL point to schema, may not be nullorg.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed or not validjava.io.IOException
- if an IO exception occurs.public static void parseSAX(java.io.File file, java.net.URL schemaURL, org.xml.sax.helpers.DefaultHandler handler) throws org.xml.sax.SAXException, java.io.IOException
file
- the file to parse.schemaURL
- URL point to schema, may not be nullorg.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed or not validjava.io.IOException
- if an IO exception occurs.public static void parseSAX(org.xml.sax.InputSource input, java.net.URL schemaURL, org.xml.sax.helpers.DefaultHandler handler) throws org.xml.sax.SAXException, java.io.IOException
input
- the input to parse.schemaURL
- URL point to schema, may not be nullorg.xml.sax.SAXException
- if a parsing exception occurs, i.e. if the file is not
well-formed or not validjava.io.IOException
- if an IO exception occurs.public static javax.xml.stream.XMLEventReader createStAXParser(org.xml.sax.InputSource input) throws javax.xml.stream.XMLStreamException
javax.xml.stream.XMLStreamException
public static org.w3c.dom.Document parse(java.io.File file, java.io.File schema) throws org.xml.sax.SAXException, java.io.IOException
parse(File, URL)
but with schema file.java.lang.IllegalArgumentException
- if the schema file could not be converted to an URLorg.xml.sax.SAXException
java.io.IOException
public static java.lang.String prettyPrint(org.w3c.dom.Document doc) throws javax.xml.transform.TransformerException
javax.xml.transform.TransformerException
public static java.lang.String print(org.w3c.dom.Document document) throws javax.xml.transform.TransformerException
prettyPrint(Document)
for readable xml output.javax.xml.transform.TransformerException
public static int getElementPosition(org.w3c.dom.Element element)
public static java.util.List<org.w3c.dom.Element> getNamedChildren(org.w3c.dom.Element element, java.lang.String elementNames)
public static org.w3c.dom.Element getNamedChild(org.w3c.dom.Element element, java.lang.String name)
public static org.w3c.dom.Element getLastNamedChild(org.w3c.dom.Element element, java.lang.String name)
public static java.lang.String getNamedChildContent(org.w3c.dom.Element parent, java.lang.String name)
public static org.w3c.dom.Element getElementByChildContent(java.util.List<org.w3c.dom.Element> elements, java.lang.String childName, java.lang.String childContent)
public static org.w3c.dom.Element getAncestor(org.w3c.dom.Element element, int distance)
public static java.util.List<org.w3c.dom.Element> elementNodes(org.w3c.dom.NodeList nodeList)
nodeList
- the NodeList to be searched for ElementNodes.public static void removeElement(org.w3c.dom.Element element)
public static org.w3c.dom.Element appendChild(org.w3c.dom.Element element, java.lang.String tagName)
public static org.w3c.dom.Element appendChild(org.w3c.dom.Element parent, java.lang.String tagName, java.lang.String textContent)
public static java.util.List<org.w3c.dom.Element> leafElementNodes(org.w3c.dom.Element root)
root
- The root elementpublic static org.w3c.dom.Document createEmptyDocument()
public static org.xml.sax.InputSource toInputSource(java.lang.String string)
String
to a SAX InputSource
.public static java.lang.String fixIllegalXmlChars(java.lang.String content)