public class XMLRead extends Object
The XMLRead and XMLWrite classes are simple to use and efficient. XMLWrite can be used to create an XML-formatted file with settings of any software. XMLRead, this class, helps in reading the file and provide methods to access the elements and their values. The class is typically used for reading scripts, for example:
<axes-ratio fix="false" value="0.65"/>
<axes-color red="0" green="0" blue="0"/>
<axis show="true">
<range min="0.0" max="10.0"/>
<scaling log="false" offset="0.0"/>
</axis>
The data can be accessed via the different get-methods, e.g.
XMLRead xr("myscript.xml");
boolean fix=false;
fix = xr.getBoolean("axes-ratio/fix",fix);
float ratio=1.0;
ratio = xr.getFloat("axes-ratio/value",ratio);
All elements can be accessed randomly, e.g.
min = xr.getFloat("axis/range/min",min);
is perfectly valid code. To avoid long strings, it is often more practical to set a local scope with the 'open' method:
xr.open("axis");
min = xr.getFloat("range/min",min);
max = xr.getFloat("range/max",max);
xr.close(); // closes 'axis'
Note that all elements beyond the local scope remain invisible until the scope is closed using the 'close()' method. Many more methods are available: check the methods below for more details.
XMLWrite
Constructor and Description |
---|
XMLRead()
Builds the XML tool class.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the current context and moves back to a more global one.
|
boolean |
getBoolean(String element,
boolean val)
Returns the value of a boolean element (true/false).
|
Color |
getColor(String element,
Color c)
Returns the value of a color element: it must be a color.
|
Vector<String> |
getData()
get all lines between the attribute
|
Dimension |
getDimension(String element,
Dimension d)
Returns the value of a specific element, which must be a dimension.
|
double |
getDouble(String element,
double val)
Returns the value of a number (double) element.
|
float |
getFloat(String element,
float val)
Returns the value of a number (float) element.
|
Font |
getFont(String element,
Font f)
Returns the value of a font element: it must be a font.
|
int |
getInt(String element,
int val)
Returns a value of a number (integer) element.
|
String |
getString(String element,
String val)
Returns the value of a string element.
|
Stroke |
getStroke(String element,
BasicStroke c)
Returns Stroke
|
void |
hide(String context)
Hides an element or a context from the current vector.
|
boolean |
open(String context)
Opens a specific context for finding elements.
|
boolean |
parse(BufferedReader in,
String context)
Reads a (part of a) file and stores the XML stuff in a vector.
|
void |
parse(File file) |
boolean |
parse(File file,
String context)
Reads a (part of a) file and stores the XML stuff in a vector.
|
void |
printVector() |
void |
unHide()
Unsets all the hidden contexts.
|
public boolean parse(File file, String context)
file
- file which contains the XML scriptcontext
- tag with the global context, i.e. only the stuff within this
context is actually read.public boolean parse(BufferedReader in, String context)
in
- BufferedReader which contains the XML scriptcontext
- tag with the global context, i.e. only the stuff within this
context is actually read.public void parse(File file)
public boolean open(String context)
context
- context element name.close()
public void close()
open(String context)
public void hide(String context)
context
- name of the elementpublic void unHide()
hide(String)
public void printVector()
public Dimension getDimension(String element, Dimension d)
element
- element or search pathd
- dimension, used to recycle memoryopen(String context)
public Color getColor(String element, Color c)
element
- element or search path.c
- color, default in case we didn't find the color in the script.open(String context)
public Stroke getStroke(String element, BasicStroke c)
public Font getFont(String element, Font f)
element
- element or search path, general a name such as 'font'f
- font, place-holder and initial fontopen(String context)
public int getInt(String element, int val)
element
- element or search path, such as foo or foo/barval
- default value, returned if some error occuredpublic double getDouble(String element, double val)
element
- element or search path, such as foo or foo/barval
- default value, returned if some error occuredpublic float getFloat(String element, float val)
element
- element or search path, such as foo or foo/barval
- default value, returned if some error occuredpublic String getString(String element, String val)
element
- element or search path, such as foo or foo/barval
- default value, returned if some error occuredpublic boolean getBoolean(String element, boolean val)
element
- element or search path, such as foo or foo/barval
- default value, returned if some error occuredJas4pp 1.5 © Java Analysis Studio for Particle Physics