Class XMLHelper

java.lang.Object
org.biojava.nbio.core.util.XMLHelper

public class XMLHelper extends Object
Helper methods to simplify boilerplate XML parsing code for org.w3c.dom XML objects
Author:
Scooter
  • Constructor Details

    • XMLHelper

      public XMLHelper()
  • Method Details

    • addChildElement

      public static Element addChildElement(Element parentElement, String elementName)
      Creates a new element called elementName and adds it to parentElement
      Parameters:
      parentElement -
      elementName -
      Returns:
      the new child element
    • getNewDocument

      public static Document getNewDocument() throws ParserConfigurationException
      Create a new, empty org.w3c.dom.Document
      Returns:
      a new org.w3c.dom.Document
      Throws:
      ParserConfigurationException
    • loadXML

      public static Document loadXML(String fileName) throws SAXException, IOException, ParserConfigurationException
      Given a path to an XML file, parses into an org.w3c.dom.Document
      Parameters:
      fileName - path to a readable XML file
      Returns:
      Throws:
      SAXException
      IOException
      ParserConfigurationException
    • inputStreamToDocument

      public static Document inputStreamToDocument(InputStream inputStream) throws SAXException, IOException, ParserConfigurationException
      Creates an org.w3c.dom.Document from the content of the inputStream
      Parameters:
      inputStream -
      Returns:
      a Document
      Throws:
      SAXException
      IOException
      ParserConfigurationException
    • outputToStream

      public static void outputToStream(Document document, OutputStream outputStream) throws TransformerException
      Given an org.w3c.dom.Document, writes it to the given outputStream
      Parameters:
      document -
      outputStream -
      Throws:
      TransformerException
    • selectParentElement

      public static Element selectParentElement(Element element, String parentName)
      Given an element, searches upwards through ancestor Elements till the first Element matching the requests parentName is found.
      Parameters:
      element - The starting element
      parentName - The tag name of the requested Element.
      Returns:
      The found element, or null if no matching element is found,
    • selectSingleElement

      public static Element selectSingleElement(Element element, String xpathExpression) throws XPathExpressionException
      If xpathExpression is a plain string with no '/' characterr, this is interpreted as a child element name to search for. If xpathExpression is an XPath expression, this is evaluated and is assumed to identify a single element.
      Parameters:
      element -
      xpathExpression -
      Returns:
      A single element or null if no match or the 1st match if matches more than 1
      Throws:
      XPathExpressionException
    • selectElements

      public static ArrayList<Element> selectElements(Element element, String xpathExpression) throws XPathExpressionException
      Gets a list of elements matching xpathExpression. If xpathExpression lacks a '/' character, only immediate children o element are searched over.
      If xpathExpression contains an '/' character, a full XPath search is made
      Parameters:
      element -
      xpathExpression -
      Returns:
      A possibly empty but non-null ArrayList
      Throws:
      XPathExpressionException