public class XmlFile
extends java.lang.Object
<foo> <bar attr="value" /> </foo>corresponds to the sections
getString("/foo/bar", "attr", null);returns "value"
<foo> <bar attr="value" /> <bar attr="other" /> </foo>Since there seems no way to have access to one of the "/foo/bar" tags, an autonumbering method is used, which adds an unqiue number to all duplicate tags. Better than guessing the assigned numbers, is to use the
getSections()
method which returns a Vector with all valid paths:
getSections("/foo/bar")will return something like
/foo/bar#000000/ /foo/bar#000001/Using that paths enables the direct access again.
<foo> <bar name="joe" attr="value" /> <bar name="carl" attr="other" /> </foo>To access a named tag directly just use a special encoding:
getString("/foo/\\bar\\carl", "attr", null);returns "other".
Constructor and Description |
---|
XmlFile()
construct an XmlFile object without an file.
|
XmlFile(java.lang.String fileName)
Deprecated.
use XmlFile() and readFile()
|
Modifier and Type | Method and Description |
---|---|
void |
addComment(java.lang.String section,
java.lang.String comment)
Set the specified content element of the section as String.
|
void |
addContent(java.lang.String section,
java.lang.String content) |
void |
clear()
Remove all content from this instance.
|
void |
clearSection(java.lang.String section)
Clear all content and children from this section.
|
java.lang.String |
createSection(java.lang.String section)
Creates always a new section and returns its unique path.
|
void |
dropSection(java.lang.String section)
Drop the section and remov all content and children from this section.
|
static ByteRef |
encode(java.lang.String s) |
static ByteRef |
encode(java.lang.String s,
java.lang.String encoding) |
void |
flush()
Write all internal data to the file.
|
java.util.Map<java.lang.String,java.lang.String> |
getAttributes(java.lang.String section)
Return a map with all attributes: name->value.
|
java.lang.String |
getContent(java.lang.String section)
Get the specified content element of the section as String.
|
byte[] |
getContentBytes(java.lang.String section)
Get the complete content of the section as byte array.
|
java.util.Vector<java.lang.String> |
getKeys(java.lang.String section)
get all attribute names (keys) for a section in the given XML file.
|
java.util.Vector<java.lang.String> |
getSections(java.lang.String path)
Get all sections for the given XML file, matching the path String.
|
java.lang.String |
getString(java.lang.String section,
java.lang.String attribute,
java.lang.String def)
get a value for an attribute in the section for the given XML file.
|
boolean |
isPreserveWhiteSpaces() |
void |
read(java.io.InputStream is)
Read and parse the specified intput stream.
|
void |
readFile(java.lang.String fileName)
read the specified xml file.
|
void |
readString(java.lang.String content)
Read the xml content from the given String.
|
java.util.Iterator<java.lang.String> |
sections(java.lang.String sectionName)
Get an Iterator for the child sections.
|
void |
setContent(java.lang.String section,
java.lang.String content)
add a comment to the section as String.
|
void |
setEncoding(java.lang.String encoding) |
void |
setFile(java.lang.String fileName)
set a new file name.
|
void |
setPreserveWhiteSpaces(boolean preserveWhiteSpaces) |
void |
setString(java.lang.String section,
java.lang.String attribute,
java.lang.String value)
Set a value for an attribute in the section for the given XML file.
|
void |
sort(java.lang.String section,
java.lang.String order)
Sort the entries by the specified order.
|
java.lang.String |
toString()
Displays the full XML content.
|
void |
write(java.io.OutputStream os)
Write the current XML file to given OutputStream.
|
public XmlFile(java.lang.String fileName)
fileName
- name of the used XML filepublic XmlFile()
public void readFile(java.lang.String fileName)
fileName
- the file namepublic void readString(java.lang.String content)
content
- the xml content.public void setFile(java.lang.String fileName)
fileName
- the new file namepublic void clear()
public void read(java.io.InputStream is)
is
- n input stream.public void flush()
public void write(java.io.OutputStream os) throws java.io.IOException
os
- the OutputStreamjava.io.IOException
- on errorpublic java.util.Vector<java.lang.String> getSections(java.lang.String path)
path
- the match Stringpublic java.util.Vector<java.lang.String> getKeys(java.lang.String section)
section
- the name of the section *public java.util.Map<java.lang.String,java.lang.String> getAttributes(java.lang.String section)
section
- the name of the section.public java.lang.String getString(java.lang.String section, java.lang.String attribute, java.lang.String def)
section
- the name of the sectionattribute
- the name of the attributedef
- a default value, if the attribute was not foundsetString(java.lang.String, java.lang.String, java.lang.String)
public void setString(java.lang.String section, java.lang.String attribute, java.lang.String value)
section
- the name of the sectionattribute
- the name of the attributevalue
- the new key valuegetString(java.lang.String, java.lang.String, java.lang.String)
public void clearSection(java.lang.String section)
section
- a path to a XML tagpublic void dropSection(java.lang.String section)
section
- a path to a XML tagpublic byte[] getContentBytes(java.lang.String section)
section
- the name of a section.public java.lang.String getContent(java.lang.String section)
<sometag attr1="a" ...>CONTENT</sometag>
section
- the name of a section.setContent(java.lang.String, java.lang.String)
public void addComment(java.lang.String section, java.lang.String comment)
section
- the name of a section.comment
- the new content.public void setContent(java.lang.String section, java.lang.String content)
section
- the name of a section.content
- the new content.getContent(java.lang.String)
public void addContent(java.lang.String section, java.lang.String content)
public static ByteRef encode(java.lang.String s)
public static ByteRef encode(java.lang.String s, java.lang.String encoding)
public void setEncoding(java.lang.String encoding)
public java.lang.String createSection(java.lang.String section)
section
- the section to be createdpublic void sort(java.lang.String section, java.lang.String order)
section
- the name of the section = a path to the xml tag.order
- the ordered child tag names which is applied to the existing children.public java.lang.String toString()
toString
in class java.lang.Object
Object.toString()
public java.util.Iterator<java.lang.String> sections(java.lang.String sectionName)
sectionName
- the section name is an XML path like "/foo/bar"public boolean isPreserveWhiteSpaces()
public void setPreserveWhiteSpaces(boolean preserveWhiteSpaces)