public final class XMLUtils extends Object
Constructor and Description |
---|
XMLUtils() |
Modifier and Type | Method and Description |
---|---|
static String |
getValue(String xpath,
Document document)
The a "value" from an XML file using XPath.
|
static String |
getValue(String xpath,
File file)
The a "value" from an XML file using XPath.
|
static String |
getValue(String xpath,
File file,
String fileDataEncoding)
The a "value" from an XML file using XPath.
|
static Document |
parse(File file)
Parse the supplied XML file data to a
Document . |
static Document |
parse(File file,
String encoding)
Deprecated.
use
parse(File) |
static Document |
parse(InputStream stream)
Parse the supplied XML stream data to a
Document . |
static Document |
parse(Reader stream)
Parse the supplied XML stream data to a
Document . |
static void |
safeTransform(Source source,
Result out)
Transform the source to the output in a manner that is protected against XXE attacks.
|
public static void safeTransform(@NonNull Source source, @NonNull Result out) throws TransformerException, SAXException
disableXXEPrevention
to true
.source
- The XML input to transform. - This should be a StreamSource
or a
SAXSource
in order to be able to prevent XXE attacks.out
- The Result of transforming the source
.TransformerException
SAXException
@NonNull public static Document parse(@NonNull InputStream stream) throws SAXException, IOException
Document
.
This function does not close the stream.
stream
- The XML stream.Document
.SAXException
- Error parsing the XML stream data e.g. badly formed XML.IOException
- Error reading from the steam.@NonNull public static Document parse(@NonNull Reader stream) throws SAXException, IOException
Document
.
This function does not close the stream.
In most cases you should prefer parse(InputStream)
.
stream
- The XML stream.Document
.SAXException
- Error parsing the XML stream data e.g. badly formed XML.IOException
- Error reading from the steam.@NonNull public static Document parse(@NonNull File file) throws SAXException, IOException
Document
.file
- The file to parse.SAXException
- Error parsing the XML file data e.g. badly formed XML.IOException
- Error reading from the file.@Deprecated @NonNull public static Document parse(@NonNull File file, @NonNull String encoding) throws SAXException, IOException
parse(File)
SAXException
IOException
@NonNull public static String getValue(@NonNull String xpath, @NonNull File file) throws IOException, SAXException, XPathExpressionException
Uses the system encoding for reading the file.
xpath
- The XPath expression to select the value.file
- The file to read.String
is returned when the expression does not evaluate
to anything in the document.IOException
- Error reading from the file.SAXException
- Error parsing the XML file data e.g. badly formed XML.XPathExpressionException
- Invalid XPath expression.@NonNull public static String getValue(@NonNull String xpath, @NonNull File file, @NonNull String fileDataEncoding) throws IOException, SAXException, XPathExpressionException
xpath
- The XPath expression to select the value.file
- The file to read.fileDataEncoding
- The file data format.String
is returned when the expression does not evaluate
to anything in the document.IOException
- Error reading from the file.SAXException
- Error parsing the XML file data e.g. badly formed XML.XPathExpressionException
- Invalid XPath expression.public static String getValue(String xpath, Document document) throws XPathExpressionException
xpath
- The XPath expression to select the value.document
- The document from which the value is to be extracted.String
is returned when the expression does not evaluate
to anything in the document.XPathExpressionException
- Invalid XPath expression.Copyright © 2004–2021. All rights reserved.