BookmarkSubscribeSubscribe to RSS Feed
uday_guntupalli

Community Trekker

Joined:

Sep 15, 2014

Parse XML Help

All, 
   I have some XML, I would like to parse using JSL. I am not familiar with XML, so pardon my ignorance. 


   <Table><Rule DayName=\!"Sunday\!" Val1=\!"1\!"/>

 

   What I tried is the following: 

 

Parse XML( Test,
			On Element( "Table", Start Tag( New Table( XML Attr( "Table" ) ) ) ),
			On Element("Rule",End Tag( New Column( XML Attr( "DayName" ), Set Values( Parse( XML Text() ) ) ) ) )
		 );

  I know, I wold have to add one more rule using On Element to parse the "Val1" attribute, but that is not working, what I would like the output to be is : 

 

image.png

Best
Uday
1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Craige_Hales

Staff

Joined:

Mar 21, 2013

Solution

Re: Parse XML Help

test data

Test =
"\[
<Table>
    <Rule DayName="Sunday" Val1="1"/>
    <Rule DayName="Monday" Val1="2"/>
    <Rule DayName="Tuesday" Val1="3"/>
</Table>
]\";

You can get what you asked for like this:


Parse XML( Test,
    On Element( "Table", Start Tag( dt1 = New Table( XML Attr( "Table" ) ) ) ),
    On Element( "Rule", End Tag( New Column( XML Attr( "DayName" ), Set Values( Eval List( {XML Attr( "Val1" )} ) ) ) ) )
);

Capture.PNGone way

But I think a better intepretation of the file might look like this:

Parse XML( Test,
    On Element( "Table", Start Tag( dt2 = New Table( XML Attr( "Table" ), New Column( "DayName" ), New Column( "Val1" ) ) ) ),
    On Element(
        "Rule",
        End Tag(
            dt2 << addrow( 1 );
            dt2:DayName = XML Attr( "DayName" );
            dt2:Val1 = XML Attr( "Val1" );
        )
    )
);

Capture2.PNGAnother way

(You don't want the parse(...); the example in the scripting index that uses it also has a JSL matrix in the XML file that needs the parse. Your example has a single number, in quotation marks. You could use the num() function to make the conversion so the column would be numeric.   dt2:Val1 =num( XML Attr( "Val1" ));  )

Craige
1 REPLY
Highlighted
Craige_Hales

Staff

Joined:

Mar 21, 2013

Solution

Re: Parse XML Help

test data

Test =
"\[
<Table>
    <Rule DayName="Sunday" Val1="1"/>
    <Rule DayName="Monday" Val1="2"/>
    <Rule DayName="Tuesday" Val1="3"/>
</Table>
]\";

You can get what you asked for like this:


Parse XML( Test,
    On Element( "Table", Start Tag( dt1 = New Table( XML Attr( "Table" ) ) ) ),
    On Element( "Rule", End Tag( New Column( XML Attr( "DayName" ), Set Values( Eval List( {XML Attr( "Val1" )} ) ) ) ) )
);

Capture.PNGone way

But I think a better intepretation of the file might look like this:

Parse XML( Test,
    On Element( "Table", Start Tag( dt2 = New Table( XML Attr( "Table" ), New Column( "DayName" ), New Column( "Val1" ) ) ) ),
    On Element(
        "Rule",
        End Tag(
            dt2 << addrow( 1 );
            dt2:DayName = XML Attr( "DayName" );
            dt2:Val1 = XML Attr( "Val1" );
        )
    )
);

Capture2.PNGAnother way

(You don't want the parse(...); the example in the scripting index that uses it also has a JSL matrix in the XML file that needs the parse. Your example has a single number, in quotation marks. You could use the num() function to make the conversion so the column would be numeric.   dt2:Val1 =num( XML Attr( "Val1" ));  )

Craige