Class XmlReader
- All Implemented Interfaces:
AutoCloseable
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes the XML stream.Gets theXmlTokenthat the reader points to currently.booleanelementNameMatches(String localName) Checks if the current element name matches the provided local name.booleanelementNameMatches(String namespaceUri, String localName) Checks if the current element namespace URI and name matches the provided namespace URI and local name.static XmlReaderfromBytes(byte[] xml) static XmlReaderfromReader(Reader xml) Creates anXmlReaderthat parses the passedxml.static XmlReaderfromStream(InputStream xml) Creates anXmlReaderthat parses the passedxml.static XmlReaderfromString(String xml) Creates anXmlReaderthat parses the passedxml.static XmlReaderfromXmlStreamReader(XMLStreamReader reader) Creates anXmlReaderthat parses the passedxml.byte[]getBinaryAttribute(String namespaceUri, String localName) Gets the binary value for the attribute in the XML element.byte[]Gets the binary value for the current element.booleangetBooleanAttribute(String namespaceUri, String localName) Gets the boolean value for the attribute in the XML element.booleanGets the boolean value for the current element.doublegetDoubleAttribute(String namespaceUri, String localName) Gets the double value for the attribute in the XML element.doubleGets the double value for the current element.Gets the local name for the current XML element.Gets theQNamefor the current XML element.Gets the namespace URI for the current XML element.floatgetFloatAttribute(String namespaceUri, String localName) Gets the float value for the attribute in the XML element.floatGets the float value for the current element.intgetIntAttribute(String namespaceUri, String localName) Gets the int value for the attribute in the XML element.intGets the int value for the current element.longgetLongAttribute(String namespaceUri, String localName) Gets the long value for the attribute in the XML element.longGets the long value for the current element.<T> TgetNullableAttribute(String namespaceUri, String localName, XmlReadValueCallback<String, T> converter) Gets the nullable value for the attribute in the XML element.<T> TgetNullableElement(XmlReadValueCallback<String, T> converter) Gets the nullable value for the current element.getStringAttribute(String namespaceUri, String localName) Gets the string value for the attribute in the XML element.Gets the string value for the current element.Iterates to and returns the nextXmlToken.START_ELEMENTorXmlToken.END_ELEMENTin the XML stream.voidprocessNextElement(XmlElementConsumer callback) Processes the next element in the XML stream.<T> TreadObject(String localName, XmlReadValueCallback<XmlReader, T> converter) Reads an object from the XML stream.<T> TreadObject(String namespaceUri, String localName, XmlReadValueCallback<XmlReader, T> converter) Reads an object from the XML stream.voidSkips the current XML element.
-
Method Details
-
fromBytes
Creates anXMLStreamReader-basedXmlReaderthat parses the passedxml.This uses the
XMLStreamReaderimplementation provided by the defaultXMLInputFactory.newInstance(). If you need to provide a custom implementation ofXMLStreamReaderusefromXmlStreamReader(XMLStreamReader).- Parameters:
xml- The XML to parse.- Returns:
- A new
XmlReaderinstance. - Throws:
NullPointerException- Ifxmlis null.XMLStreamException- If anXmlReadercannot be instantiated.
-
fromString
Creates anXmlReaderthat parses the passedxml.This uses the
XMLStreamReaderimplementation provided by the defaultXMLInputFactory.newInstance(). If you need to provide a custom implementation ofXMLStreamReaderusefromXmlStreamReader(XMLStreamReader).- Parameters:
xml- The XML to parse.- Returns:
- A new
XmlReaderinstance. - Throws:
NullPointerException- Ifxmlis null.XMLStreamException- If anXmlReadercannot be instantiated.
-
fromStream
Creates anXmlReaderthat parses the passedxml.This uses the
XMLStreamReaderimplementation provided by the defaultXMLInputFactory.newInstance(). If you need to provide a custom implementation ofXMLStreamReaderusefromXmlStreamReader(XMLStreamReader).- Parameters:
xml- The XML to parse.- Returns:
- A new
XmlReaderinstance. - Throws:
NullPointerException- Ifxmlis null.XMLStreamException- If anXmlReadercannot be instantiated.
-
fromReader
Creates anXmlReaderthat parses the passedxml.This uses the
XMLStreamReaderimplementation provided by the defaultXMLInputFactory.newInstance(). If you need to provide a custom implementation ofXMLStreamReaderusefromXmlStreamReader(XMLStreamReader).- Parameters:
xml- The XML to parse.- Returns:
- A new
XmlReaderinstance. - Throws:
NullPointerException- Ifxmlis null.XMLStreamException- If anXmlReadercannot be instantiated.
-
fromXmlStreamReader
Creates anXmlReaderthat parses the passedxml.This uses the provided
XMLStreamReaderimplementation to parse the XML.- Parameters:
reader- TheXMLStreamReaderto parse the XML.- Returns:
- A new
XmlReaderinstance. - Throws:
NullPointerException- Ifreaderis null.
-
currentToken
Gets theXmlTokenthat the reader points to currently.Returns
XmlToken.START_DOCUMENTif the reader hasn't begun reading the XML stream. ReturnsXmlToken.END_DOCUMENTif the reader has completed reading the XML stream.- Returns:
- The
XmlTokenthat the reader points to currently.
-
nextElement
Iterates to and returns the nextXmlToken.START_ELEMENTorXmlToken.END_ELEMENTin the XML stream.Returns
XmlToken.END_DOCUMENTif iterating to the next element token completes reading of the XML stream.- Returns:
- The next
XmlToken.START_ELEMENTorXmlToken.END_ELEMENTin the XML stream, orXmlToken.END_DOCUMENTif reading completes. - Throws:
XMLStreamException- If the next element cannot be determined.
-
close
Closes the XML stream.- Specified by:
closein interfaceAutoCloseable- Throws:
XMLStreamException- If the underlying content store fails to close.
-
getElementName
Gets theQNamefor 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:
- The
QNamefor the current XML element. - Throws:
IllegalStateException- If thecurrentToken()isn'tXmlToken.START_ELEMENTorXmlToken.END_ELEMENT.
-
getElementNamespaceUri
Gets the namespace URI for the current XML element.If the current element doesn't have a namespace URI,
XMLConstants.NULL_NS_URIwill be returned.- Returns:
- The namespace URI for the current XML element.
-
getElementLocalName
Gets the local name for the current XML element.- Returns:
- The local name for the current XML element.
-
elementNameMatches
Checks if the current element name matches the provided local name.If the namespace of the current element also needs to be validated use
elementNameMatches(String, String).- Parameters:
localName- The local name to match.- Returns:
- Whether the current element name matches the provided local name.
-
elementNameMatches
Checks if the current element namespace URI and name matches the provided namespace URI and local name.Namespace URI validation replaces null values with
XMLConstants.NULL_NS_URI.- Parameters:
namespaceUri- The namespace URI to match.localName- The local name to match.- Returns:
- Whether the current element namespace URI and name matches the provided namespace URI and local name.
-
processNextElement
Processes the next element in the XML stream.The
callbackwill be invoked with the current element's namespace URI, where null is replaced withXMLConstants.NULL_NS_URI, and local name and thisXmlReader.- Parameters:
callback- The callback to process the next element.- Throws:
XMLStreamException- If the next element cannot be processed.
-
getStringAttribute
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:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.- Returns:
- The string value for the attribute in the XML element, or null if the attribute doesn't exist.
- Throws:
IllegalStateException- IfcurrentToken()isn'tXmlToken.START_ELEMENT.
-
getBinaryAttribute
Gets the binary value for the attribute in the XML element.- Parameters:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.- Returns:
- The binary value for the attribute in the XML element.
-
getBooleanAttribute
Gets the boolean value for the attribute in the XML element.- Parameters:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.- Returns:
- The boolean value for the attribute in the XML element.
-
getDoubleAttribute
Gets the double value for the attribute in the XML element.- Parameters:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.- Returns:
- The double value for the attribute in the XML element.
-
getFloatAttribute
Gets the float value for the attribute in the XML element.- Parameters:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.- Returns:
- The float value for the attribute in the XML element.
-
getIntAttribute
Gets the int value for the attribute in the XML element.- Parameters:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.- Returns:
- The int value for the attribute in the XML element.
-
getLongAttribute
Gets the long value for the attribute in the XML element.- Parameters:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.- Returns:
- The long value for the attribute in the XML element.
-
getNullableAttribute
public <T> T getNullableAttribute(String namespaceUri, String localName, XmlReadValueCallback<String, T> converter) throws XMLStreamExceptionGets 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, String)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 }- Type Parameters:
T- Type of the attribute.- Parameters:
namespaceUri- Attribute namespace, may be null.localName- Attribute local name.converter- Function that converts the attribute text value to the nullable type.- Returns:
- The converted text value, or null if the attribute didn't have a value.
- Throws:
XMLStreamException- If the nullable attribute cannot be read.
-
getStringElement
Gets the string value for the current element.- Returns:
- The string value for the current element.
- Throws:
XMLStreamException- If the String element cannot be read.
-
getBinaryElement
Gets the binary value for the current element.- Returns:
- The binary value for the current element.
- Throws:
XMLStreamException- If the binary element cannot be read.
-
getBooleanElement
Gets the boolean value for the current element.- Returns:
- The boolean value for the current element.
- Throws:
XMLStreamException- If the boolean element cannot be read.
-
getDoubleElement
Gets the double value for the current element.- Returns:
- The double value for the current element.
- Throws:
XMLStreamException- If the double element cannot be read.
-
getFloatElement
Gets the float value for the current element.- Returns:
- The float value for the current element.
- Throws:
XMLStreamException- If the float element cannot be read.
-
getIntElement
Gets the int value for the current element.- Returns:
- The int value for the current element.
- Throws:
XMLStreamException- If the int element cannot be read.
-
getLongElement
Gets the long value for the current element.- Returns:
- The long value for the current element.
- Throws:
XMLStreamException- If the long element cannot be read.
-
getNullableElement
Gets the nullable value for the current element.If the current element doesn't have a value null will be returned, otherwise the element
text valueis 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 }- Type Parameters:
T- Type of the element.- Parameters:
converter- Function that converts the element text value to the nullable type.- Returns:
- The converted text value, or null if the element didn't have a value.
- Throws:
XMLStreamException- If the nullable element cannot be read.
-
readObject
public <T> T readObject(String localName, XmlReadValueCallback<XmlReader, T> converter) throws XMLStreamExceptionReads an object from the XML stream.Validates that the
XmlReaderis currently pointing to anXmlToken.START_ELEMENTwhich has the qualifying name specified by thestartTagName.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; });- Type Parameters:
T- Type of the object.- Parameters:
localName- The expecting starting local name for the object.converter- The function that reads the object.- Returns:
- An instance of the expected object,
- Throws:
IllegalStateException- If the starting tag isn'tXmlToken.START_ELEMENTor the tag doesn't match the expectedstartTagNameXMLStreamException- If the object cannot be read.
-
readObject
public <T> T readObject(String namespaceUri, String localName, XmlReadValueCallback<XmlReader, T> converter) throws XMLStreamExceptionReads an object from the XML stream.Validates that the
XmlReaderis currently pointing to anXmlToken.START_ELEMENTwhich has the qualifying name specified by thestartTagName.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; });- Type Parameters:
T- Type of the object.- Parameters:
namespaceUri- The expecting namespace for the object.localName- The expecting starting local name for the object.converter- The function that reads the object.- Returns:
- An instance of the expected object,
- Throws:
IllegalStateException- If the starting tag isn'tXmlToken.START_ELEMENTor the tag doesn't match the expectedstartTagNameXMLStreamException- If the object cannot be read.
-
skipElement
Skips the current XML element.If the
currentToken()isn't anXmlToken.START_ELEMENTthis is a no-op.This reads the XML stream until the matching
XmlToken.END_ELEMENTis found for the currentXmlToken.START_ELEMENT.- Throws:
XMLStreamException- If skipping the element fails.
-