Newer Version Available
Reading XML Using Streams
The XMLStreamReader class methods enable forward, read-only access to XML
data.
Those methods are used in conjunction with HTTP callouts to parse XML data or skip
unwanted events. You can parse nested XML content that’s up to 50 nodes deep. The following example shows how to
instantiate a new XmlStreamReader
object:
1String xmlString = '<books><book>My Book</book><book>Your Book</book></books>';
2XmlStreamReader xsr = new XmlStreamReader(xmlString);These methods work on the following XML events:
- An attribute event is specified for a particular element. For example, the element <book> has an attribute title: <book title="Salesforce.com for Dummies">.
- A start element event is the opening tag for an element, for example <book>.
- An end element event is the closing tag for an element, for example </book>.
- A start document event is the opening tag for a document.
- An end document event is the closing tag for a document.
- An entity reference is an entity reference in the code, for example !ENTITY title = "My Book Title".
- A characters event is a text character.
- A comment event is a comment in the XML file.
Use the next and hasNext methods to iterate over XML data. Access data in XML using get methods such as the getNamespace method.
When iterating over the XML data, always check that stream data is available using hasNext before calling next to avoid attempting to read past the end of the XML data.
XmlStreamReader Example
The following example processes an XML string.
1public class XmlStreamReaderDemo {
2
3 // Create a class Book for processing
4 public class Book {
5 String name;
6 String author;
7 }
8
9 public Book[] parseBooks(XmlStreamReader reader) {
10 Book[] books = new Book[0];
11 boolean isSafeToGetNextXmlElement = true;
12 while(isSafeToGetNextXmlElement) {
13 // Start at the beginning of the book and make sure that it is a book
14 if (reader.getEventType() == XmlTag.START_ELEMENT) {
15 if ('Book' == reader.getLocalName()) {
16 // Pass the book to the parseBook method (below)
17 Book book = parseBook(reader);
18 books.add(book);
19 }
20 }
21 // Always use hasNext() before calling next() to confirm
22 // that we have not reached the end of the stream
23 if (reader.hasNext()) {
24 reader.next();
25 } else {
26 isSafeToGetNextXmlElement = false;
27 break;
28 }
29 }
30 return books;
31 }
32
33 // Parse through the XML, determine the author and the characters
34 Book parseBook(XmlStreamReader reader) {
35 Book book = new Book();
36 book.author = reader.getAttributeValue(null, 'author');
37 boolean isSafeToGetNextXmlElement = true;
38 while(isSafeToGetNextXmlElement) {
39 if (reader.getEventType() == XmlTag.END_ELEMENT) {
40 break;
41 } else if (reader.getEventType() == XmlTag.CHARACTERS) {
42 book.name = reader.getText();
43 }
44 // Always use hasNext() before calling next() to confirm
45 // that we have not reached the end of the stream
46 if (reader.hasNext()) {
47 reader.next();
48 } else {
49 isSafeToGetNextXmlElement = false;
50 break;
51 }
52 }
53 return book;
54 }
55}1@isTest
2private class XmlStreamReaderDemoTest {
3 // Test that the XML string contains specific values
4 static testMethod void testBookParser() {
5
6 XmlStreamReaderDemo demo = new XmlStreamReaderDemo();
7
8 String str = '<books><book author="Chatty">Alpha beta</book>' +
9 '<book author="Sassy">Baz</book></books>';
10
11 XmlStreamReader reader = new XmlStreamReader(str);
12 XmlStreamReaderDemo.Book[] books = demo.parseBooks(reader);
13
14 System.debug(books.size());
15
16 for (XmlStreamReaderDemo.Book book : books) {
17 System.debug(book);
18 }
19 }
20}