Skip to main content

XML Builder functions

create#

The create function takes the string as input and returns the xml builder object, the object contains start and end tags of the string.

Syntax:

new NameSpace("xml").create(String tagName);

Returns: Object

Example:

xmlObject = new NameSpace("xml").create("bookstore");log xmlObject;

getXMLString#

The getXMLString function returns the xml string from the reference xmlObject.

Syntax:

xmlObject.getXMLString();

Returns: String

Example:

xmlObject = new NameSpace("xml").create("bookstore");log xmlObject.getXMLString(); //<bookstore/>

element#

The element function takes the elementName as input and returns the xmlObject, containing the reference xmlObject and the elementName inside that.

Syntax:

xmlObject.element(String elementName);

Returns: Object

Example:

xmlObject = new NameSpace("xml").create("bookstore");xmlObject2= xmlObject.element("book").text("7Habits").parent();log xmlObject2.getElement("book");

text#

The text function takes the string content as input and returns the xmlObject containing the content given along with the elements.

Syntax:

xmlObject.text(String content);

Returns: Object

Example:

xmlObject = new NameSpace("xml").create("bookstore");log xmlObject.text("7Habits").getXMLString();//<bookstore>7Habits</bookstore>

attribute#

The attribute function takes a key and value to be set as attribute of the xml tag as input and returns the xmlObject containing the tag with attribute given.

Syntax:

xmlObject.attribute(String key,String value);

Returns: Object

Example:

xmlObject = new NameSpace("xml").create("bookstore");log xmlObject.attribute("a","b").getXMLString(); //<bookstore a="b"/>

parent#

The parent function returns the pointer to the parent of the given element.

Syntax:

xmlObject.parent();

Returns: Object

Example:

xmlObject = new NameSpace("xml").create("bookstore");log xmlObject.element("book").parent(); // in this case the pointer returns to bookstore

parse#

The parse function takes the string containing xml content as input and returns the xmlObject by converting the xmlString into xml.

Syntax:

new NameSpace("xml").parse(String content);

Returns: Object

Example:

xmlString =  "<Emp id=\"1\"><name>Pankaj</name><age>25</age>\n"+"<role>Developer</role><gen>Male</gen></Emp>";xmlobj = new NameSpace("xml").parse(xmlString);log xmlobj.getXMLString();

getText#

The getText function returns the text in given reference xmlObject.

Syntax:

xmlObject.getText();

Returns: String

Example:

xmlObject = new NameSpace("xml").create("bookstore");xmlObject2 = xmlObject.text("7Habits");log xmlObject2.getText(); //7Habits

getElement#

The getElement function takes the elementName as input and returns the xmlObject of that element.

Syntax:

xmlObject.getElement(String elementName);

Returns: object

Example:

xmlObject = new NameSpace("xml").create("bookstore");xmlObject2= xmlObject.element("book").text("7Habits").parent();log xmlObject2.getElement("book");

getAllElement#

The getAllElement function takes the elementName as input and returns the xmlObject of all the elements.

Syntax:

xmlObject.getAllElement(String elementName);

Returns: object

Example:

xmlObject = new NameSpace("xml").create("bookstore");xmlObject2= xmlObject.element("book").text("7Habits").parent();log xmlObject2.getAllElement("book");

getAttribute#

The getAttribute function takes key name as input and returns the value of the key in the reference xmlObject.

Syntax:

xmlObject.getAttribute(String keyName);

Returns: String

Example:

xmlObject = new NameSpace("xml").create("bookstore");xmlObject2= xmlObject.attribute("a","b");log xmlObject2.getAttribute("a"); //b