Class XmlTools

java.lang.Object
i5.las2peer.serialization.XmlTools

public class XmlTools extends Object
Simple static class collecting useful methods for XML (de-)serialization.
  • Constructor Details

    • XmlTools

      public XmlTools()
  • Method Details

    • getRootElement

      public static Element getRootElement(String xml, String rootElementName) throws MalformedXMLException
      Gets the root element from the given XML String and throws an exception if the name does not match with the given name.
      Parameters:
      xml - The XML String that should be parsed.
      rootElementName - The tag name of the root element. CASE SENSITIVE
      Returns:
      Returns the root element with the given tag name.
      Throws:
      MalformedXMLException - If the root element does not have the given name or multiple root elements exist.
    • getRootElement

      public static Element getRootElement(File xmlFile, String rootElementName) throws MalformedXMLException
      Gets the root element from the given file containing only ONE XML representation and throws an exception if the name does not match with the given name.
      Parameters:
      xmlFile - The file containing one XML representation that should be parsed.
      rootElementName - The tag name of the root element. CASE SENSITIVE
      Returns:
      Returns the root element with the given tag name.
      Throws:
      MalformedXMLException - If the root element does not have the given name or multiple root elements exist.
    • getRootElement

      public static Element getRootElement(InputStream inputStream, String rootElementName) throws MalformedXMLException
      Gets the root element from the given input stream containing only ONE XML representation and throws an exception if the name does not match with the given name.
      Parameters:
      inputStream - The input stream containing one XML representation that should be parsed.
      rootElementName - The tag name of the root element. CASE SENSITIVE
      Returns:
      Returns the root element with the given tag name.
      Throws:
      MalformedXMLException - If the root element does not have the given name or multiple root elements exist.
    • getRootElement

      public static Element getRootElement(Document document, String rootElementName) throws MalformedXMLException
      Gets the root element from the given document type containing only ONE XML representation and throws an exception if the name does not match with the given name.
      Parameters:
      document - The document type containing one XML representation that should be parsed.
      rootElementName - The tag name of the root element. CASE SENSITIVE
      Returns:
      Returns the root element with the given tag name.
      Throws:
      MalformedXMLException - If the root element does not have the given name or multiple root elements exist.
    • getSingularElement

      public static Element getSingularElement(Element parent, String tagName) throws MalformedXMLException
      Gets exactly one child tag from given parent XML element. Otherwise throws an exception.
      Parameters:
      parent - The parent XML element.
      tagName - The tag name of the singular child element. CASE SENSITIVE
      Returns:
      Returns the child element with the given tag name.
      Throws:
      MalformedXMLException - If not exactly one child has the specified tag name or it's not a node itself.
    • getOptionalElement

      public static Element getOptionalElement(Element parent, String tagName) throws MalformedXMLException
      Gets one optional child tag from given parent XML element. If more than one child matches an exception is thrown.
      Parameters:
      parent - The parent XML element.
      tagName - The tag name of the optional child element. CASE SENSITIVE
      Returns:
      Returns the child element with the given tag name or null if no child matches the given tag name.
      Throws:
      MalformedXMLException - If more than one child has the specified tag name or it's not a node itself.
    • getElementList

      public static List<Element> getElementList(Element parent, String tagName) throws MalformedXMLException
      Gets all elements with the given child tag from given parent XML element.
      Parameters:
      parent - The parent XML element.
      tagName - The tag name of the child elements. CASE SENSITIVE
      Returns:
      Returns the child elements with the given tag name or null if no child matches the given tag name.
      Throws:
      MalformedXMLException - If it's not a node itself.
    • escapeString

      public static String escapeString(String str)
    • escapeAttributeValue

      public static String escapeAttributeValue(String attribute)