0

I know this is related to many questions out there, but I could not find an example that used an xml layout like mine.

<product name="ANALYTICS" count="24" occurred_at_time="1386648300000">
    <user_type name="Unknown" count="24" occurred_at_time="1386648300000">
        <session_source name="Web" count="24" occurred_at_time="1386648300000"/>
    </user_type>
</product>
<product name="CARSWELL" count="492" occurred_at_time="1386651900000">
    <user_type name="External" count="492" occurred_at_time="1386651900000">
        <session_source name="Web" count="492" occurred_at_time="1386651900000"/>
    </user_type>
    <user_type name="Internal" count="19" occurred_at_time="1386622200000">
        <session_source name="UNKNOWN" count="12" occurred_at_time="1386624300000" />
        <session_source name="Web" count="10" occurred_at_time="1386604200000" />
    </user_type>
</product>

I am looping through a properties file that specifies which products we're interested within the xml and am having a hard time creating the right expression.

for (int j = 0; j < Products.length; j++) {
     XPathFactory xPathfactory = XPathFactory.newInstance();
     XPath xpath = xPathfactory.newXPath();

     try {
          XPathExpression expr = xpath.compile("product[@name=" + Products[j] + "]attribute::count");
          CompareNextWeb = expr.evaluate(doc);
          System.out.println(CompareNextWeb);

     } catch (XPathExpressionException ex) {
          Logger.getLogger(NextReport.class.getName()).log(Level.SEVERE, null, ex);
     }
}

So I want to be able to select the count, and in the future, occured_at_time, with this expression "product[@name=" + Products[j] + "]attribute::count". Really I just need to know if this is possible given the circumstances, or I need to come at this a different way.

1 Answer 1

1

You want xpath.compile("product[@name='" + Products[j] + "']/attribute::count"); or xpath.compile("product[@name='" + Products[j] + "']/@count");.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.