XmlStreamReader Class
The XmlStreamReader class provides methods for forward, read-only access to XML data. You can pull data from XML or skip unwanted events. You can parse nested XML content that’s up to 50 nodes deep.
The XmlStreamReader class is similar to the XMLStreamReader utility class from StAX (Streaming API for XML). StAX is an API to read and write XML documents, originating from the Java programming language community.
The XmlStreamReader class in Apex is based on its counterpart in Java. See Java XMLStreamReader class.
See Also
The following are constructors for XmlStreamReader.
-
Creates a new instance of the
XmlStreamReaderclass for the specified XML input.
Creates a new instance of the XmlStreamReader class for the specified XML input.
public XmlStreamReader(String xmlInput)
-
xmlInput:
Type: String
The XML string input.
The following are methods for XmlStreamReader. All are instance methods.
-
Returns the number of attributes on the start element, excluding namespace definitions.
-
Returns the local name of the attribute at the specified index.
-
Returns the namespace URI of the attribute at the specified index.
-
Returns the prefix of this attribute at the specified index.
-
Returns the XML type of the attribute at the specified index.
-
getAttributeValue(namespaceUri, localName)
Returns the value of the attribute in the specified localName at the specified URI.
-
Returns the value of the attribute at the specified index.
-
Returns the type of XML event the cursor is pointing to.
-
Returns the local name of the current event.
-
Return the current location of the cursor.
-
If the current event is a start element or end element, this method returns the URI of the prefix or the default namespace.
-
Returns the number of namespaces declared on a start element or end element.
-
Returns the prefix for the namespace declared at the index.
-
Return the URI for the given prefix.
-
Returns the URI for the namespace declared at the index.
-
Returns the data section of a processing instruction.
-
Returns the target section of a processing instruction.
-
Returns the prefix of the current XML event or
nullif the event does not have a prefix. -
Returns the current value of the XML event as a string.
-
Returns the XML version specified on the XML declaration. Returns
nullif none was declared. -
Returns
trueif the current XML event has a name. Returnsfalseotherwise. -
Returns
trueif there are more XML events andfalseif there are no more XML events. -
Returns
trueif the current event has text,falseotherwise. -
Returns
trueif the cursor points to a character data XML event. Otherwise, returnsfalse. -
Returns
trueif the cursor points to an end tag. Otherwise, it returnsfalse. -
Returns
trueif the cursor points to a start tag. Otherwise, it returnsfalse. -
Returns
trueif the cursor points to a character data XML event that consists of all white space. Otherwise it returnsfalse. -
Reads the next XML event. A processor may return all contiguous character data in a single chunk, or it may split it into several chunks. Returns an integer which indicates the type of event.
-
Skips any white space (the
isWhiteSpacemethod returnstrue), comment, or processing instruction XML events, until a start element or end element is reached. Returns the index for that XML event. -
setCoalescing(returnAsSingleBlock)
If you specify
truefor returnAsSingleBlock, text is returned in a single block, from a start element to the first end element or the next start element, whichever comes first. If you specify it asfalse, the parser may return text in multiple blocks. -
setNamespaceAware(isNamespaceAware)
If you specify
truefor isNamespaceAware, the parser recognizes namespace. If you specify it asfalse, the parser does not. The default value istrue. -
Returns a string containing the length of the input XML given to
XmlStreamReaderand the first 50 characters of the input XML.
Returns the number of attributes on the start element, excluding namespace definitions.
public Integer getAttributeCount()
Type: Integer
This method is only valid on a start element or attribute XML events. The count for the number of attributes for an attribute XML event starts with zero.
Returns the local name of the attribute at the specified index.
public String getAttributeLocalName(Integer index)
-
index:
Type: Integer
Type: String
If there is no name, an empty string is returned. This method is only valid with start element or attribute XML events.
Returns the namespace URI of the attribute at the specified index.
public String getAttributeNamespace(Integer index)
-
index:
Type: Integer
Type: String
If no namespace is specified, null is returned. This method is only valid with start element or attribute XML events.
Returns the prefix of this attribute at the specified index.
public String getAttributePrefix(Integer index)
-
index:
Type: Integer
Type: String
If no prefix is specified, null is returned. This method is only valid with start element or attribute XML events.
Returns the XML type of the attribute at the specified index.
public String getAttributeType(Integer index)
-
index:
Type: Integer
Type: String
For example, id is an attribute type. This method is only valid with start element or attribute XML events.
Returns the value of the attribute in the specified localName at the specified URI.
public String getAttributeValue(String namespaceUri, String localName)
Type: String
Returns null if the value is not found. You must specify a value for localName. This method is only valid with start element or attribute XML events.
Returns the value of the attribute at the specified index.
public String getAttributeValueAt(Integer index)
-
index:
Type: Integer
Type: String
This method is only valid with start element or attribute XML events.
Returns the type of XML event the cursor is pointing to.
public System.XmlTag getEventType()
Type: System.XmlTag
The values for XmlTag are:
-
ATTRIBUTE -
CDATA -
CHARACTERS -
COMMENT -
DTD -
END_DOCUMENT -
END_ELEMENT -
ENTITY_DECLARATION -
ENTITY_REFERENCE -
NAMESPACE -
NOTATION_DECLARATION -
PROCESSING_INSTRUCTION -
SPACE -
START_DOCUMENT -
START_ELEMENT
Returns the local name of the current event.
public String getLocalName()
Type: String
For start element or end element XML events, it returns the local name of the current element. For the entity reference XML event, it returns the entity name. The current XML event must be start element, end element, or entity reference.
Return the current location of the cursor.
public String getLocation()
Type: String
If the location is unknown, returns -1. The location information is only valid until the next method is called.
If the current event is a start element or end element, this method returns the URI of the prefix or the default namespace.
public String getNamespace()
Type: String
Returns null if the XML event does not have a prefix.
Returns the number of namespaces declared on a start element or end element.
public Integer getNamespaceCount()
Type: Integer
This method is only valid on a start element, end element, or namespace XML event.
Returns the prefix for the namespace declared at the index.
public String getNamespacePrefix(Integer index)
-
index:
Type: Integer
Type: String
Returns null if this is the default namespace declaration. This method is only valid on a start element, end element, or namespace XML event.
Return the URI for the given prefix.
public String getNamespaceURI(String prefix)
-
prefix:
Type: String
Type: String
The returned URI depends on the current state of the processor.
Returns the URI for the namespace declared at the index.
public String getNamespaceURIAt(Integer index)
-
index:
Type: Integer
Type: String
This method is only valid on a start element, end element, or namespace XML event.
Returns the data section of a processing instruction.
public String getPIData()
Type: String
Returns the target section of a processing instruction.
public String getPITarget()
Type: String
Returns the prefix of the current XML event or null if the event does not have a prefix.
public String getPrefix()
Type: String
Returns the current value of the XML event as a string.
public String getText()
Type: String
The valid values for the different events are:
-
The string value of a character XML event
-
The string value of a comment
-
The replacement value for an entity reference. For example, assume
getTextreads the following XML snippet:The
getTextmethod returnsSalesforce for Dummies, not&Title. -
The string value of a CDATA section
-
The string value for a space XML event
-
The string value of the internal subset of the DTD
Returns the XML version specified on the XML declaration. Returns null if none was declared.
public String getVersion()
Type: String
Returns true if the current XML event has a name. Returns false otherwise.
public Boolean hasName()
Type: Boolean
This method is only valid for start element and stop element XML events.
Returns true if there are more XML events and false if there are no more XML events.
public Boolean hasNext()
Type: Boolean
This method returns false if the current XML event is end document.
Returns true if the current event has text, false otherwise.
public Boolean hasText()
Type: Boolean
The following XML events have text: characters, entity reference, comment and space.
Returns true if the cursor points to a character data XML event. Otherwise, returns false.
public Boolean isCharacters()
Type: Boolean
Returns true if the cursor points to an end tag. Otherwise, it returns false.
public Boolean isEndElement()
Type: Boolean
Returns true if the cursor points to a start tag. Otherwise, it returns false.
public Boolean isStartElement()
Type: Boolean
Returns true if the cursor points to a character data XML event that consists of all white space. Otherwise it returns false.
public Boolean isWhiteSpace()
Type: Boolean
Reads the next XML event. A processor may return all contiguous character data in a single chunk, or it may split it into several chunks. Returns an integer which indicates the type of event.
public Integer next()
Type: Integer
Skips any white space (the isWhiteSpace method returns true), comment, or processing instruction XML events, until a start element or end element is reached. Returns the index for that XML event.
public Integer nextTag()
Type: Integer
This method throws an error if elements other than white space, comments, processing instruction, start elements or stop elements are encountered.
If you specify true for returnAsSingleBlock, text is returned in a single block, from a start element to the first end element or the next start element, whichever comes first. If you specify it as false, the parser may return text in multiple blocks.
public Void setCoalescing(Boolean returnAsSingleBlock)
-
returnAsSingleBlock:
Type: Boolean
Type: Void
If you specify true for isNamespaceAware, the parser recognizes namespace. If you specify it as false, the parser does not. The default value is true.
public Void setNamespaceAware(Boolean isNamespaceAware)
-
isNamespaceAware:
Type: Boolean
Type: Void
Returns a string containing the length of the input XML given to XmlStreamReader and the first 50 characters of the input XML.
public String toString()
Type: String