Class XmlUtils


  • public class XmlUtils
    extends Object
    This class offers utility methods for handling XML files.
    • Constructor Detail

      • XmlUtils

        public XmlUtils()
        default constructor will assign no namespaces and use a default file handler.
      • XmlUtils

        public XmlUtils​(boolean namespaceAware)
        default constructor will assign no namespaces and use a default file handler.
        Parameters:
        namespaceAware - true if XmlUtils should be namespace aware.
      • XmlUtils

        public XmlUtils​(FileHandler fileHandler)
        constructor will assign no namespaces.
        Parameters:
        fileHandler - used to read and write xml files.
      • XmlUtils

        public XmlUtils​(FileHandler fileHandler,
                        boolean namespaceAware)
        constructor will assign no namespaces.
        Parameters:
        fileHandler - used to read and write xml files.
        namespaceAware - true if XmlUtils should be namespace aware.
    • Method Detail

      • selectElementsMatchingXPath

        public List<Element> selectElementsMatchingXPath​(String xpath,
                                                         Element toSearch)
        The following will search the given element for the specified XPath and return a list of nodes that match.
        Parameters:
        xpath - - selection criteria
        toSearch - - element to start the search at
        Returns:
        List of matching elements
      • selectElementMatchingXPath

        public Element selectElementMatchingXPath​(String xpath,
                                                  Element toSearch)
        The following will search the given element for the specified XPath and return any node that matches.
        Parameters:
        xpath - - selection criteria
        toSearch - - element to start the search at
        Returns:
        a matching element
      • loadXmlFromFile

        public Document loadXmlFromFile​(String sourceFile)
        read the specified file into a Document.
        Parameters:
        sourceFile - file to read
        Returns:
        Document corresponding with sourceFile
      • saveXml

        public void saveXml​(Document document,
                            String filename)
        write the xml document to disk, rethrowing checked exceptions as runtime.
        Parameters:
        document - document to write to disk
        filename - where to write the document
      • getFileHandler

        public FileHandler getFileHandler()
        Returns:
        the Cargo file utility class
      • setFileHandler

        public void setFileHandler​(FileHandler fileHandler)
        Parameters:
        fileHandler - the Cargo file utility class to use. This method is useful for unit testing with Mock objects as it can be passed a test file handler that doesn't perform any real file action.
      • setNamespaces

        public void setNamespaces​(Map<String,​String> namespaces)
        Parameters:
        namespaces - the namespaces to set
      • getNamespaces

        public Map<String,​String> getNamespaces()
        Returns:
        the namespaces
      • parseIntoElement

        public Element parseIntoElement​(String elementToParse)
        parse the passed string into an Element object.
        Parameters:
        elementToParse - string to parse
        Returns:
        result of parsing
      • createDocument

        public Document createDocument()
        Creates a new, blank XML document.
        Returns:
        New, blank XML document.
      • toString

        public String toString​(Element node)
        Output an XML node as string, without the XML header.
        Parameters:
        node - Node to output.
        Returns:
        String representation of node.