public class XMLDocument extends Object implements RevisionHandler
PI
Modifier and Type | Field and Description |
---|---|
static String |
ATT_NAME
the "name" attribute.
|
static String |
ATT_VERSION
the "version" attribute.
|
static String |
DTD_ANY
the ANY placeholder.
|
static String |
DTD_AT_LEAST_ONE
the at least one marker.
|
static String |
DTD_ATTLIST
the AttList definition.
|
static String |
DTD_CDATA
the CDATA placeholder.
|
static String |
DTD_DOCTYPE
the DocType definition.
|
static String |
DTD_ELEMENT
the Element definition.
|
static String |
DTD_IMPLIED
the #IMPLIED placeholder.
|
static String |
DTD_OPTIONAL
the optional marker.
|
static String |
DTD_PCDATA
the #PCDATA placeholder.
|
static String |
DTD_REQUIRED
the #REQUIRED placeholder.
|
static String |
DTD_SEPARATOR
the option separator.
|
static String |
DTD_ZERO_OR_MORE
the zero or more marker.
|
static String |
PI
the parsing instructions "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
(may not show up in Javadoc due to tags!).
|
static String |
VAL_NO
the value "no".
|
static String |
VAL_YES
the value "yes".
|
Constructor and Description |
---|
XMLDocument()
initializes the factory with non-validating parser.
|
XMLDocument(File file)
Creates a new instance of XMLDocument.
|
XMLDocument(InputStream stream)
Creates a new instance of XMLDocument.
|
XMLDocument(Reader reader)
Creates a new instance of XMLDocument.
|
XMLDocument(String xml)
Creates a new instance of XMLDocument.
|
Modifier and Type | Method and Description |
---|---|
void |
clear()
sets up an empty DOM document, with the current DOCTYPE and root node.
|
Boolean |
evalBoolean(String xpath)
Evaluates and returns the boolean result of the XPath expression.
|
Double |
evalDouble(String xpath)
Evaluates and returns the double result of the XPath expression.
|
String |
evalString(String xpath)
Evaluates and returns the boolean result of the XPath expression.
|
NodeList |
findNodes(String xpath)
Returns the nodes that the given xpath expression will find in the
document.
|
DocumentBuilder |
getBuilder()
returns the DocumentBuilder.
|
static Vector |
getChildTags(Node parent)
returns all non tag-children from the given node.
|
static Vector |
getChildTags(Node parent,
String name)
returns all non tag-children from the given node.
|
static String |
getContent(Element node)
returns the text between the opening and closing tag of a node
(performs a
trim() on the result). |
String |
getDocType()
returns the current DOCTYPE, can be
null . |
Document |
getDocument()
returns the parsed DOM document.
|
DocumentBuilderFactory |
getFactory()
returns the DocumentBuilderFactory.
|
Node |
getNode(String xpath)
Returns the node represented by the XPath expression.
|
String |
getRevision()
Returns the revision string.
|
String |
getRootNode()
returns the current root node.
|
boolean |
getValidating()
returns whether a validating parser is used.
|
static void |
main(String[] args)
for testing only.
|
Document |
newDocument(String docType,
String rootNode)
creates a new Document with the given information.
|
void |
print()
prints the current DOM document to standard out.
|
Document |
read(File file)
parses the given file and returns a DOM document.
|
Document |
read(InputStream stream)
parses the given stream and returns a DOM document.
|
Document |
read(Reader reader)
parses the given reader and returns a DOM document.
|
Document |
read(String xml)
parses the given XML string (can be XML or a filename) and returns a
DOM Document.
|
void |
setDocType(String docType)
sets the DOCTYPE-String to use in the XML output.
|
void |
setDocument(Document newDocument)
sets the DOM document to use.
|
void |
setRootNode(String rootNode)
sets the root node to use in the XML output.
|
void |
setValidating(boolean validating)
sets whether to use a validating parser or not.
Note: this does clear the current DOM document! |
String |
toString()
returns the current DOM document as XML-string.
|
void |
write(File file)
writes the current DOM document into the given file.
|
void |
write(OutputStream stream)
writes the current DOM document into the given stream.
|
void |
write(String file)
writes the current DOM document into the given file.
|
void |
write(Writer writer)
writes the current DOM document into the given writer.
|
public static final String PI
public static final String DTD_DOCTYPE
public static final String DTD_ELEMENT
public static final String DTD_ATTLIST
public static final String DTD_OPTIONAL
public static final String DTD_AT_LEAST_ONE
public static final String DTD_ZERO_OR_MORE
public static final String DTD_SEPARATOR
public static final String DTD_CDATA
public static final String DTD_ANY
public static final String DTD_PCDATA
public static final String DTD_IMPLIED
public static final String DTD_REQUIRED
public static final String ATT_VERSION
public static final String ATT_NAME
public static final String VAL_YES
public static final String VAL_NO
public XMLDocument() throws Exception
Exception
- if the construction failspublic XMLDocument(String xml) throws Exception
xml
- the xml to parse (if "
Exception
- if the construction of the DocumentBuilder failssetValidating(boolean)
public XMLDocument(File file) throws Exception
file
- the XML file to parseException
- if the construction of the DocumentBuilder failssetValidating(boolean)
public XMLDocument(InputStream stream) throws Exception
stream
- the XML stream to parseException
- if the construction of the DocumentBuilder failssetValidating(boolean)
public XMLDocument(Reader reader) throws Exception
reader
- the XML reader to parseException
- if the construction of the DocumentBuilder failssetValidating(boolean)
public DocumentBuilderFactory getFactory()
public DocumentBuilder getBuilder()
public boolean getValidating()
public void setValidating(boolean validating) throws Exception
validating
- whether to use a validating parserException
- if the instantiating of the DocumentBuilder failspublic Document getDocument()
public void setDocument(Document newDocument)
newDocument
- the DOM document to usepublic void setDocType(String docType)
null
the DOCTYPE is omitted.docType
- the DOCTYPE definition to use in XML outputpublic String getDocType()
null
.null
public void setRootNode(String rootNode)
rootNode
- the root node to use in the XML outputpublic String getRootNode()
public void clear()
setRootNode(String)
,
setDocType(String)
public Document newDocument(String docType, String rootNode)
docType
- the DOCTYPE definition (no checking happens!), can be nullrootNode
- the name of the root node (must correspond to the one
given in docType
)public Document read(String xml) throws Exception
xml
- the xml to parse (if "
Exception
- if something goes wrong with the parsingpublic Document read(File file) throws Exception
file
- the XML file to parseException
- if something goes wrong with the parsingpublic Document read(InputStream stream) throws Exception
stream
- the XML stream to parseException
- if something goes wrong with the parsingpublic Document read(Reader reader) throws Exception
reader
- the XML reader to parseException
- if something goes wrong with the parsingpublic void write(String file) throws Exception
file
- the filename to write toException
- if something goes wrong with the parsingpublic void write(File file) throws Exception
file
- the filename to write toException
- if something goes wrong with the parsingpublic void write(OutputStream stream) throws Exception
stream
- the filename to write toException
- if something goes wrong with the parsingpublic void write(Writer writer) throws Exception
writer
- the filename to write toException
- if something goes wrong with the parsingpublic static Vector getChildTags(Node parent)
parent
- the node to get the children frompublic static Vector getChildTags(Node parent, String name)
parent
- the node to get the children fromname
- the name of the tags to return, "" for allpublic NodeList findNodes(String xpath)
xpath
- the XPath expression to run on the documentpublic Node getNode(String xpath)
xpath
- the XPath expression to run on the documentpublic Boolean evalBoolean(String xpath)
xpath
- the expression to evaluatepublic Double evalDouble(String xpath)
xpath
- the expression to evaluatepublic String evalString(String xpath)
xpath
- the expression to evaluatepublic static String getContent(Element node)
trim()
on the result).node
- the node to get the text frompublic void print()
public String toString()
public String getRevision()
getRevision
in interface RevisionHandler
public static void main(String[] args) throws Exception
args
- the commandline argumentsException
- if something goes wrongCopyright © 2019 University of Waikato, Hamilton, NZ. All rights reserved.