これらの線に沿って何か:
public String getNR(File inputXml, String inputName)
{
String nr = null;
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new File(inputXml));
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
XPathExpression expr = xpath.compile("//list/person/name[text()=" + "'" + inputName + "'" + "]/parent::*/nr");
Object exprEval = expr.evaluate(doc, XPathConstants.NODESET);
if (exprEval != null && exprEval instanceof NodeList)
{
NodeList nodeList = (NodeList)exprEval;
if (nodeList.getLength() == 1)
{
nr = nodeList.item(0).getTextContent();
}
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
return nr;
}
あなたがgetNR(inputxml, "Bob")
を呼び出した場合、それは9
を返す必要があります。