0

我有 mule 流,它将 xml 存储在会话变量中。我希望将此 xml 插入在 groovy 中生成的 xml 中。

我的会话变量看起来像#[sessionVars.shippingdetails]

这个会话变量有<a><Subject1>science</Subject1><Subject2>Maths</Subject2></a>

当我在我的 xmlmarkup 构建器中使用会话变量时,如下所示。我收到错误groovy.lang.GroovyRuntimeException: Namespace prefix: CDATA is not bound to a URI (javax.script.ScriptException). Message payload is of type: CaseInsensitiveHashMap (org.mule.api.transformer.TransformerMessagingException). Message payload is of type: CaseInsensitiveHashMap

import groovy.xml.XmlUtil
def builder = new groovy.xml.StreamingMarkupBuilder()
builder.encoding = "UTF-8"

// MAPPING
def person = {
  // use mkp object which provides features like setting the namespace
  mkp.xmlDeclaration()
  mkp.declareNamespace("":"urn:abc:alos:BaseComponents")

  //start with the XML root node closure
  ItemRequest {
    Requester{
        subject(message.payload.subject)
    }
  Item{
    Title(message.payload.name)
    Description(message.payload.desc)
    Category {
        CategoryID(message.payload.cat_id)
    }
    ConditionID (message.payload.condition)
    Mark (message.payload.Mark)

 ShippingDetails [CDATA[sessionVars.shippingdetails]]

    }
  }
}

// WRITING
def writer = new StringWriter()
writer << builder.bind(person)
println writer.toString()

XmlUtil.serialize(builder.bind(person))

因此我的输出 xml 应该如下所示。

<?xml version="1.0" encoding="UTF-8"?>
<ItemRequest xmlns="urn:abc:alos:BaseComponents">
    <Requester>
        <subject>something</subject>
    </Requester>
    <Item>
        <Title>Cooler Can Blue</Title>
        <Description>This is the place for description.</Description>
        <Category>
            <CategoryID>562</CategoryID>
        </Category>
        <ConditionID>25</ConditionID>
        <Mark>3</Mark>
        <ShippingDetails>
            <a>
                <Subject1>science</Subject1>
                <Subject2>Maths</Subject2>
            </a>
        </ShippingDetails>
    </Item>
</ItemRequest>
4

1 回答 1

0

使用 Groovy 2.4.3,这是获取指定输出的一种方法。(这不涉及骡子):

import groovy.xml.*

def markupBuilder = new StreamingMarkupBuilder()

def xml = markupBuilder.bind { builder ->
    mkp.declareNamespace( "": "urn:abc:alos:BaseComponents" )
    ItemRequest {
        Requester {
            subject("something")
        }
        Item {
            Title("Cooler Can Blue")
            Description("This is the place for description.")
            Category {
                CategoryID("562")
            } 
            ConditionID("25")
            Mark("3")
            ShippingDetails {
                a {
                    Subject1("science")
                    Subject2("Maths")
                }
            }
        }
    }
}
def goal = """<?xml version="1.0" encoding="UTF-8"?><ItemRequest xmlns="urn:abc:alos:BaseComponents">
  <Requester>
    <subject>something</subject>
  </Requester>
  <Item>
    <Title>Cooler Can Blue</Title>
    <Description>This is the place for description.</Description>
    <Category>
      <CategoryID>562</CategoryID>
    </Category>
    <ConditionID>25</ConditionID>
    <Mark>3</Mark>
    <ShippingDetails>
      <a>
        <Subject1>science</Subject1>
        <Subject2>Maths</Subject2>
      </a>
    </ShippingDetails>
  </Item>
</ItemRequest>
"""

assert goal == XmlUtil.serialize(xml)
于 2016-06-16T23:57:13.853 回答