Package org.biojava.nbio.core.util
Class XMLHelper
- java.lang.Object
-
- org.biojava.nbio.core.util.XMLHelper
-
public class XMLHelper extends java.lang.ObjectHelper methods to simplify boilerplate XML parsing code for org.w3c.dom XML objects- Author:
- Scooter
-
-
Constructor Summary
Constructors Constructor Description XMLHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static org.w3c.dom.ElementaddChildElement(org.w3c.dom.Element parentElement, java.lang.String elementName)Creates a new element called elementName and adds it to parentElementstatic org.w3c.dom.DocumentgetNewDocument()Create a new, empty org.w3c.dom.Documentstatic org.w3c.dom.DocumentinputStreamToDocument(java.io.InputStream inputStream)Creates an org.w3c.dom.Document from the content of the inputStreamstatic org.w3c.dom.DocumentloadXML(java.lang.String fileName)Given a path to an XML file, parses into an org.w3c.dom.Documentstatic voidoutputToStream(org.w3c.dom.Document document, java.io.OutputStream outputStream)Given an org.w3c.dom.Document, writes it to the given outputStreamstatic java.util.ArrayList<org.w3c.dom.Element>selectElements(org.w3c.dom.Element element, java.lang.String xpathExpression)Gets a list of elements matching xpathExpression.static org.w3c.dom.ElementselectParentElement(org.w3c.dom.Element element, java.lang.String parentName)Given an element, searches upwards through ancestor Elements till the first Element matching the requests parentName is found.static org.w3c.dom.ElementselectSingleElement(org.w3c.dom.Element element, java.lang.String xpathExpression)If xpathExpression is a plain string with no '/' characterr, this is interpreted as a child element name to search for.
-
-
-
Method Detail
-
addChildElement
public static org.w3c.dom.Element addChildElement(org.w3c.dom.Element parentElement, java.lang.String elementName)Creates a new element called elementName and adds it to parentElement- Parameters:
parentElement-elementName-- Returns:
- the new child element
-
getNewDocument
public static org.w3c.dom.Document getNewDocument() throws javax.xml.parsers.ParserConfigurationExceptionCreate a new, empty org.w3c.dom.Document- Returns:
- a new org.w3c.dom.Document
- Throws:
javax.xml.parsers.ParserConfigurationException
-
loadXML
public static org.w3c.dom.Document loadXML(java.lang.String fileName) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationExceptionGiven a path to an XML file, parses into an org.w3c.dom.Document- Parameters:
fileName- path to a readable XML file- Returns:
- Throws:
org.xml.sax.SAXExceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationException
-
inputStreamToDocument
public static org.w3c.dom.Document inputStreamToDocument(java.io.InputStream inputStream) throws org.xml.sax.SAXException, java.io.IOException, javax.xml.parsers.ParserConfigurationExceptionCreates an org.w3c.dom.Document from the content of the inputStream- Parameters:
inputStream-- Returns:
- a Document
- Throws:
org.xml.sax.SAXExceptionjava.io.IOExceptionjavax.xml.parsers.ParserConfigurationException
-
outputToStream
public static void outputToStream(org.w3c.dom.Document document, java.io.OutputStream outputStream) throws javax.xml.transform.TransformerExceptionGiven an org.w3c.dom.Document, writes it to the given outputStream- Parameters:
document-outputStream-- Throws:
javax.xml.transform.TransformerException
-
selectParentElement
public static org.w3c.dom.Element selectParentElement(org.w3c.dom.Element element, java.lang.String parentName)Given an element, searches upwards through ancestor Elements till the first Element matching the requests parentName is found.- Parameters:
element- The starting elementparentName- The tag name of the requested Element.- Returns:
- The found element, or null if no matching element is found,
-
selectSingleElement
public static org.w3c.dom.Element selectSingleElement(org.w3c.dom.Element element, java.lang.String xpathExpression) throws javax.xml.xpath.XPathExpressionExceptionIf 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:
javax.xml.xpath.XPathExpressionException
-
selectElements
public static java.util.ArrayList<org.w3c.dom.Element> selectElements(org.w3c.dom.Element element, java.lang.String xpathExpression) throws javax.xml.xpath.XPathExpressionExceptionGets 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:
javax.xml.xpath.XPathExpressionException
-
-