I have mule flow which stores the xml in session variable. I want this xml to be inserted in the xml which is generated in groovy.
My session variable looks like #[sessionVars.shippingdetails]
This session variable has <a><Subject1>science</Subject1><Subject2>Maths</Subject2></a>
When i use session variable in my xmlmarkup builder as like below. I am getting error as 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))
Hence my output xml should like below.
<?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>
Using Groovy 2.4.3, here is one way to get the output specified. (This does not address Mule):
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)
Related
I'm trying to figure out how to add attributes to an array element
I get an error Error: Invalid character in name when trying to build the following XML from an object.
<?xml version="1.0" encoding="UTF-8"?>
<Requests xmlns="http://example.com">
<Request>
</Request>
<Request>
</Request>
</Requests>
Here is my object
let myObject = {
Requests:[
{$:{xmlns:"http://example.com"}},
{Request:{}},
{Request:{}}
]
}
let builder = new xml2js.Builder();
let xml = builder.buildObject(myObject);
console.log(xml)
I've also tried wrapping the name in quotes
{"$":{"xmlns":"http://example.com"}},
Stripping out the attribute declaration altogether produces the desired XML but obviously leaves out the needed attribute
<?xml version="1.0" encoding="UTF-8"?>
<Requests>
<Request>
</Request>
<Request>
</Request>
</Requests>
I suppose you should try this:
let myObject = {
Requests:{
$:{xmlns:"http://example.com"},
Request:[{}, {}]
}
I want to modify the incoming message (As shown in the Modified XML) output.
Incoming XML
<xml>
<Body>
<Request>
<Container>
<name>test</name>
<Numbers>
<sn>//base64encodedstring//</sn>
<sn>//base64encodedstring//</sn>
<sn>//base64encodedstring//</sn>
</Numbers>
</Container>
</Request>
</Body>
</xml>
Modified XML
<xml>
<Body>
<Request>
<Container>
<name>test</name>
<Numbers>
<sn>//Decodedstring//</sn>
<sn>//Decodedstring//</sn>
<sn>//Decodedstring//</sn>
</Numbers>
</Container>
</Request>
</Body>
</xml>
As per the answer received: I can create an array of ListOfResults.
def listOfResults = new XmlSlurper()
.parseText(xml)
.Body.Request.Container.Numbers.sn
.collect { new String(it.text().decodeBase64()) }
I can do the following
def data = "<xml><Body><Request><Container><name>test</name><Numbers>"
for (i= 0; i <listOfResults.size(); i++)
{
data = data +"<sn>" +listOfResults[i] + "</sn>";
}
data = data + "<Numbers></Container></Request></Body></xml>";
Modified data
<xml><Body><Request><Container><name>test</name>
<Numbers>
<sn>decoded string</sn>
<sn>decoded string</sn>
<sn>decoded string</sn>
</Numbers> </Container></Request></Body></xml>
Is this the fastest way to do this operation? Is there any other better way?
You should be able to do (assuming your XML is in a string variable called xml):
def listOfResults = new XmlSlurper()
.parseText(xml)
.Body.Request.Container.Numbers.sn
.collect { new String(it.text().decodeBase64()) }
I want to include data retrieved from an XMLParser and build a new file using the MarkupBuilder. I'm having trouble figuring out how this would work.
If I'm taking the wrong approach here, such as not using MarkupBuilder and using something else, let me know. Thanks!
C:/file.xml:
<externalData>
<data><nestedData><soOnAndSoForth/></nestedData></data>
</externalData>
Code.groovy:
def writer = new StringWriter()
def xml = new groovy.xml.MarkupBuilder(writer).'root'("id":"foo") {
File content = new File("C:/file.xml")
def externalFile = new XmlParser(false,true,true).parse(content)
// may or may not modify this external data...
externalFile.each { elem -> ${elem} }
'moreData'('id':'myData')
}
println writer.toString()
Expected result:
<root id="foo">
<externalData>
<data><nestedData><soOnAndSoForth/></nestedData></data>
</externalData>
<moreData id="myData">
</root>
What I get:
<root id="foo">
<$ />
<moreData id="myData">
</root>
In Groovy given a GPath I want to find that the path exists in XML document. So that I can set a value for the node. I can use xmlslurper for parsing the document. GPath is a string path represented in slashes format.
Look at example:
def xml = '''
<?xml version="1.0" encoding="UTF-8"?>
<data>
<level0 id="1" t="0">
<level1 id="lev1id01" att1="2015-05-12" val="12" status="0" year="2015" month="05" />
</level0>
</data>
'''
// find all nodes named 'level1'
def level1Nodes = new XmlSlurper().parseText(xml).level0.level1
// display found nodes names
level1Nodes.each { node ->
println "node: ${node.name()}"
}
// Get 'year' attribute value for 'level1' node
def level1YearValue = level1Nodes.each { node ->
println "${node.#year}"
}
Could you be more specific in your question?
I want to create a xml file using SSJS on server. Is there a way to do so? Can anyone please give a sample code to create a xml file on server.
There are quite some ways. The seemingly easiest one is to create a string that looks like XML.
The next one would be the use of Java DOM classes. There is an article describing it.
Finally you can use SAX with a little helper class
Let us know how it goes.
Update: This would be my version of #Michael's code sample:
<?xml version="1.0" encoding="UTF-8"?>
<!-- XPage which is not rendered but returns data like XML, JSON, etc. -->
<!-- More: http://www.wissel.net/blog/d6plinks/shwl-7mgfbn -->
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.beforeRenderResponse><![CDATA[#{javascript:try {
var out = facesContext.getOutputStream();
var exCon = facesContext.getExternalContext();
var response = exCon.getResponse(); // get the response context
// set content type, e.g. ...
response.setContentType("text/xml");
// set caching option
response.setHeader("Cache-Control", "no-cache");
// write XML output ...
var result = new biz.taoconsulting.xmltools.SimpleXMLDoc();
result.setOut(out);
result.openTag("result");
result.dateTag("created", new java.util.Date());
result.addSimpleTag("Author",#UserName);
result.openTag("FruitList");
result.addComment("Stephan really likes the fruits example");
var attributes = new java.util.HashMap();
attributes.add("name","Durian");
attributes.add("color","white");
attributes.add("taste","Don't ask");
result.addEmptyTag("fruit",attributes);
result.closeDocument();
// close the output
exCon.responseComplete();
out.close();
} catch (e) {
print(e.toString());
}}]]>
</xp:this.beforeRenderResponse>
</xp:view>
Note the differences here:
I use the beforeRenderResponse event
Access to outputStream instead of writer (stream is not accessible in the afterRenderResponse event)
set the response complete to stop the page from further output, so you can simply type comments on the page what it does
use of the helper class
What seems a little odd when you read the source of the helper class: why not use the output stream in the constructor, so you won't miss it? - I would today add a second constructor with that, but the parameterless constructor allow me to define that class as a managed bean if I fancy that.
to "render" XML in a String as #Stefan suggested I would use the XAgent approach:
<?xml version="1.0" encoding="UTF-8"?>
<!-- XPage which is not rendered but returns data like XML, JSON, etc. -->
<!-- More: http://www.wissel.net/blog/d6plinks/shwl-7mgfbn -->
<xp:view xmlns:xp="http://www.ibm.com/xsp/core" rendered="false">
<xp:this.afterRenderResponse><![CDATA[#{javascript:try {
var writer = facesContext.getResponseWriter(), // get a writer object
response = facesContext.getExternalContext().getResponse(); // get the response context
// set content type, e.g. ...
response.setContentType("text/xml");
// set caching option
response.setHeader("Cache-Control", "no-cache");
// write XML output ...
writer.write(
'<?xml version="1.0"?>\n'
+ '<root>\n'
+ '<entity>Example Content</entity>\n'
+ '</root>\n'
);
// close the stream
writer.endDocument();
} catch (e) {
print(e.toString());
}}]]>
</xp:this.afterRenderResponse>
<xp:this.resources>
<xp:script src="/XBAN.jss" clientSide="false"></xp:script>
</xp:this.resources>
</xp:view>
Simply put his code into a newly created XPage and test it. Modify the lines in writer.write() to fit to your needs.