Class SimpleXml


  • public class SimpleXml
    extends java.lang.Object
    Utility class to load and save data from/to XML files using a very simple XML format.
    • Constructor Summary

      Constructors 
      Constructor Description
      SimpleXml​(java.io.File f)
      Create a new SimpleXml object by loading a file.
      SimpleXml​(java.io.InputStream resourceAsStream)
      Create a new SimpleXml object by loading a file.
      SimpleXml​(java.lang.String rootElement)
      Create a new SimpleXml just containing the root element.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(java.lang.String key, java.lang.String... values)
      Add entries to the current set of settings.
      java.util.List<java.lang.String> getList​(java.lang.String key)
      Get list of all items matching the key.
      java.lang.String getString​(java.lang.String key)
      Get the first entry that matches the key.
      StringTree<java.lang.String> getTree​(java.lang.String path)
      Return a tree structure reflecting the XML and trimmed values.
      boolean hasKey​(java.lang.String key)
      Checks if a setting with the specified key exists.
      void save​(java.io.File f)
      Save the current settings as an XML file.
      void update​(java.lang.String key, java.lang.String... values)
      Removes current settings for a key and adds new values for the same key.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SimpleXml

        public SimpleXml​(java.io.File f)
        Create a new SimpleXml object by loading a file.
        Parameters:
        f - XML file to load
      • SimpleXml

        public SimpleXml​(java.lang.String rootElement)
        Create a new SimpleXml just containing the root element.
        Parameters:
        rootElement - Name of the root element
      • SimpleXml

        public SimpleXml​(java.io.InputStream resourceAsStream)
        Create a new SimpleXml object by loading a file.
        Parameters:
        resourceAsStream - XML file to load
    • Method Detail

      • add

        public void add​(java.lang.String key,
                        java.lang.String... values)
        Add entries to the current set of settings. If a node with this key already exists in the XML, the new nodes are added after that, otherwise the new nodes are added at the end.
        Parameters:
        key - the key to add
        values - the values to add
        Throws:
        ArgumentNotValid - if the key is null or empty, or the value is null
      • update

        public void update​(java.lang.String key,
                           java.lang.String... values)
        Removes current settings for a key and adds new values for the same key. Calling update() is equivalent to calling delete() and add(), except the old value does not get destroyed on errors and order of the elements are kept. If no values are given, the key is removed.
        Parameters:
        key - The key for which the value should be updated.
        values - The new values that should be set for the key.
        Throws:
        UnknownID - if the key does not exist
        ArgumentNotValid - if the key is null or empty, or any of the values are null
      • getString

        public java.lang.String getString​(java.lang.String key)
        Get the first entry that matches the key. Keys are constructed as a dot separated path of xml tag names. Example: The following XML definition of a user name <dk><netarkivet><user>ssc</user> </netarkivet></dk> is accessed using the path: "dk.netarkivet.user"
        Parameters:
        key - the key of the entry.
        Returns:
        the first entry that matches the key.
        Throws:
        UnknownID - if no element matches the key
        ArgumentNotValid - if the key is null or empty
      • hasKey

        public boolean hasKey​(java.lang.String key)
        Checks if a setting with the specified key exists.
        Parameters:
        key - a key for a setting
        Returns:
        true if the key exists
        Throws:
        ArgumentNotValid - if key is null or empty
      • getList

        public java.util.List<java.lang.String> getList​(java.lang.String key)
        Get list of all items matching the key. If no items exist matching the key, an empty list is returned.
        Parameters:
        key - the path down to elements to get
        Returns:
        a list of items that match the supplied key
      • save

        public void save​(java.io.File f)
        Save the current settings as an XML file.
        Parameters:
        f - the file to write the XML to.
      • getTree

        public StringTree<java.lang.String> getTree​(java.lang.String path)
        Return a tree structure reflecting the XML and trimmed values.
        Parameters:
        path - Dotted path into the xml.
        Returns:
        A tree reflecting the xml at the given path.
        Throws:
        UnknownID - If the path does not exist in the tree or is ambiguous