Info from unknown XML

Post Reply
cwswitch
Member
Posts: 55
Joined: Fri Feb 24, 2017 12:25 am

Info from unknown XML

Post by cwswitch » Mon Sep 10, 2018 3:50 am

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

dkelly
TOP CONTRIBUTOR
Posts: 649
Joined: Mon Nov 29, 2010 8:45 pm
Location: Alpharetta GA USA
Contact:

Re: Info from unknown XML

Post by dkelly » Tue Sep 11, 2018 8:17 pm

Using XPath

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

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

cstevens
Member
Posts: 75
Joined: Tue Feb 12, 2013 8:42 pm

Re: Info from unknown XML

Post by cstevens » Tue Sep 11, 2018 9:56 pm

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

cwswitch
Member
Posts: 55
Joined: Fri Feb 24, 2017 12:25 am

Re: Info from unknown XML

Post by cwswitch » Tue Sep 11, 2018 10:59 pm

dkelly, cstevens,

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

Post Reply