tailieunhanh - Programming Groovy dynamic productivity for the java developer phần 6

Bạn thậm chí có thể nhận được các giá trị tiếp giáp từ bộ sưu tập bằng cách sử dụng các đối tượng Range, như được hiển thị ở đây Đoạn mã trên trả về giá trị liền kề nhau trong danh sách bắt đầu từ phần tử ở vị trí thứ 2, như được hiển thị ở đây | Parsing xml 156 For the examples in the rest of this chapter you ll work with an xml document shown next with a list of languages and authors Download WorkingWithXML languages language name C author Stroustrup author language language name Java author Gosling author language language name Lisp author McCarthy author language language name Modula-2 author Wirth author language language name Oberon-2 author Wirth author language language name Pascal author Wirth author language languages Using DOMCategory Groovy categories allow you to define dynamic methods on classes. I ll discuss categories in detail in Section Injecting Methods Using Categories on page 203. Groovy provides a category for working with the DOM DOMCategory. Groovy simplifies the DOM API by adding convenience methods. DOMCategory allows you to navigate the DOM structure using GPath-like notation. You can access all child elements simply using the child name. For example instead of calling getElementsByTagName name use the property name to get it as in . That is given the root element languages you can obtain all the language elements by simply calling . The rootElement can be obtained using a DOM parser in the following example you ll use the DOMBuilder s parse method to get it. You can obtain the value for an attribute by placing an @ before the attribute name as in language.@name. In the following code you use DOMCategory to fetch language names and authors from the document Download WorkingWithXML document new FileReader rootElement use println Languages and authors languages language - println language. @name authored by 0 .text def languagesByAuthor authorName - 0 .textO authorName .collect it. @name .join println Languages by Wirth .

TỪ KHÓA LIÊN QUAN