ストリームを使用した XML の読み取り
XMLStreamReader クラスメソッドでは、XML データの転送と参照のみアクセスを可能にします。
これらのメソッドは HTTP コールアウトと併用して、XML データを解析したり、余分なイベントをスキップしたりします。深度が最大 50 ノードのネストされた XML コンテンツを解析できます。次の例は、新しい XmlStreamReader オブジェクトのインスタンス化の方法を示しています。
1String xmlString = '<books><book>My Book</book><book>Your Book</book></books>';
2XmlStreamReader xsr = new XmlStreamReader(xmlString);これらのメソッドは、次の XML イベント上で動作します。
- 属性イベントは、特定の要素のために指定されます。たとえば、要素 <book> には、属性 title:<book title="Salesforce.com for Dummies"> があります。
- 要素開始イベントは、要素用の開始タグです。例: <book>。
- 要素終了イベントは、要素用の終了タグです。例: </book>。
- ドキュメント開始イベントは、ドキュメント用の開始タグです。
- ドキュメント終了イベントは、ドキュメント用の終了タグです。
- エンティティ参照は、コード内のエンティティ参照です。例: !ENTITY title = "My Book Title"。
- 文字イベントは、テキスト文字です。
- コメントイベントは、XML ファイル内のコメントです。
XML データを繰り返し処理するには、next メソッドと hasNext メソッドを使用します。getNamespace メソッドなどの get メソッドを使用して XML 内のデータにアクセスします。
XML データを反復するときに、XML データの最後を追い越して読み込むことを避けるために、next をコールする前に hasNext を使用してストリームデータが利用可能であることを必ず確認します。
XmlStreamReader の例
次の例のように XML 文字列は処理されます。
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}