user2085909
user2085909

Reputation: 1

How to convert XML to tag value pairs using Tcl

I have an XML like this :

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Request OrderType="ADD_SUB" TransactionId="XMLTXN000000299" Priority="1">
  <ApplicationKeyValues>
    <Key KeyName="TESTID" KeyValue="GT_SYS_001"/>
  </ApplicationKeyValues>
  <ServiceChangeRequest Operation="Activate" SubscriberKey="704020328567323" AlternateSubscriberKey="50223101123">
    <RequiredServices>
      <ServiceDescription ServiceTag="GSMSUB">
        <ParameterDesc ParameterTag="IMSI" ParameterValue="704020328567323"/>
        <ParameterDesc ParameterTag="MSISDN" ParameterValue="50223101123"/>
      </ServiceDescription>
      <ServiceDescription ServiceTag="AER1"/>
    </RequiredServices>
  </ServiceChangeRequest>
</Request>

This is my first time with TCL script. The script should convert any similar XML to tag value pairs . Please help

Want it to look like something like this

{{ORDERTYPE "ADD_SUB"} {TRANSACTIONID "RMI020222983029285"} {PRIORITY "7"}{APPLICATIONKEYS { { IMSI "704020328567330" } { MSISDN "50223101130" }} }{SERVICEREQUEST{{ OPERATION "ADD" }{ KEYS { { IMSI "704020328567330" } { MSISDN "50223101130" }} }{SERVICES { {GSMSUB{ { MSISDN "50223101130" }{ OPERATION "ADD" }{ IMSI "704020328567330" }{ KI "A3E60077BDDF73C4425E371C90E0C0E6" } } }{AN7D{ { OPERATION "ADD" } } }{VMAS{ { OPERATION "ADD" } } }{VC3G{ { OPERATION "ADD" } } }{AMMS{ { OPERATION "ADD" } } }{CAMP{ { OPERATION "ADD" } } }{ALLE{ { OPERATION "ADD" } } }{AROA{ { OPERATION "ADD" } } }{CINR{ { RSA "2" }{ OPERATION "ADD" } } }}}} } }

Upvotes: 0

Views: 155

Answers (1)

mrcalvin
mrcalvin

Reputation: 3434

tDOM is your friend, especially tDOM's asList method:

Returns the DOM substree starting form the current node as a nested Tcl list

Watch:

% package req tdom
0.9.2
% set doc [dom parse $str]
domDoc0x7feeebc06ef0
% set root [$doc documentElement]
domNode0x7feeebc07ae0
% $root asList
Request {OrderType ADD_SUB TransactionId XMLTXN000000299 Priority 1} {{ApplicationKeyValues {} {{Key {KeyName TESTID KeyValue GT_SYS_001} {}}}} {ServiceChangeRequest {Operation Activate SubscriberKey 704020328567323 AlternateSubscriberKey 50223101123} {{RequiredServices {} {{ServiceDescription {ServiceTag GSMSUB} {{ParameterDesc {ParameterTag IMSI ParameterValue 704020328567323} {}} {ParameterDesc {ParameterTag MSISDN ParameterValue 50223101123} {}}}} {ServiceDescription {ServiceTag AER1} {}}}}}}}

That said, as it has been pointed out to you, once having consumed your XML string using tDOM, you will not need the nested-list structure. You would rather use the tDOM structure to access the data of interest.

Upvotes: 3

Related Questions