Parse a XML string in TTCN

200 Views Asked by At

I am writing a test case in TTCN-3 using eclipse. In one of the test case, I got the response from simulator a XML string which is containing the multiple records, as below:

<Templates><Template><Id>1001</Id><Category>refill</Category><Description>Template description</Description><ApplicationId>AIR</ApplicationId><Name>Template name</Name><SchemaVersion>3.3.14</SchemaVersion></Template><Template><Id>1002</Id><Category>refill</Category><Description>Template Description 1</Description><ApplicationId>AIR</ApplicationId><Name>Template name</Name><SchemaVersion>3.3.14</SchemaVersion></Template></Templates>

Now, I need to parse this xml string and get the template objects out of it to use them further in the test case.

Here is Template Object definition:

public type record Template
{
    charstring id,
    charstring category,
    charstring description,
    charstring applicationId,
    charstring name,
    charstring schemaVersion
}
public type record of Template Templates;

I am new to TTCN, so any help is much appreciated. Thanks.

1

There are 1 best solutions below

0
Nomce On

You mentioned Eclipse, and in that case it can be either Spirent's TTWorkbench propriertary solution, or Eclipse's (Ericsson) TITAN open-source implementation of TTCN-3 compiler and executor. Here I will take as example the open-source TITAN.

Titan has internal codec for XML that is explained here and here. As you can see in the second example:

external function enc_AccessControlPolicy(in AccessControlPolicy pdu) return octetstring
with { extension "prototype (convert) encode(XER:XER_EXTENDED)" }

external function dec_AccessControlPolicy(in octetstring stream) return AccessControlPolicy
with { extension "prototype (convert) decode(XER:XER_EXTENDED)" }

This will convert the XML to TTCN-3 structure and vice-versa.

You can also define new functions in C/C++ and write a codec by yourself, using the aforementioned method (if you add a new file with 'dec_AccessControlPolicy' and 'enc_AccessControlPolicy' as functions). This can be useful for some complex and (sometimes) non-standards-compliant protocols (see MQTT, CoAP and other codec implementations in Titan).