Class XmlNode

java.lang.Object
yearreview.app.util.xml.XmlNode
All Implemented Interfaces:
Iterable<XmlNode>

public class XmlNode extends Object implements Iterable<XmlNode>
A Wrapper around a node that allows for easier access of child nodes.
Author:
ColdStone37
  • Field Details

    • node

      private final Node node
      The node that is wrapped by this object.
    • nameToNodeMap

      private final HashMap<String,Node> nameToNodeMap
      A Map used to convert the name of a child to its node directly.
  • Constructor Details

    • XmlNode

      public XmlNode(Node node)
      Constructs a ConfigNode from a Node.
      Parameters:
      node - the node to create the ConfigNode from
  • Method Details

    • getName

      public String getName()
      Gets the Name of the Node. (Wrapper for Node.getNodeName())
      Returns:
      name of the node
    • getChildByName

      public XmlNode getChildByName(String name)
      Gets a child of this node by name.
      Parameters:
      name - name of the node to get
      Returns:
      child with given name or null if such a node doesn't exists
    • getChildContent

      public String getChildContent(String name)
      Gets text content of a child node directly.
      Parameters:
      name - name of the node to get the text content from
      Returns:
      text content of the child node with the given name
    • hasChild

      public boolean hasChild(String name)
      Tests whether a child node exists.
      Parameters:
      name - name of the child node to test for
      Returns:
      true if the node exists, false otherwise
    • getTextContent

      public String getTextContent()
      Gets the text content of a node. (Wrapper for Node.getTextContent())
      Returns:
      text content of this node
    • getAttributeByName

      public String getAttributeByName(String name)
      Gets an attribute by its name.
      Parameters:
      name - name of the attribute to get
      Returns:
      Value of the attribute or null if the attribute doesn't exist
    • assertChildNodesExist

      public void assertChildNodesExist(String... children)
      Makes sure that this node has certain children. If one or more of them don't exist an error is thrown.
      Parameters:
      children - the children that must exist
    • assertAttributesExist

      public void assertAttributesExist(String... attributes)
      Makes sure that this node has certain attributes. If one or more of them don't exist an error is thrown.
      Parameters:
      attributes - the attributes that must exist
    • iterator

      public Iterator<XmlNode> iterator()
      Gets an Iterator for the children contained in the node. Internally a ArrayList of ConfigNodes is created and it's iterator gets returned.
      Specified by:
      iterator in interface Iterable<XmlNode>
      Returns:
      iterator over all children
    • parseXmlFile

      public static XmlNode parseXmlFile(File f) throws IOException, ParserConfigurationException, SAXException
      Parses a given XML-file and return the root-XmlNode.
      Parameters:
      f - file to parse
      Returns:
      root-node of the XML-file
      Throws:
      IOException - if the file cannot be loaded
      ParserConfigurationException - if the parsed was not configured correctly
      SAXException - If the file cannot be parsed