Skip to content
Web

XML

Overview

AMI supports building and/or modification of XML files. This section will discuss the various ways to work with XML files in AMI.

Contents:

  • AMI XML Classes
  • Example Workflows

AMI XML Classes

XmlParser

Method Return Type Description
constructor XmlParser Creates a XML parser.
getClassName() String Returns the string name of this object's class type.
parseDocument(String xml) XmlElement Parses an XML string and returns an XmlElement object.
toJson() String Returns a JSON representation of this object.

Constructor

Creates an XML parser.

XmlParser parser = new XmlParser();
com.f1.utils.xml.XmlParser@663982b4


getClassName()

Creates a xml parser.

XmlParser parser = new XmlParser();
parser.getClassName();
XmlParser


parseDocument(string xml)

Parses an xml string and returns an XmlElement object.

1
2
3
4
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
<note date="20240612"><to>Adam</to><from>Bob</from><heading>Reminder</heading><body>Don't forget the milk!</body></note>
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


toJson()

Returns a json representation of this object.

XmlParser parser = new XmlParser();
parser.toJson();
"com.f1.utils.xml.XmlParser@319b3f3d"


XmlText

Method Return Type Description
constructor(string string) XmlParser Creates an XmlText node based on the given string.
getClassName() String Returns the string name of this object's class type.
toJson() String Returns a JSON representation of this object.
toLegibleString() String Returns the XmlText node as a legible string.
toString() String Returns the XmlText node as a string.

Constructor

Creates an XmlText node based on the given string.

XmlText textnode = new xmltext("Hello, World!");
Hello, World!


getClassName()

Creates a xml parser.

XmlText textnode = new xmltext("Hello, World!");
textnode.getClassName();
XmlText


toJson()

Returns a json representation of this object.

XmlText textnode = new xmltext("Hello, World!");
textnode.toJson();
"Hello, World!"


toLegibleString()

Returns the XmlText node as a legible string.

XmlText textnode = new xmltext("Hello, World!");
textnode.toLegibleString();
Hello, World!


toString()

Returns the XmlText node as a string.

XmlText textnode = new xmltext("Hello, World!");
textnode.toString();
Hello, World!


XmlElement

Method Return Type Description
constructor(String xmlString) XmlElement Creates an XML Element from an XML String
addAttribute(String key, String value) XmlElement Add an attribute to the XmlElement corresponding to the key-value pair.
addAttribute(String key, Long value) XmlElement Add an attribute to the XmlElement corresponding to the key-value pair.
addAttribute(String key, Double value) XmlElement Add an attribute to the XmlElement corresponding to the key-value pair.
addNode(XmlText xmlText) XmlElement Add an existing XmlText node to the XmlElement.
getAttribute(String name) String Gets the attribute specified by a name.
getAttributes() Map Returns any attributes of the current element.
getChildren() List Returns a list of the XmlElement's children.
getClassName() String Returns the string name of this object's class type.
getElements() List Returns any elements within the current element.
getElements(String name) String Returns any elements within the current element based on the name specified.
getFirstElement(String name) XmlElement Returns the first element with the specified name.
getInnerAsString() String Returns inner value of element as string.
getName() String Returns tag name of current element.
hasAttribute(String key) String Returns tag name of current element.
removeAttribute(String name) XmlElement Remove an attribute based on the attribute name entered.
toJson() String Returns a JSON representation of this object.
toLegibleString() String Returns the XmlElement as a legible string.
toString() String Returns the XmlElement as a string.

Constructor(string xmlString)

Creates an XML Element from an XML String.

XmlElement element = new XmlElement("person");
<person/>


addAttribute(String key, String value)

Add an attribute to the XmlElement corresponding to the key-value pair.

XmlElement element = new XmlElement("person");
element.addAttribute("name", "John");
<person name="John"/>


addAttribute(String key, Long value)

Add an attribute to the XmlElement corresponding to the key-value pair.

XmlElement element = new XmlElement("person");
element.addAttribute("age", 35L);
<person age="35"/>


addAttribute(String key, Double value)

Add an attribute to the XmlElement corresponding to the key-value pair.

XmlElement element = new XmlElement("person");
element.addAttribute("height", 1.75);
<person height=1.75/>


addNode(XmlText xmltext)

Add an existing XmlText node to the XmlElement.

1
2
3
XmlElement element = new XmlElement("person");
element.addAttribute("name", "John");
element.addNode(new xmltext("John loves to travel."));
<person name="John">John loves to travel.</person>


getAttribute(String name)

Gets the attribute specified by a name.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../people.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getAttribute("totalCount");
2
<people totalCount="2" version="1.0">
    <person name="John Doe" age="30" height="1.75" weight="75kg">
        <contact>
            <email>john.doe@example.com</email>
            <phone>123-456-7890</phone>
        </contact>
        <hobbies>
            <hobby>Hiking</hobby>
            <hobby>Photography</hobby>
            <hobby>Reading</hobby>
        </hobbies>
    </person>
    <person name="Jane Smith" age="28" height="1.65" weight="60kg">
        <contact>
            <email>jane.smith@example.com</email>
            <phone>234-567-8901</phone>
        </contact>
        <hobbies>
            <hobby>Traveling</hobby>
            <hobby>Cooking</hobby>
        </hobbies>
    </person>
</people>


getAttributes()

Returns any attributes of the current element.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../people.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getAttributes(); 
{"totalCount":"2", "version":"1.0"}
<people totalCount="2" version="1.0">
    <person name="John Doe" age="30" height="1.75" weight="75kg">
        <contact>
            <email>john.doe@example.com</email>
            <phone>123-456-7890</phone>
        </contact>
        <hobbies>
            <hobby>Hiking</hobby>
            <hobby>Photography</hobby>
            <hobby>Reading</hobby>
        </hobbies>
    </person>
    <person name="Jane Smith" age="28" height="1.65" weight="60kg">
        <contact>
            <email>jane.smith@example.com</email>
            <phone>234-567-8901</phone>
        </contact>
        <hobbies>
            <hobby>Traveling</hobby>
            <hobby>Cooking</hobby>
        </hobbies>
    </person>
</people>


getChildren()

Returns a list of the XmlElement's children.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getChildren(); 
[<to>Adam</to>, <from>Bob</from>, <heading>Reminder</heading>, <body>Don't forget the milk!</body>]
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


getClassName()

Returns the string name of this object's class type.

XmlElement element = new XmlElement("person");
element.getClassName();
XmlElement


getElements()

Returns any elements within the current element.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getElements();
[<to>Adam</to>, <from>Bob</from>, <heading>Reminder</heading>, <body>Don't forget the milk!</body>]
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


getElements(String name)

Returns any elements within the current element based on the name specified.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getElements("body");
[<body>Don't forget the milk!</body>]
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


getFirstElement(String name)

Returns the first element with the specified name.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../people.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getFirstElements("person");
<person name="John Doe" age="30" height="1.75" weight="75kg"><contact><email>john.doe@example.com</email><phone>123-456-7890</phone></contact><hobbies><hobby>Hiking</hobby><hobby>Photography</hobby><hobby>Reading</hobby></hobbies></person>
<people totalCount="2" version="1.0">
    <person name="John Doe" age="30" height="1.75" weight="75kg">
        <contact>
            <email>john.doe@example.com</email>
            <phone>123-456-7890</phone>
        </contact>
        <hobbies>
            <hobby>Hiking</hobby>
            <hobby>Photography</hobby>
            <hobby>Reading</hobby>
        </hobbies>
    </person>
    <person name="Jane Smith" age="28" height="1.65" weight="60kg">
        <contact>
            <email>jane.smith@example.com</email>
            <phone>234-567-8901</phone>
        </contact>
        <hobbies>
            <hobby>Traveling</hobby>
            <hobby>Cooking</hobby>
        </hobbies>
    </person>
</people>


getInnerAsString()

Returns inner value of element as string.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getInnerAsString();
<to>Adam</to><from>Bob</from><heading>Reminder</heading><body>Don't forget the milk!</body>
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


getName()

Returns tag name of current element.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.getName();
note
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


hasAttribute(String key)

Check if the XmlElement contains an attribute based on the entered key.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.hasAttribute("date");
true
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


removeAttribute(String name)

Remove an attribute based on the attribute name entered.

1
2
3
4
5
6
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.hasAttribute("date");
element;
<note><to>Adam</to><from>Bob</from><heading>Reminder</heading><body>Don't forget the milk!</body></note>
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


toJson()

Returns a JSON representation of this object..

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.toJson();
"<note date=\"20240612\"><to>Adam</to><from>Bob</from><heading>Reminder</heading><body>Don't forget the milk!</body></note>"
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


toLegibleString()

Returns the XmlElement as a legible string.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.toLegibleString();
<note date="20240612">
  <to>
    Adam
  </to>
  <from>
    Bob
  </from>
  <heading>
    Reminder
  </heading>
  <body>
    Don't forget the milk!
  </body>
</note>
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


toString()

Returns the XmlElement as a legible string.

1
2
3
4
5
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../note.xml");
XmlParser parser = new XmlParser();
XmlElement element = parser.parseDocument(xmlstring);
element.toString();
<note date="20240612"><to>Adam</to><from>Bob</from><heading>Reminder</heading><body>Don't forget the milk!</body></note>
1
2
3
4
5
6
<note date="20240612">
    <to>Adam</to>
    <from>Bob</from>
    <heading>Reminder</heading>
    <body>Don't forget the milk!</body>
</note>


Sample Use Case

Displaying XML in HTML panel

Steps to display the parsed XML inside a HTML textarea field.

The sample code below will display the parsed XML inside a HTML text area.

//Reads the XML file from your system
FileSystem fs = session.getFileSystem();
String xmlstring = fs.readFile("/path/to/file/.../books.xml");

//Parses the XML file into a XML element for manipulation
XmlParser parser = new XmlParser();
XmlElement xml = parser.parseDocument(xmlstring);

//Manipulate the XML element
stringbuilder s = new StringBuilder();
string s1 = xml.getInnerAsString();

for(int i =0; i < s1.length()-1; i++){
  s.append(s1.charAt(i));
  if(s1.charAt(i) == ">" || s1.charAt(i+1) == "<"){
    s.append("\n");
  }

}
Html1.setValue("textarea",s);