この文章は Salesforce 機械翻訳システムを使用して翻訳されました。詳細はこちらをご参照ください。
英語に切り替える

Newer Version Available

This content describes an older version of this product. View Latest

ストリームを使用した XML の読み取り

XMLStreamReader クラスメソッドでは、XML データの転送と参照のみアクセスを可能にしま���。
これらのメソッドは HTTP コールアウトと併用して、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 文字列は処理されます。

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}