public class XML extends Object
Modifier and Type | Field and Description |
---|---|
static Document |
EMPTY_DOCUMENT
An empty XML DOM document.
|
static EntityResolver |
ENTERPRISE_ENTITY_RESOLVER
The resolver for entities for the JPL enterprise.
|
Constructor and Description |
---|
XML() |
Modifier and Type | Method and Description |
---|---|
static void |
add(Node node,
String name,
Collection values)
Add a repeating child element with text from the given collection to the given node.
|
static void |
add(Node node,
String name,
Object object)
Add a child element with the string representation of the given
object to the given node.
|
static void |
add(Node node,
String name,
String text)
Add a child element with the given text to the given element.
|
static void |
addNonNull(Node node,
String name,
String text)
Add a child element with the given text to the given element.
|
static Document |
createDocument()
Create a DOM document.
|
static DOMParser |
createDOMParser()
Create a DOM parser.
|
static SAXParser |
createSAXParser()
Create a SAX parser.
|
static void |
dump(PrintWriter writer,
Node node)
Dump the structure of the DOM tree rooted at the given node to the given writer.
|
static String |
escape(String source)
Escapes a string so it may be printed as text content or attribute value.
|
static DOMImplementation |
getDOMImplementation()
Get the DOM implementation.
|
static Document |
parse(InputSource inputSource)
Parse the given XML document into a DOM tree.
|
static Document |
parse(InputStream inputStream)
Parse the given XML document into a DOM tree.
|
static Document |
parse(Reader reader)
Parse the given XML document into a DOM tree.
|
static Document |
parse(String string)
Parse the given XML document into a DOM tree.
|
static void |
removeComments(Node node)
Remove all comments from the given document node's subtree.
|
static String |
serialize(Document doc)
Serialize an XML DOM document into a String.
|
static String |
serialize(Document doc,
boolean omitXMLDeclaration)
Serialize an XML DOM document into a String.
|
static void |
serialize(Document doc,
Writer writer)
Serialize an XML DOM document into a writer.
|
static void |
serialize(Document doc,
Writer writer,
boolean omitXMLDeclaration)
Serialize an XML DOM document into a writer.
|
static String |
text(Node node)
Get the text from the given DOM node.
|
static String |
unwrappedText(Node node)
Get unwrapped text from the given DOM node
This method unwraps any wrapped text.
|
public static final EntityResolver ENTERPRISE_ENTITY_RESOLVER
public static final Document EMPTY_DOCUMENT
public static DOMImplementation getDOMImplementation()
public static Document createDocument()
public static DOMParser createDOMParser()
public static SAXParser createSAXParser()
public static String serialize(Document doc, boolean omitXMLDeclaration)
doc
- The document.omitXMLDeclaration
- True if we should omit the XML declaration, false to keep the XML declaration.public static String serialize(Document doc)
doc
- The document.public static void serialize(Document doc, Writer writer, boolean omitXMLDeclaration)
doc
- The document.writer
- Where to write it.omitXMLDeclaration
- True if we should omit the XML declaration, false to keep the XML declaration.IOException
- If an I/O error occurs.public static void serialize(Document doc, Writer writer)
doc
- The document.writer
- Where to write it.IOException
- If an I/O error occurs.public static Document parse(InputSource inputSource) throws SAXException, IOException
inputSource
- The XML document to parse.SAXException
- If a parse error occurs.IOException
- If an I/O error occurs.public static Document parse(String string) throws SAXException
string
- The XML document to parse.SAXException
- If a parse error occurs.public static Document parse(Reader reader) throws SAXException, IOException
reader
- The XML document to parse.SAXException
- If a parse error occurs.IOException
- If an I/O error occurs.public static Document parse(InputStream inputStream) throws SAXException, IOException
inputStream
- The XML document to parse.SAXException
- If a parse error occurs.IOException
- If an I/O error occurs.public static void add(Node node, String name, Collection values) throws DOMException
List
with strings items
"a", "b", and "c", and name is "value", then the XML document will
have
<value>a</value> <value>b</value> <value>c</value>appended to node.
node
- Node to which to add child elements.name
- Name to give each child element.values
- Collection of values to assign to each child element, in iterator order.DOMException
- If a DOM error occurs.public static void addNonNull(Node node, String name, String text) throws DOMException
<node> ... </node>becomes
<node> ... <name>text>/name> </node>Adding a null name does nothing. Adding null text won't add the element.
node
- Node to which to add a child element.name
- Name of the child element to add to node.text
- What text the text-node child the child element named name should have. If null,
nothing happens.DOMException
- If a DOM error occurs.public static void add(Node node, String name, String text) throws DOMException
<node> ... </node>becomes
<node> ... <name>text>/name> </node>Adding a null name does nothing. Adding null text results in an empty name tag.
node
- Node to which to add a child element.name
- Name of the child element to add to node.text
- What text the text-node child the child element named name should have.DOMException
- If a DOM error occurs.public static void add(Node node, String name, Object object) throws DOMException
<node> ... </node>becomes
<node> ... <name>string-rep>/name> </node>Adding a null name does nothing. Adding null object results in an empty name tag.
node
- Node to which to add a child element.name
- Name of the child element to add to node.object
- The string representation of the object to have as the text-node
child the child element named name.DOMException
- If a DOM error occurs.public static String unwrappedText(Node node)
<node>Hello, world. This is my first document. </node>then the node's unwrapped text is
Hello, world. This is my first document.while the
text(org.w3c.dom.Node)
method would return the wrapped value
Hello, world. This is my first document.
In other words, it collects the text nodes under the given node and replaces strings of newlines and spaces with a single space. Unwrapping a null node returns a null string.
node
- The node.public static String text(Node node)
node
- The node.public static void dump(PrintWriter writer, Node node)
#text
), and for many nodes, the value is
null.writer
- The writer to which write the tree structure.node
- The tree to output.public static void removeComments(Node node)
node
- Node from which to search for comments to nuke.public static String escape(String source)
<
).
Taken from Xerces 1.0.3. Apache license applies; see source code for license.
source
- The string to escapeCopyright © 1999–2017 Apache OODT. All rights reserved.