0

In Java, when I attempt to create a new InputSource and hand it a ByteArrayInputStream it does not create a CharacterStream nor is encoding set.

XPathExpression compilablePath = xpath.compile("/OutputRoot/Output/AuthPlus/DataMatches/NoAgePri");
String result = compilablePath.evaluate(
    new InputSource(new StringReader(xml))
);
if (!"".equals(result)) {
    this.noAgePri = Integer.parseInt(result);
}

I'm doing the above, where xml is a valid XML stored as a String. The XML itself is as below.

<?xml version="1.0" encoding="utf-16"?>
<OutputRoot>
  <Output>
    <Control>
      <Reference>ZX1J49CKQ4</Reference>
    </Control>
    <AuthPlus>
      <ApplicantIdentifier>1</ApplicantIdentifier>
      <AuthPlusRef />
      <IACA>
        <NoPriItem>12</NoPriItem>
        <StrtOldPri>199402</StrtOldPri>
        <NoSecItem>4</NoSecItem>
        <NoSecSrc>1</NoSecSrc>
        <StrtOldSec>201608</StrtOldSec>
      </IACA>
      <AOCA>
        <NoPriItem>1</NoPriItem>
        <StrtOldPri>200910</StrtOldPri>
        <NoSecItem>1</NoSecItem>
        <NoSecSrc>1</NoSecSrc>
      </AOCA>
      <IAPA>
        <NoPriItem>0</NoPriItem>
        <NoSecItem>0</NoSecItem>
        <NoSecSrc>0</NoSecSrc>
      </IAPA>
      <AOPA>
        <NoPriItem>0</NoPriItem>
        <NoSecItem>0</NoSecItem>
        <NoSecSrc>0</NoSecSrc>
      </AOPA>
      <DataMatches>
        <NoAgePri>10</NoAgePri>
      </DataMatches>
      <Decision>
        <DecCode>AU01</DecCode>
        <DecText>The Applicant has been Authenticated to your required 'Level 1'</DecText>
        <AuthIndex>80</AuthIndex>
        <AuthText>A high level of Authentication has been found for the identity supplied</AuthText>
        <IDConfLvl>1</IDConfLvl>
        <IDConfText>The identity supplied has been confirmed at the required 'Level 1'</IDConfText>
        <HighRiskCount>0</HighRiskCount>
      </Decision>
    </AuthPlus>
  </Output>
</OutputRoot>

The result is that result is an empty string and thus noAgePir is not set.

Thanks in advance for any help.

Fixed.

Thanks for all the help - the main issue was with the XPath I was using. For some reason the XML file was not considering Output a valid tag - only when I did /OutputRoot/child::node()[2]/AuthPlus/DataMatches/NoAgePri did it work.

Still not sure why but it works at least.

MPhil123
  • 1
  • 2
  • 1
    Please include the value of `xml` in your question. Note that you don’t need to use a charset here at all; you can simply write `new InputSource(new StringReader(xml))`. – VGR Oct 18 '16 at 14:21
  • Edited my question to include the XML. When I try new `StringReader(xml)` like you suggest the XPath still only returns empty strings. – MPhil123 Oct 18 '16 at 14:29
  • Your XPath expression contains `Output/DataMatches` but DataMatches is not a child of Output. – VGR Oct 18 '16 at 14:41
  • Thanks for pointing that out @VGR unfortunately it still isn't detecting the actual value in the XML - keeps returning the empty String – MPhil123 Oct 18 '16 at 14:50

1 Answers1

0

The result of your XPath expression is empty because your path is wrong: you omitted the step AuthPlus.

You need to apply a divide-and-conquer approach to solving problems like this. You can check that your API invocation is correct by trying it with a trivial XPath expression, and you can check that your XPath is correct by trying it in an XPath testing tool (e.g. oXygen). You seem to have been jumping to conclusions about the cause of the error without considering the simplest possibility first.

(And having said that, if xml is a string, then you should be using a StringReader, rather than converting it to bytes just so the XML parser can convert it back to a string.)

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Hi, thanks for the help - the corrections you pointed out I have applied (as was also suggested by VGR in the comments of my original question) which unfortunately still doesn't fix the problem - I have now edited my original question to show the corrected code. – MPhil123 Oct 18 '16 at 14:59