Stringing Together XML

Stringing Together XML

Someone just asked how to convert a string into XML.  A common use case is to retrieve XML stored as a string in the database.  Although you could argue that it would be better to store XML using the XML facilities of the Oracle database the fact is we still see lots of XML stored as VarChar2 or CLobs.  So how can we read these into BPEL?  Well in addition to explaining this I thought it was also a good excuse to explain the use of the append rule within an assign and also a use for the anyType.

Reading XML

Fortunately the developers foresaw the problem of converting strings to XML and have provided a handy function ora:parseEscapedXML which takes a string as a parameter and returns an XML document.  The string can be either a full XML document or just a document fragment.  Similarly it can have an explicit namespace or no namespace.
I have uploaded a simple BPEL process that takes an XML string as input and returns the DOM representation of that string.

Using an anyType

For the input I take a string type but for the output I have used an anyType.  This allows me to legitimatly assign it any value.  Note that although anyType is not supported by the graphical XPath builder in the XPath wizard it is supported by the editor, which will drill you down to the anyType.

Using Append instead of Copy

The other thing I have done in the assign is to use an append rule to add the parsed XML underneath the base output element.  This allows us to see the entire result.  If I had used a copy rule then the top level element of the parsed XML would have been thrown away and only its contents copied.  Using the append rule causes the entire subtree selected by the XPath expression to be placed underneath the target element.  Because the target is an anyType I don't have to worry about the XML conforming to any particular schema, I can dump any well formed XML in there.

Testing it Out

You may want to try it on the following input data to see how it works.  Note that there is no difference in the way documents and document fragments are handled.  the only requirement is that the XML be well formed.

Fragment Without a namespace

<NAME><FIRST>Antony</FIRST><LAST>Reynolds</LAST></NAME>

Fragment With a namespace

<NAME xmlns="http://blog.oracle.com/reynolds"><FIRST>Antony</FIRST><LAST>Reynolds</LAST></NAME>

Document Without a namespace

<?xml version="1.0" encoding="UTF-8"?><NAME><FIRST>Antony</FIRST><LAST>Reynolds</LAST></NAME>

Document With a namespace

<?xml version="1.0" encoding="UTF-8"?><NAME xmlns="http://blog.oracle.com/reynolds"><FIRST>Antony</FIRST><LAST>Reynolds</LAST></NAME>

Comments:

Post a Comment:
Comments are closed for this entry.
About

Musings on Fusion Middleware and SOA Picture of Antony Antony works with customers across the US and Canada in implementing SOA and other Fusion Middleware solutions. Antony is the co-author of the SOA Suite 11g Developers Cookbook, the SOA Suite 11g Developers Guide and the SOA Suite Developers Guide.

Search

Archives
« July 2014
SunMonTueWedThuFriSat
  
1
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today