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. 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.
1swfobject.registerObject("clippy.codeblock-1", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17public class XmlStreamReaderDemo {
18
19 // Create a class Book for processing
20 public class Book {
21 String name;
22 String author;
23 }
24
25 public Book[] parseBooks(XmlStreamReader reader) {
26 Book[] books = new Book[0];
27 boolean isSafeToGetNextXmlElement = true;
28 while(isSafeToGetNextXmlElement) {
29 // Start at the beginning of the book and make sure that it is a book
30 if (reader.getEventType() == XmlTag.START_ELEMENT) {
31 if ('Book' == reader.getLocalName()) {
32 // Pass the book to the parseBook method (below)
33 Book book = parseBook(reader);
34 books.add(book);
35 }
36 }
37 // Always use hasNext() before calling next() to confirm
38 // that we have not reached the end of the stream
39 if (reader.hasNext()) {
40 reader.next();
41 } else {
42 isSafeToGetNextXmlElement = false;
43 break;
44 }
45 }
46 return books;
47 }
48
49 // Parse through the XML, determine the author and the characters
50 Book parseBook(XmlStreamReader reader) {
51 Book book = new Book();
52 book.author = reader.getAttributeValue(null, 'author');
53 boolean isSafeToGetNextXmlElement = true;
54 while(isSafeToGetNextXmlElement) {
55 if (reader.getEventType() == XmlTag.END_ELEMENT) {
56 break;
57 } else if (reader.getEventType() == XmlTag.CHARACTERS) {
58 book.name = reader.getText();
59 }
60 // Always use hasNext() before calling next() to confirm
61 // that we have not reached the end of the stream
62 if (reader.hasNext()) {
63 reader.next();
64 } else {
65 isSafeToGetNextXmlElement = false;
66 break;
67 }
68 }
69 return book;
70 }
71}1swfobject.registerObject("clippy.codeblock-2", "9");
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17@isTest
18private class XmlStreamReaderDemoTest {
19 // Test that the XML string contains specific values
20 static testMethod void testBookParser() {
21
22 XmlStreamReaderDemo demo = new XmlStreamReaderDemo();
23
24 String str = '<books><book author="Chatty">Foo bar</book>' +
25 '<book author="Sassy">Baz</book></books>';
26
27 XmlStreamReader reader = new XmlStreamReader(str);
28 XmlStreamReaderDemo.Book[] books = demo.parseBooks(reader);
29
30 System.debug(books.size());
31
32 for (XmlStreamReaderDemo.Book book : books) {
33 System.debug(book);
34 }
35 }
36}