【发布时间】:2016-10-18 14:17:36
【问题描述】:
在 Java 中,当我尝试创建一个新的 InputSource 并将其传递给 ByteArrayInputStream 时,它不会创建 CharacterStream,也不会创建编码集。
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);
}
我正在执行上述操作,其中xml 是存储为字符串的有效 XML。 XML 本身如下。
<?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>
结果是result 是一个空字符串,因此noAgePir 没有设置。
提前感谢您的帮助。
已修复。
感谢所有帮助 - 主要问题在于我使用的 XPath。由于某种原因,XML 文件没有将 Output 视为有效标签 - 只有当我这样做时,/OutputRoot/child::node()[2]/AuthPlus/DataMatches/NoAgePri 才起作用。
仍然不知道为什么,但至少它有效。
【问题讨论】:
-
请在您的问题中包含
xml的值。请注意,您根本不需要在这里使用字符集;你可以简单地写new InputSource(new StringReader(xml))。 -
编辑了我的问题以包含 XML。当我像你建议的那样尝试新的
StringReader(xml)时,XPath 仍然只返回空字符串。 -
您的 XPath 表达式包含
Output/DataMatches,但 DataMatches 不是 Output 的子项。 -
感谢您指出@VGR,不幸的是它仍然没有检测到 XML 中的实际值 - 不断返回空字符串
标签: java xpath xml-parsing bytearrayinputstream