Search code examples
javaxmlxpathnodelist

Evaluate XPath And Retrieve Elements into Java List


I am using xPath in Java and I would like to retrieve the nodes team1_win_perc, team2_win_perc and draw_perc. Here is the xml document:

<stats timestamp="1628874073" date="08/13/2021 12:01:13">
    <common>
        <EV_AVERAGE_HOME>0.006669</EV_AVERAGE_HOME>
        <EV_AVERAGE_AWAY>0.00193</EV_AVERAGE_AWAY>
        <EV_AVERAGE_DRAW>0.007402678</EV_AVERAGE_DRAW>
    </common>
    <games>
        <id348812>
            <gameid gsid="3509729">348812</gameid>
            <league>BUND</league>
            <team1RotationNumber>150540</team1RotationNumber>
            <team1Name>Bayern Munchen</team1Name>
            <team1_win_perc>62.1</team1_win_perc>
            <team2RotationNumber>150541</team2RotationNumber>
            <team2Name>Arsenal</team2Name>
            <team2_win_perc>17.8</team2_win_perc>
            <draw_perc>20.1</draw_perc>
        </id348812>
        <id348813>
            <gameid gsid="3509730">348813</gameid>
            <league>EPL</league>
            <team1RotationNumber>150543</team1RotationNumber>
            <team1Name>Tottenham</team1Name>
            <team1_win_perc>50</team1_win_perc>
            <team2RotationNumber>150544</team2RotationNumber>
            <team2Name>Chelsea</team2Name>
            <team2_win_perc>25</team2_win_perc>
            <draw_perc>25</draw_perc>
        </id348813>
    </games>
</stats>

And here's what I've managed to do so far:

            final DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            final DocumentBuilder db = dbf.newDocumentBuilder();
            final Document document = db.parse(new InputSource(
                    new ByteArrayInputStream(xmlResponseString.getBytes(UTF_8))));

            final XPathFactory xpathFactory = XPathFactory.newInstance();
            final XPath xpath = xpathFactory.newXPath();

            NodeList gameIds = (NodeList) xpath.evaluate("stats/games/*",
                    document, NODESET);

            final List<Double> homeWinPercentages = new ArrayList<>();
            final List<Double> awayWinPercentages = new ArrayList<>();
            final List<Double> drawPercentages = new ArrayList<>();

            for (int i = 0; i < gameIds.getLength(); ++i) {
                Node node = gameIds.item(i);

                homeWinPercentages.add(Double.valueOf((String) xpath.evaluate("stats/games/"
                        + node.getNodeName() + "/team1_win_perc", document, STRING)));
                awayWinPercentages.add(Double.valueOf((String) xpath.evaluate("stats/games/"
                        + node.getNodeName() + "/team2_win_perc", document, STRING)));
                drawPercentages.add(Double.valueOf((String) xpath.evaluate("stats/games/"
                        + node.getNodeName() + "/draw_perc", document, STRING)));

            }

Is there a way to avoid evaluating the xml document 3 times? I'd like to create a List with class Probability which includes the fields team1_win_perc, team2_win_perc and draw_perc.


Solution

  • I would consider using Saxon 10 (or 9.9) HE with XPath 3.1 to simply return an XPath 3.1 XDM map with three sequences of double values:

    import net.sf.saxon.s9api.*;
    
    import java.io.File;
    
    public class Main {
    
        public static void main(String[] args) throws SaxonApiException {
            Processor processor = new Processor(false);
    
            DocumentBuilder docBuilder = processor.newDocumentBuilder();
    
            XdmNode inputDoc = docBuilder.build(new File("sample1.xml"));
    
            XdmMap result = (XdmMap) processor.newXPathCompiler().evaluate(
                    "let $games := stats/games/* return map { 'homeWinPercentages' : $games/team1_win_perc/number(), 'awayWinPercentages' : $games/team2_win_perc/number(), 'drawPercentages' : $games/draw_perc/number() }",
                    inputDoc
            );
    
            System.out.println(result);
        }
    }
    

    Resulting output:

    map{"awayWinPercentages":(1.78e1,2.5e1),"drawPercentages":(2.01e1,2.5e1),"homeWinPercentages":(6.21e1,5.0e1)}