Class XmlTools

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

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

    Constructors 
    Constructor Description
    XmlTools()  
  • Method Summary

    Modifier and Type Method Description
    static java.lang.String escapeAttributeValue​(java.lang.String attribute)  
    static java.lang.String escapeString​(java.lang.String str)  
    static java.util.List<org.w3c.dom.Element> getElementList​(org.w3c.dom.Element parent, java.lang.String tagName)
    Gets all elements with the given child tag from given parent XML element.
    static org.w3c.dom.Element getOptionalElement​(org.w3c.dom.Element parent, java.lang.String tagName)
    Gets one optional child tag from given parent XML element.
    static org.w3c.dom.Element getRootElement​(java.io.File xmlFile, java.lang.String rootElementName)
    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.
    static org.w3c.dom.Element getRootElement​(java.io.InputStream inputStream, java.lang.String rootElementName)
    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.
    static org.w3c.dom.Element getRootElement​(java.lang.String xml, java.lang.String rootElementName)
    Gets the root element from the given XML String and throws an exception if the name does not match with the given name.
    static org.w3c.dom.Element getRootElement​(org.w3c.dom.Document document, java.lang.String rootElementName)
    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.
    static org.w3c.dom.Element getSingularElement​(org.w3c.dom.Element parent, java.lang.String tagName)
    Gets exactly one child tag from given parent XML element.

    Methods inherited from class java.lang.Object

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

  • Method Details

    • getRootElement

      public static org.w3c.dom.Element getRootElement​(java.lang.String xml, java.lang.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 org.w3c.dom.Element getRootElement​(java.io.File xmlFile, java.lang.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 org.w3c.dom.Element getRootElement​(java.io.InputStream inputStream, java.lang.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 org.w3c.dom.Element getRootElement​(org.w3c.dom.Document document, java.lang.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 org.w3c.dom.Element getSingularElement​(org.w3c.dom.Element parent, java.lang.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 org.w3c.dom.Element getOptionalElement​(org.w3c.dom.Element parent, java.lang.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 java.util.List<org.w3c.dom.Element> getElementList​(org.w3c.dom.Element parent, java.lang.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 java.lang.String escapeString​(java.lang.String str)
    • escapeAttributeValue

      public static java.lang.String escapeAttributeValue​(java.lang.String attribute)