1

i made a script using tcldom:

package require utils
package require testrunscheduler
package require tdom
tla::TSConfig::init -schedulerContext "Reporting" -environment production
tla::TSDBFactory::getConnection db
set testCaseList [$db doSQL "SELECT root_name,suite_name,case_name FROM ics where test_type = 'tce' limit 1"]
set item [join $testCaseList ""]

set doc [dom createDocument testCases]
set root [$doc documentElement]
set subnode [$doc createElement testCase]
$root appendChild $subnode

foreach item $item {
set node [$doc createElement root]
$node appendChild [$doc createTextNode $item]
$subnode appendChild $node
}

Output i get is :

<testCases>
    <testCase>
        <root>SPB</root>
        <root>subscriberServices</root>
        <root>jmsServices</root>
    </testCase>
</testCases>

but i want output to be like :

<testCases>
    <testCase>
        <root>SPB</root>
        <suite>subscriberServices</suite>
        <case>jmsServices</case>
    </testCase>
</testCases>

i used foreach for this, but it makes only for root, i am missing probably, this structure would iterate itself and grow as on the users input from sql query.

<testCases>
    <testCase>
        <root>demoRoot</root>
        <suite>demoSuite</suite>
        <case>demoCase</case>
        <testCase>test_demo001</testCase>
    </testCase>
    <testCase>
        <root>demoRoot</root>
        <suite>demoSuite</suite>
        <case>demoCase</case>
        <testCase>test_demo002</testCase>
    </testCase>
</testCases>

Please help me to get this kind of output, its very tedious to get this putput which is repetative but with one structure.

vinay
  • 53
  • 1
  • 8
  • Take a look at the tdom package, which is excellent for this kind of tasks. There is a tutorial on the wiki, search for tdom. – Peter Lewerin Dec 27 '16 at 13:26
  • Which wiki page were you looking at? – Donal Fellows Dec 27 '16 at 14:37
  • Tdom works the other way, I.e XML to raw or text conversion is what I understood , there is another package called xmlgen but that package is not installed and not allowed to install in my testing environment @Peter – vinay Dec 28 '16 at 01:48
  • @vinay: tdom works both ways, actually. I use it both to parse HTML/XML and to build it. – Peter Lewerin Dec 28 '16 at 06:35
  • @Peter Lewerin: is there any example script which could help me get the output as you said using tdom? – vinay Dec 28 '16 at 07:09

1 Answers1

2
package require tdom

set data {
    {demoRoot1 demoSuite1 demoCase1}
    {demoRoot2 demoSuite2 demoCase2}
}

set doc [dom createDocument testCases]
set root [$doc documentElement]

dom createNodeCmd elementNode testCase
dom createNodeCmd elementNode root
dom createNodeCmd elementNode suite
dom createNodeCmd elementNode case
dom createNodeCmd textNode t

foreach line $data {
    lassign $line _root suite case
    # or (if you don't have lassign) foreach {_root suite case} $line break

    $root appendFromScript {
        testCase {
            root {t $_root}
            suite {t $suite}
            case {t $case}
            testCase {t [format {test_demo%03d} [incr num]]}
        }
    }
}

$doc asXML

Output:

<testCases>
    <testCase>
        <root>demoRoot1</root>
        <suite>demoSuite1</suite>
        <case>demoCase1</case>
        <testCase>test_demo001</testCase>
    </testCase>
    <testCase>
        <root>demoRoot2</root>
        <suite>demoSuite2</suite>
        <case>demoCase2</case>
        <testCase>test_demo002</testCase>
    </testCase>
</testCases>

See the tutorial on the wiki for more examples.

Documentation: break, foreach, format, if, incr, lassign, package, set, tdom (package)

Peter Lewerin
  • 13,140
  • 1
  • 24
  • 27
  • i have reformatted the question, please help me get the output as mentioned above – vinay Dec 29 '16 at 04:01
  • @vinay: like this? – Peter Lewerin Dec 29 '16 at 17:19
  • yeah that helps , thanks a lot.. i had to modifiy a bit according to my requirement to create test_demo00# – vinay Jan 05 '17 at 10:39
  • hey Peter can you help this done? i have another query but related to this problem: http://stackoverflow.com/questions/18216742/is-that-possible-to-create-a-list-of-list-in-tcl @Peter Lewerin – vinay Jan 18 '17 at 07:18