XmlReader Class
- java.
lang. Object - com.
azure. xml. XmlReader
- com.
Implements
public final class XmlReader
implements AutoCloseable
Reads an XML encoded value as a stream of tokens.
Method Summary
Modifier and Type | Method and Description |
---|---|
T |
getNullableAttribute(String namespaceUri, String localName, XmlReadValueCallback<String,T> converter)
Gets the nullable value for the attribute in the XML element. |
T |
getNullableElement(XmlReadValueCallback<String,T> converter)
Gets the nullable value for the current element. |
T |
readObject(String localName, XmlReadValueCallback<XmlReader,T> converter)
Reads an object from the XML stream. |
T |
readObject(String namespaceUri, String localName, XmlReadValueCallback<XmlReader,T> converter)
Reads an object from the XML stream. |
void |
close()
Closes the XML stream. |
Xml |
currentToken()
Gets the XmlToken that the reader points to currently. |
static
Xml |
fromBytes(byte[] xml)
Creates an XMLStreamReader-based XmlReader that parses the passed |
static
Xml |
fromReader(Reader xml)
Creates an XmlReader that parses the passed |
static
Xml |
fromStream(InputStream xml)
Creates an XmlReader that parses the passed |
static
Xml |
fromString(String xml)
Creates an XmlReader that parses the passed |
static
Xml |
fromXmlStreamReader(XMLStreamReader reader)
Creates an XmlReader that parses the passed |
byte[] |
getBinaryAttribute(String namespaceUri, String localName)
Gets the binary value for the attribute in the XML element. |
byte[] |
getBinaryElement()
Gets the binary value for the current element. |
boolean |
getBooleanAttribute(String namespaceUri, String localName)
Gets the boolean value for the attribute in the XML element. |
boolean |
getBooleanElement()
Gets the boolean value for the current element. |
double |
getDoubleAttribute(String namespaceUri, String localName)
Gets the double value for the attribute in the XML element. |
double |
getDoubleElement()
Gets the double value for the current element. |
QName |
getElementName()
Gets the QName for the current XML element. |
float |
getFloatAttribute(String namespaceUri, String localName)
Gets the float value for the attribute in the XML element. |
float |
getFloatElement()
Gets the float value for the current element. |
int |
getIntAttribute(String namespaceUri, String localName)
Gets the int value for the attribute in the XML element. |
int |
getIntElement()
Gets the int value for the current element. |
long |
getLongAttribute(String namespaceUri, String localName)
Gets the long value for the attribute in the XML element. |
long |
getLongElement()
Gets the long value for the current element. |
String |
getStringAttribute(String namespaceUri, String localName)
Gets the string value for the attribute in the XML element. |
String |
getStringElement()
Gets the string value for the current element. |
Xml |
nextElement()
Iterates to and returns the next START_ELEMENT or END_ELEMENT in the XML stream. |
void |
skipElement()
Skips the current XML element. |
Methods inherited from java.lang.Object
Method Details
getNullableAttribute
public T
Gets the nullable value for the attribute in the XML element.
If the attribute doesn't have a value or doesn't exist null will be returned, otherwise the attribute getStringAttribute(String namespaceUri, String localName) is passed to the converter.
Code Samples
try (XmlReader reader = XmlReader.fromString("<root><element attribute=\"1234\"/></root>")) {
reader.nextElement(); // Progress to <root>
reader.nextElement(); // Progress to <element>
// Get the value of the attribute "attribute" as an Integer in a way that allows for the attribute to be
// missing or have a null value.
Objects.equals(1234, reader.getNullableAttribute(null, "attribute", Integer::parseInt));
// This attribute doesn't exist, so null is returned without causing a NumberFormatException (which is what
// Integer.parseInt throws on a null string being passed).
Objects.isNull(reader.getNullableAttribute(null, "nonExistentAttribute", Integer::parseInt));
} catch (XMLStreamException ex) {
// Do something with the exception
}
Parameters:
Returns:
Throws:
getNullableElement
public T
Gets the nullable value for the current element.
If the current element doesn't have a value null will be returned, otherwise the element getStringElement() is passed to the converter.
Code Samples
try (XmlReader reader = XmlReader.fromString("<root><element>1234</element><emptyElement/></root>")) {
reader.nextElement(); // Progress to <root>
reader.nextElement(); // Progress to <element>
// Get the value of the element "element" as an Integer in a way that allows for the element to be missing
// or have a null value.
Objects.equals(1234, reader.getNullableElement(Integer::parseInt)); // 1234
reader.nextElement(); // Progress to <emptyElement>
// This element doesn't exist, so null is returned without causing a NumberFormatException (which is what
// Integer.parseInt throws on a null string being passed).
Objects.isNull(reader.getNullableElement(Integer::parseInt));
} catch (XMLStreamException ex) {
// Do something with the exception
}
Parameters:
Returns:
Throws:
readObject
public T
Reads an object from the XML stream.
Validates that the XmlReader is currently pointing to an START_ELEMENT which has the qualifying name specified by the startTagName
.
Code Samples
return xmlReader.readObject(getRootElementName(rootElementName, "Name"), reader -> {
BlobName result = new BlobName();
result.encoded = reader.getNullableAttribute(null, "Encoded", Boolean::parseBoolean);
result.content = reader.getStringElement();
return result;
});
Parameters:
Returns:
Throws:
startTagName
readObject
public T
Reads an object from the XML stream.
Validates that the XmlReader is currently pointing to an START_ELEMENT which has the qualifying name specified by the startTagName
.
Code Samples
return xmlReader.readObject("http://schemas.microsoft.com/netservices/2010/10/servicebus/connect",
getRootElementName(rootElementName, "NamespaceInfo"), reader -> {
NamespaceProperties properties = new NamespaceProperties();
while (xmlReader.nextElement() != XmlToken.END_ELEMENT) {
QName qName = xmlReader.getElementName();
String localPart = qName.getLocalPart();
String namespaceUri = qName.getNamespaceURI();
if ("Alias".equals(localPart)
&& "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect".equals(namespaceUri)) {
properties.alias = xmlReader.getStringElement();
} else if ("CreatedTime".equals(localPart)
&& "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect".equals(namespaceUri)) {
properties.createdTime = OffsetDateTime.parse(xmlReader.getStringElement());
} else if ("MessagingSKU".equals(localPart)
&& "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect".equals(namespaceUri)) {
properties.messagingSku = MessagingSku.fromString(xmlReader.getStringElement());
} else if ("MessagingUnits".equals(localPart)
&& "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect".equals(namespaceUri)) {
properties.messagingUnits = xmlReader.getIntElement();
} else if ("ModifiedTime".equals(localPart)
&& "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect".equals(namespaceUri)) {
properties.modifiedTime = OffsetDateTime.parse(xmlReader.getStringElement());
} else if ("Name".equals(localPart)
&& "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect".equals(namespaceUri)) {
properties.name = xmlReader.getStringElement();
} else if ("NamespaceType".equals(localPart)
&& "http://schemas.microsoft.com/netservices/2010/10/servicebus/connect".equals(namespaceUri)) {
properties.namespaceType = NamespaceType.fromString(xmlReader.getStringElement());
}
}
return properties;
});
Parameters:
Returns:
Throws:
startTagName
close
public void close()
Closes the XML stream.
Throws:
currentToken
public XmlToken currentToken()
Gets the XmlToken that the reader points to currently.
Returns START_DOCUMENT if the reader hasn't begun reading the XML stream. Returns END_DOCUMENT if the reader has completed reading the XML stream.
Returns:
fromBytes
public static XmlReader fromBytes(byte[] xml)
Creates an XMLStreamReader-based XmlReader that parses the passed xml
.
This uses the XMLStreamReader implementation provided by the default XMLInputFactory#newInstance(). If you need to provide a custom implementation of XMLStreamReader use fromXmlStreamReader(XMLStreamReader reader).
Parameters:
Returns:
Throws:
xml
is null.
fromReader
public static XmlReader fromReader(Reader xml)
Creates an XmlReader that parses the passed xml
.
This uses the XMLStreamReader implementation provided by the default XMLInputFactory#newInstance(). If you need to provide a custom implementation of XMLStreamReader use fromXmlStreamReader(XMLStreamReader reader).
Parameters:
Returns:
Throws:
xml
is null.
fromStream
public static XmlReader fromStream(InputStream xml)
Creates an XmlReader that parses the passed xml
.
This uses the XMLStreamReader implementation provided by the default XMLInputFactory#newInstance(). If you need to provide a custom implementation of XMLStreamReader use fromXmlStreamReader(XMLStreamReader reader).
Parameters:
Returns:
Throws:
xml
is null.
fromString
public static XmlReader fromString(String xml)
Creates an XmlReader that parses the passed xml
.
This uses the XMLStreamReader implementation provided by the default XMLInputFactory#newInstance(). If you need to provide a custom implementation of XMLStreamReader use fromXmlStreamReader(XMLStreamReader reader).
Parameters:
Returns:
Throws:
xml
is null.
fromXmlStreamReader
getBinaryAttribute
public byte[] getBinaryAttribute(String namespaceUri, String localName)
Gets the binary value for the attribute in the XML element.
Parameters:
Returns:
getBinaryElement
public byte[] getBinaryElement()
Gets the binary value for the current element.
Returns:
Throws:
getBooleanAttribute
public boolean getBooleanAttribute(String namespaceUri, String localName)
Gets the boolean value for the attribute in the XML element.
Parameters:
Returns:
getBooleanElement
public boolean getBooleanElement()
Gets the boolean value for the current element.
Returns:
Throws:
getDoubleAttribute
public double getDoubleAttribute(String namespaceUri, String localName)
Gets the double value for the attribute in the XML element.
Parameters:
Returns:
getDoubleElement
public double getDoubleElement()
Gets the double value for the current element.
Returns:
Throws:
getElementName
public QName getElementName()
Gets the QName for the current XML element.
Code Samples
QName qName = xmlReader.getElementName();
String localPart = qName.getLocalPart(); // The name of the XML element.
String namespaceUri = qName.getNamespaceURI(); // The namespace of the XML element.
Returns:
getFloatAttribute
public float getFloatAttribute(String namespaceUri, String localName)
Gets the float value for the attribute in the XML element.
Parameters:
Returns:
getFloatElement
public float getFloatElement()
Gets the float value for the current element.
Returns:
Throws:
getIntAttribute
public int getIntAttribute(String namespaceUri, String localName)
Gets the int value for the attribute in the XML element.
Parameters:
Returns:
getIntElement
public int getIntElement()
Gets the int value for the current element.
Returns:
Throws:
getLongAttribute
public long getLongAttribute(String namespaceUri, String localName)
Gets the long value for the attribute in the XML element.
Parameters:
Returns:
getLongElement
public long getLongElement()
Gets the long value for the current element.
Returns:
Throws:
getStringAttribute
public String getStringAttribute(String namespaceUri, String localName)
Gets the string value for the attribute in the XML element.
Null is returned if the attribute doesn't exist in the XML element.
Parameters:
Returns:
getStringElement
public String getStringElement()
Gets the string value for the current element.
Returns:
Throws:
nextElement
public XmlToken nextElement()
Iterates to and returns the next START_ELEMENT or END_ELEMENT in the XML stream.
Returns END_DOCUMENT if iterating to the next element token completes reading of the XML stream.
Returns:
Throws:
skipElement
public void skipElement()
Skips the current XML element.
If the currentToken() isn't an START_ELEMENT this is a no-op.
This reads the XML stream until the matching END_ELEMENT is found for the current START_ELEMENT.
Throws:
Applies to
Azure SDK for Java