Class XMLUtils

java.lang.Object
com.tridion.util.xml.XMLUtils

public class XMLUtils extends Object
Various utility methods for manipulating XML.
  • Method Details

    • getElement

      public static Element getElement(Document doc, String tagName, int index)
      Return an Element given an XML document, tag name, and index.
      Parameters:
      doc - XML docuemnt
      tagName - a tag name
      index - a index
      Returns:
      an Element, or null in case the element could not be found.
    • getSize

      public static int getSize(Document doc, String tagName)
      Return the number of person in an XML document.
      Parameters:
      doc - XML document
      tagName - a tag name
      Returns:
      the number of person in an XML document
    • getValue

      public static String getValue(Element e, String tagName)
      Given a person element, must get the element specified by the tagName, then must traverse that Node to get the value. Step1) get Element of name tagName from e Step2) cast element to Node and then traverse it for its non-whitespace, cr/lf value. Step3) return it!

      NOTE: Element is a subclass of Node

      Parameters:
      e - an Element
      tagName - a tag name
      Returns:
      s the value of a Node
    • getFirstElement

      public static Element getFirstElement(Element parent, String tagName)
    • getAttributeValue

      public static String getAttributeValue(Element parent, String tagName, String attributeName)
      Parameters:
      parent - an Element
      tagName - a tag name
      Returns:
      the value of an attribute
    • getAttributeValue

      public static String getAttributeValue(Document parent, String tagName, String attributeName)
      Parameters:
      parent - an Document
      tagName - a tag name
      Returns:
      the value of an attribute
    • printNodeTypes

      public static void printNodeTypes(NodeList rows)
      For testing purpose, it print out Node list.
      Parameters:
      rows - a Nodelist
    • convertDocumentToString

      public static String convertDocumentToString(Document document)
      Converts the passed Document to a String.
      Parameters:
      document - The Document to convert.
      Returns:
      An XML string representation of the Document.
    • convertContentToDocument

      public static List<Object> convertContentToDocument(byte[] content) throws ParserConfigurationException, SAXException, IOException
      Convert a byte[] array to a List of DOM objects. The converter ignores the root node.
      Parameters:
      content - All the XML nodes that will be converted to DOM.
      Returns:
      List of DOM.
      Throws:
      ParserConfigurationException
      SAXException
      IOException
    • removeBOMForUTF8

      public static String removeBOMForUTF8(String content)
      Removes BOM characters for UTF-8 files.
      Parameters:
      content - Content of the file that might contain BOM.
      Returns:
      Content without BOM.
    • escape

      public static Object escape(Object content)
      Removes XML characters ('&', ''', '"', '<' and '>') from passed content.
      Parameters:
      content - The content before escaping.
      Returns:
      Content after escaping xml characters
    • loadXML

      public static Element loadXML(InputStream xmlStream)
      Passed xml string will be loaded if passed string is well formed xml.
      Parameters:
      xmlStream - xml as a stream
      Returns:
      Document object if passed xml is well formed otherwise null
    • removeNonAlphaNumericChars

      public static String removeNonAlphaNumericChars(String originalString)
      Removes everything except letters and numbers from the provided String.
      Parameters:
      originalString - the original string.
      Returns:
      the processed string.