未找到属性时返回null的JAVA Xpath表达式
在下面的xml示例中未找到属性时返回null的JAVA Xpath表达式,java,xml,xpath,Java,Xml,Xpath,在下面的xml示例中 <employee> <payment contractType="1"> <income type="0"> <gr amount="2063.00" kae="211" code="1" /> <gr amount="400.00" kae="215" code="6" /> <et a
<employee>
<payment contractType="1">
<income type="0">
<gr amount="2063.00" kae="211" code="1" />
<gr amount="400.00" kae="215" code="6" />
<et amount="47.55" kae="292" code="4012501" />
<et amount="105.21" kae="293" code="4052000" />
<de amount="88.15" code="4003101" />
</income>
</payment>
</employee>
<employee>
<payment contractType="1">
<income type="0">
<gr amount="70.00" kae="213" code="4" />
<gr amount="1560.00" kae="211" code="1" />
</income>
</payment>
</employee>
我需要为“code”=“4”获取“amount”的值。如果income节点不包含此类数据(gr with code=“4”),我需要返回null或boolean false之类的值。目的是查看xml文件中的所有员工,如果他们没有任何金额,则将其加载到带有0的Arraylist中,代码为4,否则加载金额值
我用于此部件的代码:
public class ReadXMLfile {
public static void main(String[] args) {
try {
FileInputStream file = new FileInputStream(new File("E2015_1_1.xml"));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
Document xmlDocument = builder.parse(file);
XPath xPath = XPathFactory.newInstance().newXPath();
expression = "/psp/body/organizations/organization/employees/employee/payment/income[@type='0']/gr";
nodeList = (NodeList) xPath.compile(expression).evaluate(xmlDocument, XPathConstants.NODESET);
ArrayList<String> gr4incomeList = new ArrayList<String>();
for (int i = 0; i < nodeList.getLength(); i++) {
String acode = (String)nodeList.item(i).getAttributes().getNamedItem("code").getNodeValue();
System.out.println("acode = '" + acode + "'");
if (acode.equals("4")){
System.out.println(nodeList.item(i).getAttributes().getNamedItem("amount").getNodeValue());
gr4incomeList.add(nodeList.item(i).getAttributes().getNamedItem("amount").getNodeValue());
System.out.println("array = " + gr4incomeList.get(i));
}else
gr4incomeList.add("0000");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (XPathExpressionException e) {
e.printStackTrace();
}
}
公共类ReadXMLfile{
公共静态void main(字符串[]args){
试一试{
FileInputStream文件=新的FileInputStream(新文件(“E2015_1_1.xml”);
DocumentBuilderFactory builderFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder=builderFactory.newDocumentBuilder();
Document xmlDocument=builder.parse(文件);
XPath=XPathFactory.newInstance().newXPath();
expression=“/psp/body/organization/organization/employees/employees/payment/income[@type='0']/gr”;
nodeList=(nodeList)xPath.compile(expression).evaluate(xmlDocument,XPathConstants.NODESET);
ArrayList gr4incomeList=新的ArrayList();
for(int i=0;i
}
问题是它会在arraylist中为找到的任何gr写入“0000”,但代码为“4”的gr除外
我真的被卡住了
有什么想法吗?谢谢你们 GR4收入表添加(“0000”);如果你想写“0”,你不应该使用gr4incomeList.add(“0”);是否可以有多个代码为“4”的项目,还是只有一个?@jlrise每位员工只能有一个代码为“4”的项目。感谢you@nafas我需要将其存储为4位字符串,thanks@Bonzay似乎你的代码只适用于第一个标记为code=“4”的员工。我说得对吗?
XPathExpression exp = xpath.compile("/employees/employee");
NodeList nodeList = (NodeList)exp.evaluate(xmlDocument, XPathConstants.NODESET);
XPathExpression grexp = xpath.compile("payment/income/gr[@code='4']");
XPathExpression amexp = xpath.compile("payment/income/gr[@code='4']/@amount");
for( int i = 0; i < nodeList.getLength(); ++i ){
Node item = nodeList.item( i );
Object resexp1 = grexp.evaluate( item, XPathConstants.NODE );
if( resexp1 != null ){
String resexp2 = amexp.evaluate( item );
System.out.println( resexp2 );
} else {
System.out.println( "0000" );
}
}
0000
70.00