This tutorial will guide you through the usage of a Kotlin library for handling XML elements and documents. The library includes classes and functions to create and manipulate XML structures.
The library utilizes color codes for printing colored text:
red
: Represents red color.green
: Represents green color.brightred
: Represents bright red color.reset
: Resets text color to default.
XMLElement
represents an XML element with a name, optional text content, and an optional parent element.
XMLElement(name: String, text: String = "", parent: XMLElement? = null)
: Creates an XML element with the given name, text content, and parent element.
getElementName()
: Returns the name of the XML element.getChildren()
: Returns a list of children elements.getParents()
: Returns the parent element.addElement(element: XMLElement)
: Adds a child element.removeElement(name: String, attributes: Map<String, String> = mapOf())
: Removes a child element by name and attributes.getAttributes()
: Returns the attributes of the element.addAttribute(key: String, value: String)
: Adds an attribute.updateAttribute(key: String, value: String)
: Updates an attribute.renameAttribute(key: String, newkey: String)
: Renames an attribute.removeAttribute(key: String)
: Removes an attribute.toText()
: Converts the element and its children to a string representation with colors.textToFile()
: Converts the element and its children to a string representation without colors and writes in a file.accept(visitor: (XMLElement) -> Boolean)
: Accepts a visitor function to traverse the element tree.
path
: Calculates the path of the element in the tree.
XMLDocument
represents an XML document containing a root XML element.
element
: The root XML element.
addRoot(element: XMLElement)
: Adds a root element to the document.removeElement(name: String)
: Removes an element by name.generateXML()
: Generates XML content as a string.generateXMLFile(name: String)
: Generates a file with the specified name.accept(visitor: (XMLElement) -> Boolean)
: Accepts a visitor function to traverse the document.addAttribute(name: String, attributename: String, attributevalue: String)
: Adds an attribute to an element.renameXMLElements(oldname: String, newname: String)
: Renames an XML element.renameAttributes(elementname: String, oldname: String, newname: String)
: Renames an attribute on an element.removeAttributes(entityname: String, attributename: String)
: Removes an attribute from an element.xPath(path: String)
: Finds elements matching the specified path.
This class provides annotations used for XML handling.
- Target:
AnnotationTarget.CLASS
,AnnotationTarget.PROPERTY
- Attributes:
name
: The XML element name.text
: The text content of the XML element.
- Target:
AnnotationTarget.PROPERTY
- Attributes:
name
: The name of the XML attribute.
- Target:
AnnotationTarget.CLASS
,AnnotationTarget.PROPERTY
- Target:
AnnotationTarget.PROPERTY
- Attributes:
attribute
: The class responsible for changing attribute values.
- Target:
AnnotationTarget.CLASS
- Attributes:
adapter
: The class responsible for adapting XML elements.
- Target:
AnnotationTarget.PROPERTY
The translate
function converts the objects into XML elements using the specified annotations.
- Parameters:
obj
: Any object to be translated into an XML element.
- Returns:
- An
XMLElement
that will represent the translated XML element.
- An
The translate
function iterates through the properties of the object and checks for annotations to generate the XML elements and attributes accordingly.
This class provides functions that can be used to create a directory structure.
- Description: Creates a new directory element with the specified name and builds its contents.
- Parameters:
name
: The name of the directory.build
: Lambda function to build the contents of the directory.
- Returns:
- An
XMLElement
representing the directory.
- An
- Description: Creates a new directory element within the current element and builds its contents.
- Parameters:
name
: The name of the directory.build
: Lambda function to build the contents of the directory.
- Returns:
- An
XMLElement
representing the directory inside another directory.
- An
- Description: Retrieves a directory with the specified name.
- Parameters:
name
: The name of the directory.
- Returns:
- An
XMLElement
representing the elements inside the directory.
- An