0 votes
2.1k views
in Javascript API by (21.5k points)

1 Answer

0 votes
by (30.6k points)

Every business object serialises to XML with one call, and for a custom document layout you either transform that XML (XSLT) or build the document with E4X (8.5.x) / a Java DOM (BAW 20+):

// 1. the built-in serialisation (element names = field names, nested objects and lists included)
var xml = tw.local.order.toXMLString();            // String; toXML() returns the XML document object on 8.5.x
// <Order><number>ORD-42</number><customer><name>Ana</name></customer><lines><OrderLine><item>A</item><qty>2</qty></OrderLine></lines></Order>

// 2. custom layout with E4X (BPM 8.5.x / 8.6 server scripts)
var doc = <order xmlns="http://example.com/order"/>;
doc.@id = tw.local.order.number;
doc.customer = <customer name={tw.local.order.customer.name} id={tw.local.order.customer.id}/>;
for (var i = 0; i < tw.local.order.lines.listLength; i++) {
  var l = tw.local.order.lines[i];
  doc.appendChild(<line item={l.item} qty={l.qty}/>);
}
tw.local.xml = '<?xml version="1.0" encoding="UTF-8"?>' + doc.toXMLString();

// 3. custom layout without E4X (BAW 20+): Java DOM
var d = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
var root = d.createElementNS("http://example.com/order", "order"); root.setAttribute("id", tw.local.order.number); d.appendChild(root);
var cust = d.createElement("customer"); cust.setAttribute("name", tw.local.order.customer.name); root.appendChild(cust);
for (var i = 0; i < tw.local.order.lines.listLength; i++) { var e = d.createElement("line"); e.setAttribute("item", tw.local.order.lines[i].item); e.setAttribute("qty", String(tw.local.order.lines[i].qty)); root.appendChild(e); }
var w = new java.io.StringWriter(); var t = javax.xml.transform.TransformerFactory.newInstance().newTransformer();
t.setOutputProperty("indent", "yes"); t.transform(new javax.xml.transform.dom.DOMSource(d), new javax.xml.transform.stream.StreamResult(w));
tw.local.xml = String(w.toString());

Option 4: XSLT on the built-in XML (question 757) when the target schema is fixed by a partner - keep the stylesheet as a managed asset. The reverse direction (XML to BO) has no general one-liner - use the XPath approach of question 109 and assign the fields (or a JSON round trip on BAW 20+). Write E4X code today only if the app will never move to BAW 20+ - it is removed there.

References

Related questions

0 votes
1 answer 1.7k views
0 votes
1 answer 1.3k views
0 votes
1 answer 1.1k views
0 votes
1 answer 1.5k views
0 votes
1 answer 2.3k views
0 votes
1 answer 942 views
0 votes
1 answer 2.0k views
0 votes
1 answer 1.1k views
0 votes
1 answer 2.6k views
0 votes
1 answer 1.0k views

723 questions

807 answers

98 comments

4.9k users

Join BPM Community Discord Channel

Welcome to BPM Tips Q&A, Community wiki/forum where you can ask questions and receive answers from other IBM BPM experts and members of the community. Users with 2000 points will automatically be promoted to expert level.
Created by Dosvak LLC
Our Youtube Channel
...