Page 1 of 1

Info from unknown XML

Posted: Mon Sep 10, 2018 3:50 am
by cwswitch
Hi,

I'm trying to solve an issue where I receive XML similar to the below. It could have any number of sections and I'm looking for a number for how many sections there are. This example has 8.

Along with that, I'm looking for the Quantity value for each of the sections.

Code: Select all

<Kinds>
    <Kind>
        <Key>27747</Key>
        <QQDSKey>0</QQDSKey>
        <Name>A</Name>
        <Quantity>6</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
    <Kind>
        <Key>27748</Key>
        <QQDSKey>0</QQDSKey>
        <Name>B</Name>
        <Quantity>14</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
    <Kind>
        <Key>27749</Key>
        <QQDSKey>0</QQDSKey>
        <Name>C</Name>
        <Quantity>7</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
    <Kind>
        <Key>27750</Key>
        <QQDSKey>0</QQDSKey>
        <Name>D</Name>
        <Quantity>13</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
    <Kind>
        <Key>27751</Key>
        <QQDSKey>0</QQDSKey>
        <Name>E</Name>
        <Quantity>8</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
    <Kind>
        <Key>27752</Key>
        <QQDSKey>0</QQDSKey>
        <Name>F</Name>
        <Quantity>12</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
    <Kind>
        <Key>27753</Key>
        <QQDSKey>0</QQDSKey>
        <Name>G</Name>
        <Quantity>9</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
    <Kind>
        <Key>27754</Key>
        <QQDSKey>0</QQDSKey>
        <Name>H</Name>
        <Quantity>11</Quantity>
        <IsCondensed>True</IsCondensed>
        <KindSections>
            <KindSection>
                <Name>Single-Section</Name>
            </KindSection>
        </KindSections>
    </Kind>
</Kinds>
Any tips or example scripts of how to pull the info, greatly appreciated, thanks

Re: Info from unknown XML

Posted: Tue Sep 11, 2018 8:17 pm
by dkelly
Using XPath

1. returns count of Kind elements
count(//Kind)

2. returns individual quantity values
//Kind/Quantity/text()

Re: Info from unknown XML

Posted: Tue Sep 11, 2018 9:56 pm
by cstevens
You may need to loop through the individual nodes to get each value. Something like

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	//assuming this XML file is coming into the script element as a file instead of metadata
	var xmlDoc = new Document(job.getPath());
	var kindNodes = xmlDoc.evalToNodes("//Kinds/Kind", null);
	var numKindNodes = kindNodes.length;
	var values = [];
	
	for (i=0; i<numKindNodes; i++){
		values.push(kindNodes.at(i).evalToString("./Quantity/text()", null));
	}
	
	s.log(1, "Input XML had " + numKindNodes + " Kind nodes with Quantity values of: " + values.toString());
}
That game me this response on your sample:

Input XML had 8 Kind nodes with Quantity values of: 6,14,7,13,8,12,9,11

Re: Info from unknown XML

Posted: Tue Sep 11, 2018 10:59 pm
by cwswitch
dkelly, cstevens,

many thanks both for the help! I'll try out these ideas