此内容没有您所选择的语言版本。

7.7. Mixed-DOM-and-SAX Example


Procedure 7.1. Task

  1. Take an XML message such as the following sample:
    <shopping>
        <category type="groceries">
            <item>Chocolate</item>
            <item>Coffee</item>
        </category>
        <category type="supplies">
            <item>Paper</item>
            <item quantity="4">Pens</item>
        </category>
            <category type="present">
            <item when="Aug 10">Kathryn's Birthday</item>
        </category>
    </shopping>
    
  2. You can modify the "supplies" category in the above shopping list by adding two more "pens". To do this, write a simple Groovy script and aim it at the message's <category> elements.
  3. As a result, the script simply iterates over the <item> elements in the category, and in instances where the category type is "supplies" and the item is "pens", the quantity is incremented by two:
    <?xml version="1.0"?>
    <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" 
       xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd"
       xmlns:g="http://www.milyn.org/xsd/smooks/groovy-1.1.xsd">
    
       <core:filterSettings type="SAX" />
        
       <g:groovy executeOnElement="category">
          <g:script>
          <!--
             use(DOMCategory) {
              // Modify "supplies": we need an extra 2 pens...
              if (category.'@type' == 'supplies') {
                  category.item.each { item ->
                      if (item.text() == 'Pens') {
                          item['@quantity'] = item.'@quantity'.toInteger() + 2;
                      }
                  }
              }
          }
            
          // When using the SAX filter, we need to explicitly write the fragment
          // to the result stream...
          writeFragment(category);
          -->
          </g:script>
       </g:groovy>
    
    </smooks-resource-list>
    
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.