Package org.biojava.nbio.core.util
Class XMLHelper
- java.lang.Object
-
- org.biojava.nbio.core.util.XMLHelper
-
-
Constructor Summary
Constructors Constructor Description XMLHelper()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static Element
addChildElement(Element parentElement, String elementName)
Creates a new element called elementName and adds it to parentElementstatic Document
getNewDocument()
Create a new, empty org.w3c.dom.Documentstatic Document
inputStreamToDocument(InputStream inputStream)
Creates an org.w3c.dom.Document from the content of the inputStreamstatic Document
loadXML(String fileName)
Given a path to an XML file, parses into an org.w3c.dom.Documentstatic void
outputToStream(Document document, OutputStream outputStream)
Given an org.w3c.dom.Document, writes it to the given outputStreamstatic ArrayList<Element>
selectElements(Element element, String xpathExpression)
Gets a list of elements matching xpathExpression.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.static Element
selectSingleElement(Element element, String xpathExpression)
If xpathExpression is a plain string with no '/' characterr, this is interpreted as a child element name to search for.
-
-
-
Constructor Detail
-
XMLHelper
public XMLHelper()
-
-
Method Detail
-
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 elementparentName
- 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
-
-