Package io.goobi.viewer.controller
Class XmlTools
java.lang.Object
io.goobi.viewer.controller.XmlTools
XML utilities.
-
Method Summary
Modifier and TypeMethodDescriptioncheckXMLWellformed(String xml) static StringdetermineFileFormat(String xml, String encoding) determineFileFormat.static StringdetermineFileFormat(org.jdom2.Document doc) Determines the format of the given XML file by checking for namespaces.evaluate(String expr, Object parent, org.jdom2.filter.Filter filter, List<org.jdom2.Namespace> namespaces) XPath evaluation with a given return type filter.evaluateAttributeString(String expr, Object parent, List<org.jdom2.Namespace> namespaces) evaluateString(String expr, Object parent, List<org.jdom2.Namespace> namespaces) static List<org.jdom2.Element> evaluateToElements(String expr, org.jdom2.Element element, List<org.jdom2.Namespace> namespaces) Evaluates the given XPath expression to a list of elements.evaluateToFirstAttributeString(String expr, Object parent, List<org.jdom2.Namespace> namespaces) static Optional<org.jdom2.Element> evaluateToFirstElement(String expr, org.jdom2.Element element, List<org.jdom2.Namespace> namespaces) evaluateToFirstString(String expr, Object parent, List<org.jdom2.Namespace> namespaces) static org.jdom2.DocumentgetDocumentFromString(String string, String encoding) Create a JDOM document from an XML string.static org.jdom2.input.SAXBuilderstatic StringgetStringFromElement(Object element, String encoding) getStringFromElement.static org.jdom2.output.XMLOutputterCreates an XMLOutputter with default encoding and system linebreaks.static org.jdom2.DocumentreadXmlFile(String filePath) readXmlFile.static org.jdom2.DocumentreadXmlFile(URL url) Reads an XML document from the given URL and returns a JDOM2 document.static org.jdom2.DocumentreadXmlFile(Path path) readXmlFile.static FilewriteXmlFile(org.jdom2.Document doc, String filePath) writeXmlFile.
-
Method Details
-
getSAXBuilder
public static org.jdom2.input.SAXBuilder getSAXBuilder() -
readXmlFile
public static org.jdom2.Document readXmlFile(String filePath) throws IOException, org.jdom2.JDOMException readXmlFile.- Parameters:
filePath- absolute path to the XML file to read- Returns:
- the parsed XML document
- Throws:
FileNotFoundException- if any.IOException- if any.org.jdom2.JDOMException- if any.
-
readXmlFile
Reads an XML document from the given URL and returns a JDOM2 document. Works with XML files within JARs.- Parameters:
url- URL pointing to the XML resource to read- Returns:
- the parsed XML document
- Throws:
FileNotFoundException- if any.IOException- if any.org.jdom2.JDOMException- if any.
-
readXmlFile
readXmlFile.- Parameters:
path- path to the XML file to read- Returns:
- the parsed XML document
- Throws:
FileNotFoundException- if any.IOException- if any.org.jdom2.JDOMException- if any.
-
writeXmlFile
writeXmlFile.- Parameters:
doc- the JDOM2 document to serializefilePath- absolute path of the output file to write- Returns:
- the written file
- Throws:
FileNotFoundException- if any.IOException- if any.
-
getDocumentFromString
public static org.jdom2.Document getDocumentFromString(String string, String encoding) throws org.jdom2.JDOMException, IOException Create a JDOM document from an XML string.- Parameters:
string- the XML content as a stringencoding- character encoding to use when converting the string to bytes- Returns:
- the parsed XML document
- Throws:
org.jdom2.JDOMException- if any.IOException- if any.
-
getStringFromElement
getStringFromElement.- Parameters:
element- the Document or Element to serialize to a stringencoding- character encoding to apply to the XML output- Returns:
- the XML serialization of the given document or element as a string
-
evaluateToElements
public static List<org.jdom2.Element> evaluateToElements(String expr, org.jdom2.Element element, List<org.jdom2.Namespace> namespaces) Evaluates the given XPath expression to a list of elements.- Parameters:
expr- XPath expression to evaluate.element- the root element to evaluate the expression againstnamespaces- namespace bindings required by the XPath expression- Returns:
ArrayListor null
-
evaluateToFirstElement
-
evaluate
public static List<Object> evaluate(String expr, Object parent, org.jdom2.filter.Filter filter, List<org.jdom2.Namespace> namespaces) XPath evaluation with a given return type filter.- Parameters:
expr- XPath expression to evaluate.parent- If not null, the expression is evaluated relative to this element.filter- Return type filter.namespaces- namespace bindings required by the XPath expression- Returns:
- a list of XPath evaluation results matching the given filter type
-
evaluateAttributeString
-
evaluateString
-
evaluateToFirstAttributeString
-
evaluateToFirstString
-
determineFileFormat
public static String determineFileFormat(String xml, String encoding) throws org.jdom2.JDOMException, IOException determineFileFormat.- Parameters:
xml- the XML content as a string to inspectencoding- character encoding used to parse the XML string- Returns:
- the detected source document format (e.g. "METS", "LIDO"), or null if the format cannot be determined
- Throws:
org.jdom2.JDOMException- if any.IOException- if any.
-
determineFileFormat
Determines the format of the given XML file by checking for namespaces.- Parameters:
doc- the parsed JDOM2 document to inspect- Returns:
- the detected source document format (e.g. "METS", "LIDO"), or null if the format cannot be determined
-
checkXMLWellformed
public static List<XMLError> checkXMLWellformed(String xml) throws ParserConfigurationException, SAXException, IOException - Parameters:
xml- XML string to validate- Returns:
- List
- Throws:
ParserConfigurationExceptionSAXExceptionIOException
-
getXMLOutputter
public static org.jdom2.output.XMLOutputter getXMLOutputter()Creates an XMLOutputter with default encoding and system linebreaks.- Returns:
- a new XMLOutputter instance with standard format settings
-