2016-06-16 2 views
0

У меня есть поток mule, который хранит xml в переменной сеанса. Я хочу, чтобы этот xml был вставлен в xml, который генерируется в groovy.Создание xml и слияние CDATA xml с использованием groovy

Моя переменная сеанса выглядит #[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> 

ответ

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) 
Смежные вопросы