XML is one of the most common data exchange formats used between computers. It is based on the idea that every data can be sub-divided into several components, and that each such data can have a set of associated information or attributes. Let's go through this example to better understand what XML is.
Consider a computer. Let's say your computer was made by a manufacturer who has provided you with warrenty card. The warrenty card is attached to the product id of your computer and also contains information like purchase date, purchaser name, and some other details. All of these details can be considered as the attributes of your computer, that describe it.
Again this computer is made up of is made up of a few parts, namely, CPU, monitor, speaker, keyboard and mouse. These are the components of your computer. Note that each of these components have their own product ids' and specifications, i.e., their own attributes. This is how the structure of XML is.
class Xml |
|
---|---|
Xml (file) , (str) |
parse an xml file or string Xml xml = new Xml(new File("students.xml")); Xml xml = new Xml("<movie name="Xmlon"/>"); |
equals (xml) |
check if 2 xml elements are equal (content) boolean equals = xml1.equals(xml2); |
same (xml) |
check if 2 xml elements are same (content & reference) boolean same = xml.same(xml2); |
document () |
get the W3C document object Document doc = xml.document(); |
element () |
get current W3C element Element elem = xml.element(); |
root () |
get the root element Xml root = xml.root(); |
parent () , (name) |
get parent element (opt. with given name) Xml parent = xml.parent(); Xml hulk = xml.parent("hulk"); |
prev () , (name) |
get previous element (opt. with given name) Xml prev = xml.prev(); Xml mojo = xml.prev("mojo"); |
next () , (name) |
get next element (opt. with given name) Xml next = xml.next(); Xml bojo = xml.next("bojo"); |
child () , (name) |
get first child element (opt. with given name) Xml child = xml.child(); Xml bear = xml.child("bear"); |
lastChild () , (name) |
get last child element (opt. with given name) Xml lastChild = xml.lastChild(); Xml beaver = xml.lastChild("beaver"); |
children () , (name) |
get children elements (opt. with given name) List<Xml> children = xml.children(); List<Xml> ducks = xml.children("ducks"); |
elem (name) |
create new element with given name Xml ark = xml.elem("ark"); |
copy () |
copy current element to be used elsewhere Xml copy = xml.copy(); |
addPrev (xml) , (name) |
adds new element before current (returns new) Xml actor = xml.addPrev(actorXml); Xml sensor = xml.addPrev("sensor"); |
addNext (xml) , (name) |
adds new element after current (returns new) Xml nuclear = xml.addNext(nuclearXml); Xml fuel = xml.addNext("fuel"); |
add (xml) , (name) |
adds new element as child (returns new) Xml cocoa = xml.add(cocoaXml); Xml seed = xml.add("seed"); |
remove () |
removes current element (returns parent) Xml parent = xml.remove(); |
save (file) |
saves current element to file xml.save(new File("weapons.xml")); |
toString () |
converts current element to string String xmlStr = xml.toString(); |
name () , (name) |
get or set element name String name = xml.name(); xml.name("frog"); |
attr () , (name) , (name, val) |
get element attributes / get or set value of attribute Map<String, String> attrs = xml.attr(); String id = xml.attr("id"); xml.attr("name", "Chief"); |
val () , (val) |
get or set content of element String val = xml.val(); xml.val("family", "Castoridae"); |
childVal (name) , (name, val) |
get or set value of child element String val = xml.childVal("satellite"); xml.childVal("satellite", "ISS"); |