XMLRoot
elements are Nodes which don't have a parent and have optionally a declared encodingXMLNode
or XMLRoot
using an unqualified name or a qualified name. For example:XMLNode node = new XMLNode("element"); XMLNode node1 = new XMLNode(new QName("http://org.test.names", "element", "p"));The constructor will automatically create a name with a prefix without a namespace URI if the provided name contains a colon. For example:
XMLNode node = new XMLNode("p:element"); // is equivalent to: XMLNode node = new XMLNode(new QName(XMLConstants.NULL_NS_URI, "element", "p"));All the classes working with
XMLNode
and XMLRoot
elements support using unqualified or qualified names.
XMLRoot root = new XMLRoot("root"); XMLNode child = root.addChild("theChild");The ordered list of direct children of a node can be retrieved with the XMLNode.getChildren() method. The XMLNode.getChild(int) allows to get the child at a particular index.
XMLNode child = parentNode.getChild(2); int index = parentNode.getIndex(child); // the value if index is 2 int indexInParent = child.getIndexInParent(); // the value if indexInParent is 2The XMLNode.getAllChildren(String) and XMLNode.getAllChildren(QName) methods allow to get all the children (direct and indirect) of a specified name or qualified name.
XMLNode node = new XMLNode("element"); node.addAttribute("p:name", "the content"); // is equivalent to: XMLNode node = new XMLNode("element"); QName qname = new QName(XMLConstants.NULL_NS_URI,"name", "p"); node.addAttribute(qname);
xmnls:<prefix>
name, then the API will bind a prefix to the associated URI value rather than add an attibute.XMLNode node = new XMLNode("element"); node.addAttribute("xmlns:xi", "http://www.w3.org/2001/XInclude");is equivalent to:
XMLNode node = new XMLNode("element"); node.bindPrefix("xi", "http://www.w3.org/2001/XInclude");
XMLRoot root = XMLNodeUtilities.getRootNode(<the XML file>); int valueAsInt = root.getAttributeValueAsInt("theAttribute");It is possible to specify a default value to rturn if the attribute can not be parsed as the specified numeric type, for example[1]
false
for a booleanint valueAsInt = root.getAttributeValueAsInt("theAttribute", -1); //the default value will be -1By default the method will only accept attributes which are of the specified numeric value. For example, with the following node:
<element key="2.52"/>Getting the value of the
key
attribute will return 0:int valueAsInt = node.getAttributeValueAsInt("key"); // result is 0It is possible to allow to cast the value, for example in the same case the result will be 2:
int valueAsInt = node.getAttributeValueAsInt("key", 0, false); // result is 2Several methods also allow to check if an attribute can be parsed in a specified type. For example:
int valueAsInt = node.attributeValueIsInt("key"); // result true if the attribute value can be parsed as an int
CDATA
of the node, as a String, a boolean, or any numeric type. For example:int valueAsInt = node.getCDATAValueAsInt();Several methods also allow to check if the
CDATA
can be parsed in a specified type. For example:int valueAsInt = node.CDATAValueIsInt("key"); // result true if the value can be parsed as an int
XMLNode node = new XMLNode("element"); node.bindPrefix("xi", "http://www.w3.org/2001/XInclude");will create the following node:
<element xmnls:xi="http://www.w3.org/2001/XInclude" />
false
for a booleanCopyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences