Apex Developer Guide
Release Notes
Reading XML Using Streams
Writing XML Using Streams
Reading and Writing XML Using the DOM
ZIP Support
Securing Your Data
Encoding Your Data
Apex Reference
Previous PDF Versions
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:
<book> has an attribute title: <book title="Salesforce.com for Dummies">.<book>.</book>.!ENTITY title = "My Book Title".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.
The following example processes an XML string.
1public with sharing class XmlStreamReaderDemo {
2
3 // Create a class Book for processing
4 public with sharing 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 with sharing class XmlStreamReaderDemoTest {
3 // Test that the XML string contains specific values
4 @IsTest
5 static void testBookParser() {
6
7 XmlStreamReaderDemo demo = new XmlStreamReaderDemo();
8
9 String str = '<books><book author="Chatty">Alpha beta</book>' +
10 '<book author="Sassy">Baz</book></books>';
11
12 XmlStreamReader reader = new XmlStreamReader(str);
13 XmlStreamReaderDemo.Book[] books = demo.parseBooks(reader);
14
15 System.debug(books.size());
16
17 for (XmlStreamReaderDemo.Book book : books) {
18 System.debug(book);
19 }
20 }
21}See Also