Class XMLDocument
java.lang.Object
weka.core.xml.XMLDocument
- All Implemented Interfaces:
RevisionHandler
- Direct Known Subclasses:
XMLInstances
This class offers some methods for generating, reading and writing
XML documents.
It can only handle UTF-8.
It can only handle UTF-8.
- Version:
- $Revision: 1.9 $
- Author:
- FracPete (fracpete at waikato dot ac dot nz)
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final Stringthe "name" attribute.static final Stringthe "version" attribute.static final Stringthe ANY placeholder.static final Stringthe at least one marker.static final Stringthe AttList definition.static final Stringthe CDATA placeholder.static final Stringthe DocType definition.static final Stringthe Element definition.static final Stringthe #IMPLIED placeholder.static final Stringthe optional marker.static final Stringthe #PCDATA placeholder.static final Stringthe #REQUIRED placeholder.static final Stringthe option separator.static final Stringthe zero or more marker.static final Stringthe parsing instructions "<?xml version=\"1.0\" encoding=\"utf-8\"?>" (may not show up in Javadoc due to tags!).static final Stringthe value "no".static final Stringthe value "yes". -
Constructor Summary
ConstructorsConstructorDescriptioninitializes 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. -
Method Summary
Modifier and TypeMethodDescriptionvoidclear()sets up an empty DOM document, with the current DOCTYPE and root node.evalBoolean(String xpath) Evaluates and returns the boolean result of the XPath expression.evalDouble(String xpath) Evaluates and returns the double result of the XPath expression.evalString(String xpath) Evaluates and returns the boolean result of the XPath expression.Returns the nodes that the given xpath expression will find in the document.returns the DocumentBuilder.static VectorgetChildTags(Node parent) returns all non tag-children from the given node.static VectorgetChildTags(Node parent, String name) returns all non tag-children from the given node.static StringgetContent(Element node) returns the text between the opening and closing tag of a node (performs atrim()on the result).returns the current DOCTYPE, can benull.returns the parsed DOM document.returns the DocumentBuilderFactory.Returns the node represented by the XPath expression.Returns the revision string.returns the current root node.booleanreturns whether a validating parser is used.static voidfor testing only.newDocument(String docType, String rootNode) creates a new Document with the given information.voidprint()prints the current DOM document to standard out.parses the given file and returns a DOM document.read(InputStream stream) parses the given stream and returns a DOM document.parses the given reader and returns a DOM document.parses the given XML string (can be XML or a filename) and returns a DOM Document.voidsetDocType(String docType) sets the DOCTYPE-String to use in the XML output.voidsetDocument(Document newDocument) sets the DOM document to use.voidsetRootNode(String rootNode) sets the root node to use in the XML output.voidsetValidating(boolean validating) sets whether to use a validating parser or not.
Note: this does clear the current DOM document!toString()returns the current DOM document as XML-string.voidwrites the current DOM document into the given file.voidwrite(OutputStream stream) writes the current DOM document into the given stream.voidwrites the current DOM document into the given writer.voidwrites the current DOM document into the given file.
-
Field Details
-
PI
the parsing instructions "<?xml version=\"1.0\" encoding=\"utf-8\"?>" (may not show up in Javadoc due to tags!).- See Also:
-
DTD_DOCTYPE
-
DTD_ELEMENT
-
DTD_ATTLIST
-
DTD_OPTIONAL
-
DTD_AT_LEAST_ONE
-
DTD_ZERO_OR_MORE
-
DTD_SEPARATOR
-
DTD_CDATA
-
DTD_ANY
-
DTD_PCDATA
-
DTD_IMPLIED
-
DTD_REQUIRED
-
ATT_VERSION
-
ATT_NAME
-
VAL_YES
-
VAL_NO
-
-
Constructor Details
-
XMLDocument
-
XMLDocument
-
XMLDocument
-
XMLDocument
Creates a new instance of XMLDocument.- Parameters:
stream- the XML stream to parse- Throws:
Exception- if the construction of the DocumentBuilder fails- See Also:
-
XMLDocument
-
-
Method Details
-
getFactory
returns the DocumentBuilderFactory.- Returns:
- the DocumentBuilderFactory
-
getBuilder
-
getValidating
public boolean getValidating()returns whether a validating parser is used.- Returns:
- whether a validating parser is used
-
setValidating
-
getDocument
-
setDocument
sets the DOM document to use.- Parameters:
newDocument- the DOM document to use
-
setDocType
sets the DOCTYPE-String to use in the XML output. Performs NO checking! if it isnullthe DOCTYPE is omitted.- Parameters:
docType- the DOCTYPE definition to use in XML output
-
getDocType
returns the current DOCTYPE, can benull.- Returns:
- the current DOCTYPE definition, can be
null
-
setRootNode
sets the root node to use in the XML output. Performs NO checking with DOCTYPE!- Parameters:
rootNode- the root node to use in the XML output
-
getRootNode
-
clear
public void clear()sets up an empty DOM document, with the current DOCTYPE and root node.- See Also:
-
newDocument
creates a new Document with the given information.- Parameters:
docType- the DOCTYPE definition (no checking happens!), can be nullrootNode- the name of the root node (must correspond to the one given indocType)- Returns:
- returns the just created DOM document for convenience
-
read
parses the given XML string (can be XML or a filename) and returns a DOM Document.- Parameters:
xml- the xml to parse (if "invalid input: '<'?xml" is not found then it is considered a file)- Returns:
- the parsed DOM document
- Throws:
Exception- if something goes wrong with the parsing
-
read
-
read
parses the given stream and returns a DOM document.- Parameters:
stream- the XML stream to parse- Returns:
- the parsed DOM document
- Throws:
Exception- if something goes wrong with the parsing
-
read
-
write
-
write
-
write
writes the current DOM document into the given stream.- Parameters:
stream- the filename to write to- Throws:
Exception- if something goes wrong with the parsing
-
write
-
getChildTags
-
getChildTags
-
findNodes
-
getNode
-
evalBoolean
-
evalDouble
-
evalString
-
getContent
-
print
public void print()prints the current DOM document to standard out. -
toString
-
getRevision
Returns the revision string.- Specified by:
getRevisionin interfaceRevisionHandler- Returns:
- the revision
-
main
for testing only. takes the name of an XML file as first arg, reads that file, prints it to stdout and if a second filename is given, writes the parsed document to that again.- Parameters:
args- the commandline arguments- Throws:
Exception- if something goes wrong
-