Class XMLUtils


  • public class XMLUtils
    extends Object
    Simple class to read information from small XML using DOM manipulation
    Author:
    Andrea Bollini
    • Method Detail

      • getElementList

        public static List<Element> getElementList​(Element dataRoot,
                                                   String name)
        Parameters:
        dataRoot - the starting node
        name - the name of the subelement to find
        Returns:
        the list of all DOM Element with the provided name direct child of the starting node
      • getElementAttribute

        public static String getElementAttribute​(Element dataRoot,
                                                 String name,
                                                 String attr)
        Parameters:
        dataRoot - the starting node
        name - the name of the sub element
        attr - the attribute to get
        Returns:
        the value of the attribute for the sub element with the specified name in the starting node
      • getElementValue

        public static String getElementValue​(Element dataRoot,
                                             String name)
        Parameters:
        dataRoot - the starting node
        name - the name of the sub element
        Returns:
        the text content of the sub element with the specified name in the starting node
      • getSingleElement

        public static Element getSingleElement​(Element dataRoot,
                                               String name)
        Return the first element child with the specified name
        Parameters:
        dataRoot - the starting node
        name - the name of sub element to look for
        Returns:
        the first child element or null if no present
      • getElementValueList

        public static List<String> getElementValueList​(Element rootElement,
                                                       String subElementName)
        Parameters:
        rootElement - the starting node
        subElementName - the name of the subelement to find
        Returns:
        a list of string including all the text contents of the sub element with the specified name. If there are not sub element with the supplied name the method will return null
      • getElementValueArrayList

        public static List<String[]> getElementValueArrayList​(Element rootElement,
                                                              String subElementName,
                                                              String... fieldsName)
        root/subElement[]/field1, field2, fieldN
        Parameters:
        rootElement - the starting node
        subElementName - the name of the sub element to work on
        fieldsName - the names of the sub-sub-elements from which get the text content
        Returns:
        a list of array strings. The length of the array is equals to the number of fields required. For any fields the first textual value found in the sub element is used, null if no value is present